/** * Returns the escaped form of a given literal string. * * @param string the literal string to be escaped * @return the escaped form of {@code string} * @throws NullPointerException if {@code string} is null */ @Override public String escape(String string) { checkNotNull(string); // GWT specific check (do not optimize) // Inlineable fast-path loop which hands off to escapeSlow() only if needed int length = string.length(); for (int index = 0; index < length; index++) { if (escape(string.charAt(index)) != null) { return escapeSlow(string, index); } } return string; }
/** * 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 CharEscaper} should limit themselves * to its public interface. * * @param c the character to escape if necessary * @return the replacement string, or {@code null} if no escaping was needed */ public static String computeReplacement(CharEscaper escaper, char c) { return stringOrNull(escaper.escape(c)); }
/** * Returns the escaped form of a given literal string. * * @param string the literal string to be escaped * @return the escaped form of {@code string} * @throws NullPointerException if {@code string} is null */ @Override public String escape(String string) { checkNotNull(string); // GWT specific check (do not optimize) // Inlineable fast-path loop which hands off to escapeSlow() only if needed int length = string.length(); for (int index = 0; index < length; index++) { if (escape(string.charAt(index)) != null) { return escapeSlow(string, index); } } return string; }
/** * 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 CharEscaper} should limit themselves * to its public interface. * * @param c the character to escape if necessary * @return the replacement string, or {@code null} if no escaping was needed */ public static String computeReplacement(CharEscaper escaper, char c) { return stringOrNull(escaper.escape(c)); }
/** * Returns the escaped form of a given literal string. * * @param string the literal string to be escaped * @return the escaped form of {@code string} * @throws NullPointerException if {@code string} is null */ @Override public String escape(String string) { checkNotNull(string); // GWT specific check (do not optimize) // Inlineable fast-path loop which hands off to escapeSlow() only if needed int length = string.length(); for (int index = 0; index < length; index++) { if (escape(string.charAt(index)) != null) { return escapeSlow(string, index); } } return string; }
/** * 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 CharEscaper} should limit themselves * to its public interface. * * @param c the character to escape if necessary * @return the replacement string, or {@code null} if no escaping was needed */ public static String computeReplacement(CharEscaper escaper, char c) { return stringOrNull(escaper.escape(c)); }
char[] r = escape(s.charAt(index));
public void testXmlAttributeEscaper() throws Exception { CharEscaper xmlAttributeEscaper = (CharEscaper) XmlEscapers.xmlAttributeEscaper(); assertBasicXmlEscaper(xmlAttributeEscaper, true, true); // Test quotes are escaped. assertEquals(""test"", xmlAttributeEscaper.escape("\"test\"")); assertEquals("'test'", xmlAttributeEscaper.escape("\'test'")); // Test all escapes assertEquals( "a"b<c>d&e"f'", xmlAttributeEscaper.escape("a\"b<c>d&e\"f'")); // Test '\t', '\n' and '\r' are escaped. assertEquals("a	b
c
d", xmlAttributeEscaper.escape("a\tb\nc\rd")); }
private static String getMethodAndArgument(Tree origArg, VisitorState state) { String argSource = state.getSourceForNode(origArg); Tree arg = ASTHelpers.stripParentheses(origArg); if (arg.getKind() != Tree.Kind.STRING_LITERAL) { // Even if the regex is a constant, it still needs to be treated as a regex, since the // value comes from the symbol and/or a concatenation; the values of the subexpressions may be // changed subsequently. return String.format("onPattern(%s)", argSource); } String constValue = ASTHelpers.constValue(arg, String.class); if (constValue == null) { // Not a constant value, so we can't assume anything about pattern: have to treat it as a // regex. return String.format("onPattern(%s)", argSource); } Optional<String> regexAsLiteral = convertRegexToLiteral(constValue); if (!regexAsLiteral.isPresent()) { // Can't convert the regex to a literal string: have to treat it as a regex. return String.format("onPattern(%s)", argSource); } String escaped = SourceCodeEscapers.javaCharEscaper().escape(regexAsLiteral.get()); if (regexAsLiteral.get().length() == 1) { return String.format("on('%s')", escaped); } return String.format("on(\"%s\")", escaped); }
public void testXmlContentEscaper() throws Exception { CharEscaper xmlContentEscaper = (CharEscaper) XmlEscapers.xmlContentEscaper(); assertBasicXmlEscaper(xmlContentEscaper, false, false); // Test quotes are not escaped. assertEquals("\"test\"", xmlContentEscaper.escape("\"test\"")); assertEquals("'test'", xmlContentEscaper.escape("'test'")); }
char[] r = escape(s.charAt(index));
public void testSafeRange() throws IOException { // Basic escaping of unsafe chars (wrap them in {,}'s) CharEscaper wrappingEscaper = new ArrayBasedCharEscaper(NO_REPLACEMENTS, 'A', 'Z') { @Override protected char[] escapeUnsafe(char c) { return ("{" + c + "}").toCharArray(); } }; EscaperAsserts.assertBasic(wrappingEscaper); // '[' and '@' lie either side of [A-Z]. assertEquals("{[}FOO{@}BAR{]}", wrappingEscaper.escape("[FOO@BAR]")); }
public void testSafeRange_maxLessThanMin() throws IOException { // Basic escaping of unsafe chars (wrap them in {,}'s) CharEscaper wrappingEscaper = new ArrayBasedCharEscaper(NO_REPLACEMENTS, 'Z', 'A') { @Override protected char[] escapeUnsafe(char c) { return ("{" + c + "}").toCharArray(); } }; EscaperAsserts.assertBasic(wrappingEscaper); // escape everything. assertEquals("{[}{F}{O}{O}{]}", wrappingEscaper.escape("[FOO]")); }
public void testDeleteUnsafeChars() throws IOException { CharEscaper deletingEscaper = new ArrayBasedCharEscaper(NO_REPLACEMENTS, ' ', '~') { private final char[] noChars = new char[0]; @Override protected char[] escapeUnsafe(char c) { return noChars; } }; EscaperAsserts.assertBasic(deletingEscaper); assertEquals( "Everything outside the printable ASCII range is deleted.", deletingEscaper.escape( "\tEverything\0 outside the\uD800\uDC00 " + "printable ASCII \uFFFFrange is \u007Fdeleted.\n")); }
public void testReplacementPriority() throws IOException { CharEscaper replacingEscaper = new ArrayBasedCharEscaper(SIMPLE_REPLACEMENTS, ' ', '~') { private final char[] unknown = new char[] {'?'}; @Override protected char[] escapeUnsafe(char c) { return unknown; } }; EscaperAsserts.assertBasic(replacingEscaper); // Replacements are applied first regardless of whether the character is in // the safe range or not ('&' is a safe char while '\t' and '\n' are not). assertEquals( "<tab>Fish <and>? Chips?<newline>", replacingEscaper.escape("\tFish &\0 Chips\r\n")); } }
public void testAsUnicodeEscaper() throws IOException { CharEscaper charEscaper = createSimpleCharEscaper( ImmutableMap.<Character, char[]>builder() .put('x', "<hello>".toCharArray()) .put('\uD800', "<hi>".toCharArray()) .put('\uDC00', "<lo>".toCharArray()) .build()); UnicodeEscaper unicodeEscaper = Escapers.asUnicodeEscaper(charEscaper); EscaperAsserts.assertBasic(unicodeEscaper); assertEquals("<hello><hi><lo>", charEscaper.escape("x\uD800\uDC00")); assertEquals("<hello><hi><lo>", unicodeEscaper.escape("x\uD800\uDC00")); // Test that wrapped escapers acquire good Unicode semantics. assertEquals("<hi><hello><lo>", charEscaper.escape("\uD800x\uDC00")); try { unicodeEscaper.escape("\uD800x\uDC00"); fail("should have failed for bad Unicode input"); } catch (IllegalArgumentException e) { // pass } assertEquals("<lo><hi>", charEscaper.escape("\uDC00\uD800")); try { unicodeEscaper.escape("\uDC00\uD800"); fail("should have failed for bad Unicode input"); } catch (IllegalArgumentException e) { // pass } }
static void assertBasicXmlEscaper( CharEscaper xmlEscaper, boolean shouldEscapeQuotes, boolean shouldEscapeWhitespaceChars) { assertEquals("xxx", xmlEscaper.escape("xxx")); assertEquals("test & test & test", xmlEscaper.escape("test & test & test")); assertEquals("test << 1", xmlEscaper.escape("test << 1")); assertEquals("test >> 1", xmlEscaper.escape("test >> 1")); assertEquals("<tab>", xmlEscaper.escape("<tab>")); + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "1234567890"; assertEquals(s, xmlEscaper.escape(s)); } else { String input = String.valueOf(ch); String escaped = xmlEscaper.escape(input); assertEquals( "char 0x" + Integer.toString(ch, 16) + " should not be escaped", input, escaped); "0xFFFE is forbidden and should be replaced during escaping", "[\uFFFD]", xmlEscaper.escape("[\ufffe]")); assertEquals( "0xFFFF is forbidden and should be replaced during escaping", "[\uFFFD]", xmlEscaper.escape("[\uffff]"));