/** * Convert this builder into a char escaper which is just a decorator around the underlying array * of replacement char[]s. * * @return an escaper that escapes based on the underlying array. */ public Escaper toEscaper() { return new CharArrayDecorator(toArray()); } }
/** * 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 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 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!")); }
@Override public String apply(String from) { return escape(from); } };
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")); }
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")); }
public void testAddEscapes() { char[] cs = {'a', 'b', 'c'}; CharEscaperBuilder builder = new CharEscaperBuilder().addEscapes(cs, "Z"); Escaper escaper = builder.toEscaper(); assertEquals("ZZZdef", escaper.escape("abcdef")); } }
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)); }
public void testMapLength() { Map<Character, String> map = ImmutableMap.of( 'a', "first", 'z', "last"); ArrayBasedEscaperMap fem = ArrayBasedEscaperMap.create(map); // Array length is highest character value + 1 assertEquals('z' + 1, fem.getReplacementArray().length); }
/** * Returns a new ArrayBasedEscaperMap for creating ArrayBasedCharEscaper or * ArrayBasedUnicodeEscaper instances. * * @param replacements a map of characters to their escaped representations */ public static ArrayBasedEscaperMap create(Map<Character, String> replacements) { return new ArrayBasedEscaperMap(createReplacementArray(replacements)); }
public void testCodePointAt_IndexOutOfBoundsException() { try { UnicodeEscaper.codePointAt("Testing...", 4, 2); fail(); } catch (IndexOutOfBoundsException expected) { } }
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())); }
public void testSimpleEscaper() { UnicodeEscaper e = SIMPLE_ESCAPER; String expected = "[0]abyz[128][256][2048][4096]ABYZ[65535]" + "[" + Character.MIN_SUPPLEMENTARY_CODE_POINT + "]" + "0189[" + Character.MAX_CODE_POINT + "]"; assertEquals(expected, escapeAsString(e, TEST_STRING)); }
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!")); }
private static String escapeAsString(Escaper e, String s) { return e.escape(s); } }
public void testEmptyMap() { Map<Character, String> map = ImmutableMap.of(); ArrayBasedEscaperMap fem = ArrayBasedEscaperMap.create(map); // Non-null array of zero length. assertEquals(0, fem.getReplacementArray().length); }
public void testNopEscaper() { UnicodeEscaper e = NOP_ESCAPER; assertEquals(TEST_STRING, escapeAsString(e, TEST_STRING)); }