Skip to content

Commit

Permalink
make lookaround() more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
fluentfuture committed Nov 7, 2024
1 parent b264e51 commit 7109f4d
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions mug-guava/src/main/java/com/google/mu/safesql/SafeSql.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,18 @@ private boolean lookaround(
String leftPattern, Substring.Match placeholder, String rightPattern) {
ImmutableList<String> lookbehind = TOKENS.from(leftPattern).collect(toImmutableList());
ImmutableList<String> lookahead = TOKENS.from(rightPattern).collect(toImmutableList());
ImmutableList<Substring.Match> leftTokens = allTokens.subList(
0, charIndexToTokenIndex.get(placeholder.index()));
ImmutableList<Substring.Match> rightTokens = allTokens.subList(
charIndexToTokenIndex.get(placeholder.index() + placeholder.length() - 1) + 1,
allTokens.size());
return BiStream.zip(lookbehind.reverse(), leftTokens.reverse()) // right-to-left
int closingBraceIndex = placeholder.index() + placeholder.length() - 1;
int nextTokenIndex = charIndexToTokenIndex.get(closingBraceIndex) + 1;
ImmutableList<Substring.Match> leftTokens =
allTokens.subList(0, charIndexToTokenIndex.get(placeholder.index()));
ImmutableList<Substring.Match> rightTokens =
allTokens.subList(nextTokenIndex, allTokens.size());
return BiStream.zip(lookahead, rightTokens)
.filter((s, t) -> s.equalsIgnoreCase(t.toString()))
.count() == lookbehind.size()
&& BiStream.zip(lookahead, rightTokens)
.count() == lookahead.size()
&& BiStream.zip(lookbehind.reverse(), leftTokens.reverse()) // right-to-left
.filter((s, t) -> s.equalsIgnoreCase(t.toString()))
.count() == lookahead.size();
.count() == lookbehind.size();
}
}
placeholders.forEachOrdered(new SqlWriter()::writePlaceholder);
Expand Down

0 comments on commit 7109f4d

Please sign in to comment.