< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page
rev 12069 : [mq]: 8058779-Faster-implementation-of-String-replace-SB

*** 2245,2256 **** * @param replacement The replacement sequence of char values * @return The resulting string * @since 1.5 */ public String replace(CharSequence target, CharSequence replacement) { ! return Pattern.compile(target.toString(), Pattern.LITERAL).matcher( ! this).replaceAll(Matcher.quoteReplacement(replacement.toString())); } /** * Splits this string around matches of the given * <a href="../util/regex/Pattern.html#sum">regular expression</a>. --- 2245,2277 ---- * @param replacement The replacement sequence of char values * @return The resulting string * @since 1.5 */ public String replace(CharSequence target, CharSequence replacement) { ! String starget = target.toString(); ! int i = 0, j = indexOf(starget); ! if (j < 0) { ! return this; ! } ! String srepl = replacement.toString(); ! int targLen = starget.length(); ! int targLen1 = Math.max(targLen, 1); ! final char[] value = this.value; ! final char[] replValue = srepl.value; ! // overflow-conscious code ! if (value.length - targLen > Integer.MAX_VALUE - replValue.length) { ! throw new OutOfMemoryError(); ! } ! int newLenHint = value.length - targLen + replValue.length; ! StringBuilder sb = new StringBuilder(newLenHint); ! do { ! sb.append(value, i, j - i) ! .append(replValue); ! i = j + targLen; ! } while (j < value.length && (j = indexOf(starget, j + targLen1)) > 0); ! ! return sb.append(value, i, value.length - i).toString(); } /** * Splits this string around matches of the given * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
< prev index next >