@Test public void testAppendChar() { StringBuilder sb = new StringBuilder(); Assert.assertEquals(sb, appendChar(sb, 'x')); Assert.assertEquals(sb, appendChar(sb, '\t')); Assert.assertEquals(sb, appendChar(sb, '\n')); Assert.assertEquals(sb, appendChar(sb, (char) 0x7f)); Assert.assertEquals("x\\t\\n\\d", sb.toString()); }
public static StringBuilder appendChar(StringBuilder sb, char ch) { if (isCharEscaped(ch)) { switch (ch) { case '\\': return sb.append("\\\\"); case '\b': return sb.append("\\b"); case '\f': return sb.append("\\f"); case '\n': return sb.append("\\n"); case '\r': return sb.append("\\r"); case '\t': return sb.append("\\t"); case '\'': return sb.append("\\\'"); case '\"': return sb.append("\\\""); case 0x7F: return sb.append("\\d"); default: return appendIntAsHex(sb.append('\\').append('u'), ch, 4); } } else { return sb.append(ch); } }
Handy { public static StringBuilder appendChar(StringBuilder sb, char ch) { if (isCharEscaped(ch)) { switch (ch) { case '\\': return sb.append("\\\\"); case '\b': return sb.append("\\b"); case '\f': return sb.append("\\f"); case '\n': return sb.append("\\n"); case '\r': return sb.append("\\r"); case '\t': return sb.append("\\t"); case '\'': return sb.append("\\\'"); case '\"': return sb.append("\\\""); case 0x7F: return sb.append("\\d"); default: return appendIntAsHex(sb.append('\\').append('u'), ch, 4); } } else { return sb.append(ch); } } }
Handy { public static StringBuilder appendChar(StringBuilder sb, char ch) { if (isCharEscaped(ch)) { switch (ch) { case '\\': return sb.append("\\\\"); case '\b': return sb.append("\\b"); case '\f': return sb.append("\\f"); case '\n': return sb.append("\\n"); case '\r': return sb.append("\\r"); case '\t': return sb.append("\\t"); case '\'': return sb.append("\\\'"); case '\"': return sb.append("\\\""); case 0x7F: return sb.append("\\d"); default: return appendIntAsHex(sb.append('\\').append('u'), ch, 4); } } else { return sb.append(ch); } }  }
Handy { public static StringBuilder appendChar(StringBuilder sb, char ch) { if (isCharEscaped(ch)) { switch (ch) { case '\\': return sb.append("\\\\"); case '\b': return sb.append("\\b"); case '\f': return sb.append("\\f"); case '\n': return sb.append("\\n"); case '\r': return sb.append("\\r"); case '\t': return sb.append("\\t"); case '\'': return sb.append("\\\'"); case '\"': return sb.append("\\\""); case 0x7F: return sb.append("\\d"); default: return appendIntAsHex(sb.append('\\').append('u'), ch, 4); } } else { return sb.append(ch); } }  static char nibbleToChar(int n); static StringBuilder appendByteAsHex(StringBuilder sb, int n); static String byteToHexString(int n); static StringBuilder appendByteArrayAsHex(StringBuilder sb, byte[] ab, int of, int cb); static StringBuilder appendByteArrayAsHex(StringBuilder sb, byte[] ab); static String byteArrayToHexString(byte[] ab, int of, int cb); static String byteArrayToHexString(byte[] ab); static byte[] hexStringToByteArray(String s); static String byteArrayToHexDump(byte[] ab, int of, int cb, int cBytesPerLine); static String byteArrayToHexDump(byte[] ab, int cBytesPerLine); static int countHexDigits(int n); static StringBuilder appendIntAsHex(StringBuilder sb, int n, int cch); static StringBuilder appendIntAsHex(StringBuilder sb, int n); static String intToHexString(int n); static int countHexDigits(long n); static StringBuilder appendLongAsHex(StringBuilder sb, long n, int cch); static StringBuilder appendLongAsHex(StringBuilder sb, long n); static String longToHexString(long n); static int renderByteToHex(int n, char[] ach, int of); static int renderIntToHex(int n, char[] ach, int of, int cch); static int renderLongToHex(long n, char[] ach, int of, int cch); static boolean isDigit(char ch); static int digitValue(char ch); static boolean isHexit(char ch); static int hexitValue(char ch); static boolean isAsciiLetter(char ch); static int countChar(String s, char ch); static String[] parseDelimitedString(String s, char chDelim); static String indentLines(String sText, String sIndent); static boolean isCharEscaped(char ch); static StringBuilder appendChar(StringBuilder sb, char ch); static String quotedChar(char ch); static StringBuilder appendString(StringBuilder sb, String s); static String quotedString(String s); static long readPackedLong(DataInput in); static void writePackedLong(DataOutput out, long n); static int readPackedInt(DataInput in); static int readMagnitude(DataInput in); static int readIndex(DataInput in); static int readUtf8Char(DataInput in); static void writeUtf8Char(DataOutput out, int ch); static String readUtf8String(DataInput in); static void writeUtf8String(DataOutput out, String s); static InputStream toInputStream(File file); static byte[] readFileBytes(File file); static char[] readFileChars(File file); static char[] readFileChars(File file, String sEncoding); static T[] dedupAdds(T[] aoBase, T[] aoAdd); static T[] append(T[] aoBase, T[] aoAdd); static T[] appendHead(T[] aoBase, T oAdd); static ArrayList<T> startList(T[] ao, int c); static ArrayList<T> appendList(ArrayList<T> list, T[] ao, int of, int c); static boolean anyMatches(T[] ao, T o); static int hashCode(final Object o); static boolean equals(final Object o1, final Object o2); static int compareObjects(final Comparable o1, final Comparable o2); static boolean equalArraysNullOk(final Object[] ao1, final Object[] ao2); static int compareArrays(final Comparable[] ao1, final Comparable[] ao2); static boolean checkElementsNonNull(Object[] ao); }
Handy { public static StringBuilder appendChar(StringBuilder sb, char ch) { if (isCharEscaped(ch)) { switch (ch) { case '\\': return sb.append("\\\\"); case '\b': return sb.append("\\b"); case '\f': return sb.append("\\f"); case '\n': return sb.append("\\n"); case '\r': return sb.append("\\r"); case '\t': return sb.append("\\t"); case '\'': return sb.append("\\\'"); case '\"': return sb.append("\\\""); case 0x7F: return sb.append("\\d"); default: return appendIntAsHex(sb.append('\\').append('u'), ch, 4); } } else { return sb.append(ch); } }  static char nibbleToChar(int n); static StringBuilder appendByteAsHex(StringBuilder sb, int n); static String byteToHexString(int n); static StringBuilder appendByteArrayAsHex(StringBuilder sb, byte[] ab, int of, int cb); static StringBuilder appendByteArrayAsHex(StringBuilder sb, byte[] ab); static String byteArrayToHexString(byte[] ab, int of, int cb); static String byteArrayToHexString(byte[] ab); static byte[] hexStringToByteArray(String s); static String byteArrayToHexDump(byte[] ab, int of, int cb, int cBytesPerLine); static String byteArrayToHexDump(byte[] ab, int cBytesPerLine); static int countHexDigits(int n); static StringBuilder appendIntAsHex(StringBuilder sb, int n, int cch); static StringBuilder appendIntAsHex(StringBuilder sb, int n); static String intToHexString(int n); static int countHexDigits(long n); static StringBuilder appendLongAsHex(StringBuilder sb, long n, int cch); static StringBuilder appendLongAsHex(StringBuilder sb, long n); static String longToHexString(long n); static int renderByteToHex(int n, char[] ach, int of); static int renderIntToHex(int n, char[] ach, int of, int cch); static int renderLongToHex(long n, char[] ach, int of, int cch); static boolean isDigit(char ch); static int digitValue(char ch); static boolean isHexit(char ch); static int hexitValue(char ch); static boolean isAsciiLetter(char ch); static int countChar(String s, char ch); static String[] parseDelimitedString(String s, char chDelim); static String indentLines(String sText, String sIndent); static boolean isCharEscaped(char ch); static StringBuilder appendChar(StringBuilder sb, char ch); static String quotedChar(char ch); static StringBuilder appendString(StringBuilder sb, String s); static String quotedString(String s); static long readPackedLong(DataInput in); static void writePackedLong(DataOutput out, long n); static int readPackedInt(DataInput in); static int readMagnitude(DataInput in); static int readIndex(DataInput in); static int readUtf8Char(DataInput in); static void writeUtf8Char(DataOutput out, int ch); static String readUtf8String(DataInput in); static void writeUtf8String(DataOutput out, String s); static InputStream toInputStream(File file); static byte[] readFileBytes(File file); static char[] readFileChars(File file); static char[] readFileChars(File file, String sEncoding); static T[] dedupAdds(T[] aoBase, T[] aoAdd); static T[] append(T[] aoBase, T[] aoAdd); static T[] appendHead(T[] aoBase, T oAdd); static ArrayList<T> startList(T[] ao, int c); static ArrayList<T> appendList(ArrayList<T> list, T[] ao, int of, int c); static boolean anyMatches(T[] ao, T o); static int hashCode(final Object o); static boolean equals(final Object o1, final Object o2); static int compareObjects(final Comparable o1, final Comparable o2); static boolean equalArraysNullOk(final Object[] ao1, final Object[] ao2); static int compareArrays(final Comparable[] ao1, final Comparable[] ao2); static boolean checkElementsNonNull(Object[] ao); static byte[] EMPTY_BYTE_ARRAY; static char[] EMPTY_CHAR_ARRAY; static String[] NO_ARGS; }