List of usage examples for com.google.common.primitives Chars ensureCapacity
public static char[] ensureCapacity(char[] array, int minLength, int padding)
From source file:com.google.template.soy.shared.internal.Sanitizers.java
/** * Called when we know we need to make a replacement. * * <p>At least one of {@code searchForEndCData} or {@code searchForEndTag} will be {@code true}. * * @param css The css string to modify/*from ww w .j ava 2 s .c om*/ * @param nextReplacement The location of the first replacement * @param searchForEndCData Whether there are any sequences of {@code ]]>} * @param searchForEndTag Whether there are any sequences of {@code </} * @return The modified string. */ private static String embedCssIntoHtmlSlow(String css, int nextReplacement, boolean searchForEndCData, boolean searchForEndTag) { // use an array instead of a stringbuilder so we can take advantage of the bulk copying // routine (String.getChars). For some reason StringBuilder doesn't do this. char[] buf = new char[css.length() + 16]; int endOfPreviousReplacement = 0; int bufIndex = 0; do { int charsToCopy = nextReplacement - endOfPreviousReplacement; buf = Chars.ensureCapacity(buf, bufIndex + charsToCopy + 4, 16); css.getChars(endOfPreviousReplacement, nextReplacement, buf, bufIndex); bufIndex += charsToCopy; char c = css.charAt(nextReplacement); if (c == ']') { buf[bufIndex++] = ']'; buf[bufIndex++] = ']'; buf[bufIndex++] = '\\'; buf[bufIndex++] = '>'; endOfPreviousReplacement = nextReplacement + 3; } else if (c == '<') { buf[bufIndex++] = '<'; buf[bufIndex++] = '\\'; buf[bufIndex++] = '/'; endOfPreviousReplacement = nextReplacement + 2; } else { throw new AssertionError(); } nextReplacement = -1; if (searchForEndTag) { int indexOfEndTag = css.indexOf("</", endOfPreviousReplacement); if (indexOfEndTag == -1) { searchForEndTag = false; } else { nextReplacement = indexOfEndTag; } } if (searchForEndCData) { int indexOfEndCData = css.indexOf("]]>", endOfPreviousReplacement); if (indexOfEndCData == -1) { searchForEndCData = false; } else { nextReplacement = nextReplacement == -1 ? indexOfEndCData : Math.min(nextReplacement, indexOfEndCData); } } } while (nextReplacement != -1); // copy tail int charsToCopy = css.length() - endOfPreviousReplacement; buf = Chars.ensureCapacity(buf, bufIndex + charsToCopy, 16); css.getChars(endOfPreviousReplacement, css.length(), buf, bufIndex); bufIndex += charsToCopy; return new String(buf, 0, bufIndex); }