[PATCH] regex matcher opt: remove redundant StringBuilder

Xueming Shen xueming.shen at oracle.com
Tue Apr 24 16:53:14 UTC 2018

Hi Isaac,

I actually meant to say "we are not supposed to output the partial text into
the output buffer in case of an exception". It has nothing to do with the
changeset you cited. This has been the behavior since day one/JDK1.4,
though it is not specified explicitly in the API doc. The newly added
StringBuilder variant simply follows this behavior. If it's really 
desired it
is kinda doable to save that StringBuilder without the "incompatible" 
change but just wonder if it is really worth the effort.


On 4/24/18, 9:11 AM, Isaac Levy wrote:
> (moving this to a separate discussion)
> --- a/src/java.base/share/classes/java/util/regex/Matcher.java
> +++ b/src/java.base/share/classes/java/util/regex/Matcher.java
> @@ -993,13 +993,11 @@
>      public Matcher appendReplacement(StringBuilder sb, String replacement) {
>           // If no match, return error
>           if (first<  0)
>               throw new IllegalStateException("No match available");
> -        StringBuilder result = new StringBuilder();
> -        appendExpandedReplacement(replacement, result);
>           // Append the intervening text
>           sb.append(text, lastAppendPosition, first);
>           // Append the match substitution
> +        appendExpandedReplacement(replacement, sb);
> -        sb.append(result);
> On Mon, Apr 23, 2018 at 5:05 PM Xueming Shen<xueming.shen at oracle.com>  wrote:
>> I would assume in case of an exception thrown from appendExpandedReplacement() we don't
>> want "text" to be pushed into the "sb".
>> -sherman
> Perhaps. Though the behavior under exception is undefined and this
> function is probably primarily used though the replaceAll API, which
> wouldn’t return the intermediate sb under the case of exception.
> My reading of the blame was the temp StringBuilder was an artifact of
> the api previously using StringBuffer externally.  See
> http://hg.openjdk.java.net/jdk9/dev/jdk/rev/763c564451b3

More information about the core-libs-dev mailing list