[PATCH] regex matcher opt: remove redundant StringBuilder
Isaac Levy
isaac.r.levy at gmail.com
Tue Apr 24 16:11:33 UTC 2018
(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