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!")); }
/** * Returns a builder for creating simple, fast escapers. A builder instance can be reused and each * escaper that is created will be a snapshot of the current builder state. Builders are not * thread safe. * * <p>The initial state of the builder is such that: * * <ul> * <li>There are no replacement mappings * <li>{@code safeMin == Character.MIN_VALUE} * <li>{@code safeMax == Character.MAX_VALUE} * <li>{@code unsafeReplacement == null} * </ul> * * <p>For performance reasons escapers created by this builder are not Unicode aware and will not * validate the well-formedness of their input. */ public static Builder builder() { return new Builder(); }
/** * Returns a builder for creating simple, fast escapers. A builder instance can be reused and each * escaper that is created will be a snapshot of the current builder state. Builders are not * thread safe. * * <p>The initial state of the builder is such that: * * <ul> * <li>There are no replacement mappings * <li>{@code safeMin == Character.MIN_VALUE} * <li>{@code safeMax == Character.MAX_VALUE} * <li>{@code unsafeReplacement == null} * </ul> * * <p>For performance reasons escapers created by this builder are not Unicode aware and will not * validate the well-formedness of their input. */ public static Builder builder() { return new Builder(); }
/** * Returns a builder for creating simple, fast escapers. A builder instance * can be reused and each escaper that is created will be a snapshot of the * current builder state. Builders are not thread safe. * * <p>The initial state of the builder is such that: * <ul> * <li>There are no replacement mappings<li> * <li>{@code safeMin == Character.MIN_VALUE}</li> * <li>{@code safeMax == Character.MAX_VALUE}</li> * <li>{@code unsafeReplacement == null}</li> * </ul> * <p>For performance reasons escapers created by this builder are not * Unicode aware and will not validate the well-formedness of their input. */ public static Builder builder() { return new Builder(); }
/** * Returns a builder for creating simple, fast escapers. A builder instance * can be reused and each escaper that is created will be a snapshot of the * current builder state. Builders are not thread safe. * * <p>The initial state of the builder is such that: * <ul> * <li>There are no replacement mappings<li> * <li>{@code safeMin == Character.MIN_VALUE}</li> * <li>{@code safeMax == Character.MAX_VALUE}</li> * <li>{@code unsafeReplacement == null}</li> * </ul> * <p>For performance reasons escapers created by this builder are not * Unicode aware and will not validate the well-formedness of their input. */ public static Builder builder() { return new Builder(); }
public void dumpGraph(PrintWriter writer) { Escaper escaper = Escapers.builder() .addEscape('"', "\\\"") .addEscape('\n', "\\n") .build(); writer.println("digraph {"); writer.println(String.format("%s[label=\"Entry\"]", entryNode.id)); writer.println(String.format("%s[label=\"Exit\"]", exitNode.id)); for (Node node : nodes.values()) { writer.println(String.format("%s[label=\"%s\", shape=\"rect\"]", node.id, escaper.escape(node.basicBlock.toString()))); } for (Node node : getNodes()) { for (Node out : node.outgoing) { writer.println(node.id + " -> " + out.id); } } writer.println("}"); } }
/** * Returns a builder for creating simple, fast escapers. A builder instance * can be reused and each escaper that is created will be a snapshot of the * current builder state. Builders are not thread safe. * * <p>The initial state of the builder is such that: * <ul> * <li>There are no replacement mappings<li> * <li>{@code safeMin == Character.MIN_VALUE}</li> * <li>{@code safeMax == Character.MAX_VALUE}</li> * <li>{@code unsafeReplacement == null}</li> * </ul> * <p>For performance reasons escapers created by this builder are not * Unicode aware and will not validate the well-formedness of their input. */ public static Builder builder() { return new Builder(); }
@Override public String generate(Request request, int status, String reasonPhrase, String description, Throwable exception) { return "{\"error\": [" + status + ", \"" + reasonPhrase + "\", \"" + description + "\", \"" + Escapers.builder() .addEscape('\"', "\\\"") .addEscape('\n', "\\n") .build() .escape(Throwables.getStackTraceAsString(exception)) + "\"] }"; } };
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!")); }
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!")); }
/** * Returns a builder for creating simple, fast escapers. A builder instance * can be reused and each escaper that is created will be a snapshot of the * current builder state. Builders are not thread safe. * * <p>The initial state of the builder is such that: * <ul> * <li>There are no replacement mappings<li> * <li>{@code safeMin == Character.MIN_VALUE}</li> * <li>{@code safeMax == Character.MAX_VALUE}</li> * <li>{@code unsafeReplacement == null}</li> * </ul> * <p>For performance reasons escapers created by this builder are not * Unicode aware and will not validate the well-formedness of their input. */ public static Builder builder() { return new Builder(); }
/** * Returns a builder for creating simple, fast escapers. A builder instance * can be reused and each escaper that is created will be a snapshot of the * current builder state. Builders are not thread safe. * * <p>The initial state of the builder is such that: * <ul> * <li>There are no replacement mappings<li> * <li>{@code safeMin == Character.MIN_VALUE}</li> * <li>{@code safeMax == Character.MAX_VALUE}</li> * <li>{@code unsafeReplacement == null}</li> * </ul> * <p>For performance reasons escapers created by this builder are not * Unicode aware and will not validate the well-formedness of their input. */ public static Builder builder() { return new Builder(); }
/** * Returns a builder for creating simple, fast escapers. A builder instance * can be reused and each escaper that is created will be a snapshot of the * current builder state. Builders are not thread safe. * * <p>The initial state of the builder is such that: * <ul> * <li>There are no replacement mappings<li> * <li>{@code safeMin == Character.MIN_VALUE}</li> * <li>{@code safeMax == Character.MAX_VALUE}</li> * <li>{@code unsafeReplacement == null}</li> * </ul> * <p>For performance reasons escapers created by this builder are not * Unicode aware and will not validate the well-formedness of their input. */ public static Builder builder() { return new Builder(); }
/** * Returns a builder for creating simple, fast escapers. A builder instance * can be reused and each escaper that is created will be a snapshot of the * current builder state. Builders are not thread safe. * * <p>The initial state of the builder is such that: * <ul> * <li>There are no replacement mappings<li> * <li>{@code safeMin == Character.MIN_VALUE}</li> * <li>{@code safeMax == Character.MAX_VALUE}</li> * <li>{@code unsafeReplacement == null}</li> * </ul> * <p>For performance reasons escapers created by this builder are not * Unicode aware and will not validate the well-formedness of their input. */ public static Builder builder() { return new Builder(); }
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 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")); }
private String displayString(String value) { if (maxCharDisplay > 0 && value.length() > maxCharDisplay) { value = value.substring(0, maxCharDisplay) + "... (" + value.length() + " chars)"; } String escaped = Escapers.builder() .setSafeRange(' ', '~') .addEscape('"', "\\\"") .addEscape('\\', "\\\\") .build() .escape(value); return '"' + escaped + '"'; }