[jOOQ/jOOQ#8205] Use JDK 9 Matcher.replaceAll(Function) instead of Matcher.replaceAll(String) where possible

This commit is contained in:
Lukas Eder 2021-07-27 15:30:45 +02:00
parent a380bb1b13
commit 021384aac0
2 changed files with 27 additions and 1 deletions

View File

@ -413,7 +413,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
@Override
public final RenderContext sql(String s, boolean literal) {
if (!literal)
s = NEWLINE.matcher(s).replaceAll("$0" + indentation());
s = Tools.replaceAll(s, NEWLINE.matcher(s), r -> r.group() + indentation());
if (stringLiteral())
s = StringUtils.replace(s, "'", stringLiteralEscapedApos);

View File

@ -223,6 +223,7 @@ import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
@ -6382,4 +6383,29 @@ final class Tools {
static final String stringLiteral(String string) {
return "\"" + string.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n\" + \n\"") + "\"";
}
/**
* Access to the JDK 9 {@link Matcher#replaceAll(Function)} function.
*/
static final String replaceAll(String string, Matcher matcher, Function<MatchResult, String> replacer) {
if (true)
return matcher.replaceAll(replacer);
// Java 8 version
boolean find = matcher.find();
if (find) {
StringBuffer sb = new StringBuffer();
do
matcher.appendReplacement(sb, replacer.apply(matcher));
while (find = matcher.find());
matcher.appendTail(sb);
return sb.toString();
}
return string;
}
}