Skip to content

Commit

Permalink
Eliminate a Stream from embedCssInHtml
Browse files Browse the repository at this point in the history
Change some `indexOf(String)` to `indexOf(char)`

Saw this in a profile and it is pretty trivial to improve.

PiperOrigin-RevId: 689367425
  • Loading branch information
lukesandberg authored and copybara-github committed Oct 24, 2024
1 parent 72cde93 commit f947e71
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions java/src/com/google/template/soy/shared/internal/Sanitizers.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.google.common.flogger.StackSize.MEDIUM;
import static com.google.template.soy.shared.internal.EscapingConventions.HTML_TAG_FIRST_TOKEN;
import static java.lang.Math.min;
import static java.util.Comparator.naturalOrder;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ascii;
Expand Down Expand Up @@ -55,7 +54,6 @@
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -1494,6 +1492,12 @@ private static Escaper uriEscaper() {
private static final Escaper URI_ESCAPER_NO_PLUS =
new PercentEscaper(SAFECHARS_URLENCODER, false);

private static int nextReplacementIndex(int nextReplacementIndex, int index) {
return index == -1
? nextReplacementIndex
: nextReplacementIndex == -1 ? index : min(nextReplacementIndex, index);
}

/**
* Escapes tokens that are not allowed for interpolated style values.
*
Expand Down Expand Up @@ -1535,20 +1539,17 @@ static String embedCssIntoHtml(String css, boolean stringMode) {
// This should not affect how a CSS parser recovers from syntax errors.
int indexOfEndTag = css.indexOf("</");
int indexOfEndCData = css.indexOf("]]>");
int indexOfOpenStyleBlock = stringMode ? css.indexOf("{") : -1;
int indexOfCloseStyleBlock = stringMode ? css.indexOf("}") : -1;
int indexOfOpenStyleBlock = stringMode ? css.indexOf('{') : -1;
int indexOfCloseStyleBlock = stringMode ? css.indexOf('}') : -1;
int indexOfCommentOpenings = stringMode ? css.indexOf("/*") : -1;
boolean endsTrailingBackslash = stringMode && css.endsWith("\\");
int nextReplacementIndex =
Stream.of(
indexOfEndTag,
indexOfEndCData,
indexOfOpenStyleBlock,
indexOfCloseStyleBlock,
indexOfCommentOpenings)
.filter(i -> i != -1)
.min(naturalOrder())
.orElse(-1);
boolean endsTrailingBackslash =
stringMode && css.length() > 0 && css.charAt(css.length() - 1) == '\\';
int nextReplacementIndex = indexOfEndTag;
nextReplacementIndex = nextReplacementIndex(nextReplacementIndex, indexOfEndCData);
nextReplacementIndex = nextReplacementIndex(nextReplacementIndex, indexOfOpenStyleBlock);
nextReplacementIndex = nextReplacementIndex(nextReplacementIndex, indexOfCloseStyleBlock);
nextReplacementIndex = nextReplacementIndex(nextReplacementIndex, indexOfCommentOpenings);

if (nextReplacementIndex != -1 || endsTrailingBackslash) {
return embedCssIntoHtmlSlow(
css,
Expand Down Expand Up @@ -1641,7 +1642,7 @@ private static String embedCssIntoHtmlSlow(
}
}
if (searchForOpenStyleBlock) {
int indexOfOpenStyleBlock = css.indexOf("{", endOfPreviousReplacement);
int indexOfOpenStyleBlock = css.indexOf('{', endOfPreviousReplacement);
if (indexOfOpenStyleBlock == -1) {
searchForOpenStyleBlock = false;
} else {
Expand All @@ -1652,7 +1653,7 @@ private static String embedCssIntoHtmlSlow(
}
}
if (searchForCloseStyleBlock) {
int indexOfCloseStyleBlock = css.indexOf("}", endOfPreviousReplacement);
int indexOfCloseStyleBlock = css.indexOf('}', endOfPreviousReplacement);
if (indexOfCloseStyleBlock == -1) {
searchForCloseStyleBlock = false;
} else {
Expand All @@ -1676,7 +1677,7 @@ private static String embedCssIntoHtmlSlow(
} while (nextReplacement != -1);
// copy tail
int charsToCopy = css.length() - endOfPreviousReplacement;
buf = Chars.ensureCapacity(buf, bufIndex + charsToCopy, 16);
buf = Chars.ensureCapacity(buf, bufIndex + charsToCopy + (defuseTrailingBackslash ? 1 : 0), 0);
css.getChars(endOfPreviousReplacement, css.length(), buf, bufIndex);
bufIndex += charsToCopy;

Expand Down

0 comments on commit f947e71

Please sign in to comment.