/** * 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 a {@link UnicodeEscaper} equivalent to the given escaper instance. If the escaper is * already a UnicodeEscaper then it is simply returned, otherwise it is wrapped in a * UnicodeEscaper. * * <p>When a {@link CharEscaper} escaper is wrapped by this method it acquires extra behavior with * respect to the well-formedness of Unicode character sequences and will throw {@link * IllegalArgumentException} when given bad input. * * @param escaper the instance to be wrapped * @return a UnicodeEscaper with the same behavior as the given instance * @throws NullPointerException if escaper is null * @throws IllegalArgumentException if escaper is not a UnicodeEscaper or a CharEscaper */ static UnicodeEscaper asUnicodeEscaper(Escaper escaper) { checkNotNull(escaper); if (escaper instanceof UnicodeEscaper) { return (UnicodeEscaper) escaper; } else if (escaper instanceof CharEscaper) { return wrap((CharEscaper) escaper); } // In practice this shouldn't happen because it would be very odd not to // extend either CharEscaper or UnicodeEscaper for non trivial cases. throw new IllegalArgumentException( "Cannot create a UnicodeEscaper from: " + escaper.getClass().getName()); }
public void testBuilderInitialStateNoneUnsafe() { // No characters are unsafe by default (safeMin == 0, safeMax == 0xFFFF). Escaper escaper = Escapers.builder().setUnsafeReplacement("X").build(); assertEquals("\0\uFFFF", escaper.escape("\0\uFFFF")); }
/** * Asserts that a Unicode escaper does not escape the given character. * * @param escaper the non-null escaper to test * @param cp the Unicode code point to test */ public static void assertUnescaped(UnicodeEscaper escaper, int cp) { Assert.assertNull(computeReplacement(escaper, cp)); }
public void testNullEscaper() throws IOException { Escaper escaper = Escapers.nullEscaper(); EscaperAsserts.assertBasic(escaper); String s = "\0\n\t\\az09~\uD800\uDC00\uFFFF"; assertEquals("null escaper should have no effect", s, escaper.escape(s)); }
/** * Asserts that an escaper does not escape the given character. * * @param escaper the non-null escaper to test * @param c the character to test */ public static void assertUnescaped(CharEscaper escaper, char c) { Assert.assertNull(computeReplacement(escaper, c)); }
private static Escaper newEscaper(String unwiseChars) { if (isNullOrEmpty(unwiseChars)) { return nullEscaper(); } Iterable<String> tokens = Splitter.on(",").omitEmptyStrings().split(unwiseChars); CharEscaperBuilder builder = new CharEscaperBuilder(); for (String token : tokens) { char c = token.charAt(0); builder.addEscape(c, "%" + toHexString(c).toUpperCase()); } return builder.toEscaper(); } }
/** * 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 a {@link UnicodeEscaper} equivalent to the given escaper instance. If the escaper is * already a UnicodeEscaper then it is simply returned, otherwise it is wrapped in a * UnicodeEscaper. * * <p>When a {@link CharEscaper} escaper is wrapped by this method it acquires extra behavior with * respect to the well-formedness of Unicode character sequences and will throw {@link * IllegalArgumentException} when given bad input. * * @param escaper the instance to be wrapped * @return a UnicodeEscaper with the same behavior as the given instance * @throws NullPointerException if escaper is null * @throws IllegalArgumentException if escaper is not a UnicodeEscaper or a CharEscaper */ static UnicodeEscaper asUnicodeEscaper(Escaper escaper) { checkNotNull(escaper); if (escaper instanceof UnicodeEscaper) { return (UnicodeEscaper) escaper; } else if (escaper instanceof CharEscaper) { return wrap((CharEscaper) escaper); } // In practice this shouldn't happen because it would be very odd not to // extend either CharEscaper or UnicodeEscaper for non trivial cases. throw new IllegalArgumentException( "Cannot create a UnicodeEscaper from: " + escaper.getClass().getName()); }
public void testBuilderInitialStateNoReplacement() { // Unsafe characters aren't modified by default (unsafeReplacement == null). Escaper escaper = Escapers.builder().setSafeRange('a', 'z').build(); assertEquals("The Quick Brown Fox", escaper.escape("The Quick Brown Fox")); }
/** * Asserts that an escaper escapes the given character into the expected string. * * @param escaper the non-null escaper to test * @param expected the expected output string * @param c the character to escape */ public static void assertEscaping(CharEscaper escaper, String expected, char c) { String escaped = computeReplacement(escaper, c); Assert.assertNotNull(escaped); Assert.assertEquals(expected, escaped); }
public void testNullEscaper() throws IOException { Escaper escaper = Escapers.nullEscaper(); EscaperAsserts.assertBasic(escaper); String s = "\0\n\t\\az09~\uD800\uDC00\uFFFF"; assertEquals("null escaper should have no effect", s, escaper.escape(s)); }
/** * 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 a {@link UnicodeEscaper} equivalent to the given escaper instance. If the escaper is * already a UnicodeEscaper then it is simply returned, otherwise it is wrapped in a * UnicodeEscaper. * * <p>When a {@link CharEscaper} escaper is wrapped by this method it acquires extra behavior with * respect to the well-formedness of Unicode character sequences and will throw {@link * IllegalArgumentException} when given bad input. * * @param escaper the instance to be wrapped * @return a UnicodeEscaper with the same behavior as the given instance * @throws NullPointerException if escaper is null * @throws IllegalArgumentException if escaper is not a UnicodeEscaper or a CharEscaper */ static UnicodeEscaper asUnicodeEscaper(Escaper escaper) { checkNotNull(escaper); if (escaper instanceof UnicodeEscaper) { return (UnicodeEscaper) escaper; } else if (escaper instanceof CharEscaper) { return wrap((CharEscaper) escaper); } // In practice this shouldn't happen because it would be very odd not to // extend either CharEscaper or UnicodeEscaper for non trivial cases. throw new IllegalArgumentException( "Cannot create a UnicodeEscaper from: " + escaper.getClass().getName()); }
public void testBuilderRetainsState() { // Setting a safe range and unsafe replacement works as expected. Escapers.Builder builder = Escapers.builder(); builder.setSafeRange('a', 'z'); builder.setUnsafeReplacement("X"); assertEquals("XheXXuickXXrownXXoxX", builder.build().escape("The Quick Brown Fox!")); // Explicit replacements take priority over unsafe characters. builder.addEscape(' ', "_"); builder.addEscape('!', "_"); assertEquals("Xhe_Xuick_Xrown_Xox_", builder.build().escape("The Quick Brown Fox!")); // Explicit replacements take priority over safe characters. builder.setSafeRange(' ', '~'); assertEquals("The_Quick_Brown_Fox_", builder.build().escape("The Quick Brown Fox!")); }
/** * Asserts that a Unicode escaper escapes the given code point into the expected string. * * @param escaper the non-null escaper to test * @param expected the expected output string * @param cp the Unicode code point to escape */ public static void assertEscaping(UnicodeEscaper escaper, String expected, int cp) { String escaped = computeReplacement(escaper, cp); Assert.assertNotNull(escaped); Assert.assertEquals(expected, escaped); }
/** * 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 a {@link UnicodeEscaper} equivalent to the given escaper instance. If the escaper is * already a UnicodeEscaper then it is simply returned, otherwise it is wrapped in a * UnicodeEscaper. * * <p>When a {@link CharEscaper} escaper is wrapped by this method it acquires extra behavior with * respect to the well-formedness of Unicode character sequences and will throw {@link * IllegalArgumentException} when given bad input. * * @param escaper the instance to be wrapped * @return a UnicodeEscaper with the same behavior as the given instance * @throws NullPointerException if escaper is null * @throws IllegalArgumentException if escaper is not a UnicodeEscaper or a CharEscaper */ static UnicodeEscaper asUnicodeEscaper(Escaper escaper) { checkNotNull(escaper); if (escaper instanceof UnicodeEscaper) { return (UnicodeEscaper) escaper; } else if (escaper instanceof CharEscaper) { return wrap((CharEscaper) escaper); } // In practice this shouldn't happen because it would be very odd not to // extend either CharEscaper or UnicodeEscaper for non trivial cases. throw new IllegalArgumentException( "Cannot create a UnicodeEscaper from: " + escaper.getClass().getName()); }
public void testBuilderCreatesIndependentEscapers() { // Setup a simple builder and create the first escaper. Escapers.Builder builder = Escapers.builder(); builder.setSafeRange('a', 'z'); builder.setUnsafeReplacement("X"); builder.addEscape(' ', "_"); Escaper first = builder.build(); // Modify one of the existing mappings before creating a new escaper. builder.addEscape(' ', "-"); builder.addEscape('!', "$"); Escaper second = builder.build(); // This should have no effect on existing escapers. builder.addEscape(' ', "*"); // Test both escapers after modifying the builder. assertEquals("Xhe_Xuick_Xrown_XoxX", first.escape("The Quick Brown Fox!")); assertEquals("Xhe-Xuick-Xrown-Xox$", second.escape("The Quick Brown Fox!")); }
/** * Asserts that a Unicode escaper escapes the given hi/lo surrogate pair into the expected string. * * @param escaper the non-null escaper to test * @param expected the expected output string * @param hi the high surrogate pair character * @param lo the low surrogate pair character */ public static void assertUnicodeEscaping( UnicodeEscaper escaper, String expected, char hi, char lo) { int cp = Character.toCodePoint(hi, lo); String escaped = computeReplacement(escaper, cp); Assert.assertNotNull(escaped); Assert.assertEquals(expected, escaped); } }