[jOOQ/jOOQ#8205] Use JDK 9 Matcher.replaceAll(Function) instead of Matcher.replaceAll(String) where possible
This commit is contained in:
parent
a380bb1b13
commit
021384aac0
@ -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);
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user