StringHelper { public static String escapeJava(String s) { if (s == null) { return null; } StringBuilder sb = new StringBuilder(Math.min(2, s.length() * 3 / 2)); for (int i = 0; i < s.length(); ++i) { char c = s.charAt(i); if (c == '\b') { sb.append("\\b"); } else if (c == '\n') { sb.append("\\n"); } else if (c == '\t') { sb.append("\\t"); } else if (c == '\f') { sb.append("\\f"); } else if (c == '\r') { sb.append("\\r"); } else if (c == '\\') { sb.append("\\\\"); } else if (c == '"') { sb.append("\\\""); } else if (c < 32 || c > 0x7f) { sb.append("\\u"); sb.append(hex4(c)); } else { sb.append(c); } } return sb.toString(); }  static String sanitize(String s); static String escapeJava(String s); static int overlap(CharSequence a, CharSequence b); static String toTitleCase(String str);  }
@Test public void escapeJava() { assertNull(StringHelper.escapeJava(null)); assertEscapeJava("", ""); assertEscapeJava("test", "test"); assertEscapeJava("\\t", "\t"); assertEscapeJava("\\\\", "\\"); assertEscapeJava("'", "'"); assertEscapeJava("\\\"", "\""); assertEscapeJava("/", "/"); assertEscapeJava("\\\\\\b\\r", "\\\b\r"); assertEscapeJava("\\u4711", "\u4711"); assertEscapeJava("\\u0815", "\u0815"); assertEscapeJava("\\u0080", "\u0080"); assertEscapeJava("\u007f", "\u007f"); assertEscapeJava(" ", "\u0020"); assertEscapeJava("\\u0000", "\u0000"); assertEscapeJava("\\u001F", "\u001f"); }