int cp = codePointAt(s, index, end); if (cp < 0) { throw new IllegalArgumentException("Trailing high surrogate at end of input"); char[] escaped = escape(cp); int nextIndex = index + (Character.isSupplementaryCodePoint(cp) ? 2 : 1); if (escaped != null) { if (dest.length < sizeNeeded) { int destLength = sizeNeeded + (end - index) + DEST_PAD; dest = growBuffer(dest, destIndex, destLength); index = nextEscapeIndex(s, nextIndex, end); int endIndex = destIndex + charsSkipped; if (dest.length < endIndex) { dest = growBuffer(dest, destIndex, endIndex);
/** * Returns the escaped form of a given literal string. * * <p>If you are escaping input in arbitrary successive chunks, then it is not generally safe to * use this method. If an input string ends with an unmatched high surrogate character, then this * method will throw {@link IllegalArgumentException}. You should ensure your input is valid <a * href="http://en.wikipedia.org/wiki/UTF-16">UTF-16</a> before calling this method. * * <p><b>Note:</b> When implementing an escaper it is a good idea to override this method for * efficiency by inlining the implementation of {@link #nextEscapeIndex(CharSequence, int, int)} * directly. Doing this for {@link com.google.common.net.PercentEscaper} more than doubled the * performance for unescaped strings (as measured by {@link CharEscapersBenchmark}). * * @param string the literal string to be escaped * @return the escaped form of {@code string} * @throws NullPointerException if {@code string} is null * @throws IllegalArgumentException if invalid surrogate characters are encountered */ @Override public String escape(String string) { checkNotNull(string); int end = string.length(); int index = nextEscapeIndex(string, 0, end); return index == end ? string : escapeSlow(string, index); }
int index = start; while (index < end) { int cp = codePointAt(csq, index, end); if (cp < 0 || escape(cp) != null) { break;
/** * Returns a string that would replace the given character in the specified escaper, or {@code * null} if no replacement should be made. This method is intended for use in tests through the * {@code EscaperAsserts} class; production users of {@link UnicodeEscaper} should limit * themselves to its public interface. * * @param cp the Unicode code point to escape if necessary * @return the replacement string, or {@code null} if no escaping was needed */ public static String computeReplacement(UnicodeEscaper escaper, int cp) { return stringOrNull(escaper.escape(cp)); }
public void testCodePointAt_IndexOutOfBoundsException() { try { UnicodeEscaper.codePointAt("Testing...", 4, 2); fail(); } catch (IndexOutOfBoundsException expected) { } }
int index = start; while (index < end) { int cp = codePointAt(csq, index, end); if (cp < 0 || escape(cp) != null) { break;
/** * Returns a string that would replace the given character in the specified escaper, or {@code * null} if no replacement should be made. This method is intended for use in tests through the * {@code EscaperAsserts} class; production users of {@link UnicodeEscaper} should limit * themselves to its public interface. * * @param cp the Unicode code point to escape if necessary * @return the replacement string, or {@code null} if no escaping was needed */ public static String computeReplacement(UnicodeEscaper escaper, int cp) { return stringOrNull(escaper.escape(cp)); }
public void testCodePointAt_IndexOutOfBoundsException() { try { UnicodeEscaper.codePointAt("Testing...", 4, 2); fail(); } catch (IndexOutOfBoundsException expected) { } }
int cp = codePointAt(s, index, end); if (cp < 0) { throw new IllegalArgumentException("Trailing high surrogate at end of input"); char[] escaped = escape(cp); int nextIndex = index + (Character.isSupplementaryCodePoint(cp) ? 2 : 1); if (escaped != null) { if (dest.length < sizeNeeded) { int destLength = sizeNeeded + (end - index) + DEST_PAD; dest = growBuffer(dest, destIndex, destLength); index = nextEscapeIndex(s, nextIndex, end); int endIndex = destIndex + charsSkipped; if (dest.length < endIndex) { dest = growBuffer(dest, destIndex, endIndex);
int index = start; while (index < end) { int cp = codePointAt(csq, index, end); if (cp < 0 || escape(cp) != null) { break;
/** * Returns a string that would replace the given character in the specified escaper, or {@code * null} if no replacement should be made. This method is intended for use in tests through the * {@code EscaperAsserts} class; production users of {@link UnicodeEscaper} should limit * themselves to its public interface. * * @param cp the Unicode code point to escape if necessary * @return the replacement string, or {@code null} if no escaping was needed */ public static String computeReplacement(UnicodeEscaper escaper, int cp) { return stringOrNull(escaper.escape(cp)); }
/** * Returns the escaped form of a given literal string. * * <p>If you are escaping input in arbitrary successive chunks, then it is not generally safe to * use this method. If an input string ends with an unmatched high surrogate character, then this * method will throw {@link IllegalArgumentException}. You should ensure your input is valid <a * href="http://en.wikipedia.org/wiki/UTF-16">UTF-16</a> before calling this method. * * <p><b>Note:</b> When implementing an escaper it is a good idea to override this method for * efficiency by inlining the implementation of {@link #nextEscapeIndex(CharSequence, int, int)} * directly. Doing this for {@link com.google.common.net.PercentEscaper} more than doubled the * performance for unescaped strings (as measured by {@link CharEscapersBenchmark}). * * @param string the literal string to be escaped * @return the escaped form of {@code string} * @throws NullPointerException if {@code string} is null * @throws IllegalArgumentException if invalid surrogate characters are encountered */ @Override public String escape(String string) { checkNotNull(string); int end = string.length(); int index = nextEscapeIndex(string, 0, end); return index == end ? string : escapeSlow(string, index); }
int cp = codePointAt(s, index, end); if (cp < 0) { throw new IllegalArgumentException("Trailing high surrogate at end of input"); char[] escaped = escape(cp); int nextIndex = index + (Character.isSupplementaryCodePoint(cp) ? 2 : 1); if (escaped != null) { if (dest.length < sizeNeeded) { int destLength = sizeNeeded + (end - index) + DEST_PAD; dest = growBuffer(dest, destIndex, destLength); index = nextEscapeIndex(s, nextIndex, end); int endIndex = destIndex + charsSkipped; if (dest.length < endIndex) { dest = growBuffer(dest, destIndex, endIndex);
int index = start; while (index < end) { int cp = codePointAt(csq, index, end); if (cp < 0 || escape(cp) != null) { break;
public void testGrowBuffer() { // need to grow past an initial 1024 byte buffer StringBuilder input = new StringBuilder(); StringBuilder expected = new StringBuilder(); for (int i = 256; i < 1024; i++) { input.append((char) i); expected.append("[" + i + "]"); } assertEquals(expected.toString(), SIMPLE_ESCAPER.escape(input.toString())); }
/** * Returns the escaped form of a given literal string. * * <p>If you are escaping input in arbitrary successive chunks, then it is not generally safe to * use this method. If an input string ends with an unmatched high surrogate character, then this * method will throw {@link IllegalArgumentException}. You should ensure your input is valid <a * href="http://en.wikipedia.org/wiki/UTF-16">UTF-16</a> before calling this method. * * <p><b>Note:</b> When implementing an escaper it is a good idea to override this method for * efficiency by inlining the implementation of {@link #nextEscapeIndex(CharSequence, int, int)} * directly. Doing this for {@link com.google.common.net.PercentEscaper} more than doubled the * performance for unescaped strings (as measured by {@link CharEscapersBenchmark}). * * @param string the literal string to be escaped * @return the escaped form of {@code string} * @throws NullPointerException if {@code string} is null * @throws IllegalArgumentException if invalid surrogate characters are encountered */ @Override public String escape(String string) { checkNotNull(string); int end = string.length(); int index = nextEscapeIndex(string, 0, end); return index == end ? string : escapeSlow(string, index); }
int cp = codePointAt(s, index, end); if (cp < 0) { throw new IllegalArgumentException("Trailing high surrogate at end of input"); char[] escaped = escape(cp); int nextIndex = index + (Character.isSupplementaryCodePoint(cp) ? 2 : 1); if (escaped != null) { if (dest.length < sizeNeeded) { int destLength = sizeNeeded + (end - index) + DEST_PAD; dest = growBuffer(dest, destIndex, destLength); index = nextEscapeIndex(s, nextIndex, end); int endIndex = destIndex + charsSkipped; if (dest.length < endIndex) { dest = growBuffer(dest, destIndex, endIndex);
int index = start; while (index < end) { int cp = codePointAt(csq, index, end); if (cp < 0 || escape(cp) != null) { break;
public void testNullInput() { UnicodeEscaper e = SIMPLE_ESCAPER; try { e.escape((String) null); fail("Null string should cause exception"); } catch (NullPointerException expected) { // Pass } }
/** * Returns the escaped form of a given literal string. * * <p>If you are escaping input in arbitrary successive chunks, then it is not generally safe to * use this method. If an input string ends with an unmatched high surrogate character, then this * method will throw {@link IllegalArgumentException}. You should ensure your input is valid <a * href="http://en.wikipedia.org/wiki/UTF-16">UTF-16</a> before calling this method. * * <p><b>Note:</b> When implementing an escaper it is a good idea to override this method for * efficiency by inlining the implementation of {@link #nextEscapeIndex(CharSequence, int, int)} * directly. Doing this for {@link com.google.common.net.PercentEscaper} more than doubled the * performance for unescaped strings (as measured by {@link CharEscapersBenchmark}). * * @param string the literal string to be escaped * @return the escaped form of {@code string} * @throws NullPointerException if {@code string} is null * @throws IllegalArgumentException if invalid surrogate characters are encountered */ @Override public String escape(String string) { checkNotNull(string); int end = string.length(); int index = nextEscapeIndex(string, 0, end); return index == end ? string : escapeSlow(string, index); }