@Test public void testUnescape() throws Exception { assertEquals("", unescape("")); assertEquals("a", unescape("a")); assertEquals("abc", unescape("abc")); assertEquals("_", unescape("__")); assertEquals("__", unescape("____")); assertEquals("_a", unescape("__a")); assertEquals("_abc", unescape("__abc")); assertEquals("a_", unescape("a__")); assertEquals("abc_", unescape("abc__")); assertEquals("some_test", unescape("some__test")); assertEquals("\u0000", unescape("_x00")); assertEquals("\u0001", unescape("_x01")); assertEquals("\u0010", unescape("_x10")); assertEquals("\u0077", unescape("_x77")); assertEquals("\u00A9", unescape("_xa9")); assertEquals("\u00FF", unescape("_xFF")); assertEquals("\u00FF", unescape("_xff")); assertEquals(".", unescape("_x2e")); assertEquals("\u0000", unescape("_u0000")); assertEquals("\u0001", unescape("_u0001")); assertEquals("\uE1A4", unescape("_ue1a4")); assertEquals("\uF000", unescape("_uF000")); assertEquals("\uFFFF", unescape("_uFFFF")); assertEquals(".", unescape("_u002E")); assertEquals("Embedded_underscores\u0312unicode\u0045etc", unescape("Embedded__underscores_u0312unicode_x45etc")); try { unescape("_"); fail(); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { unescape("some _a string"); fail(); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { unescape("_x"); fail(); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { unescape("_x0"); fail(); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { unescape("_u"); fail(); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { unescape("_u-1aaa"); fail(); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { unescape("_u1zaa"); fail(); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { unescape("embedded _x 00 into string"); fail(); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { unescape("embedded _x0 into string"); fail(); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { unescape("embedded _u012 into string"); fail(); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } }
public static String unescape(String s) { StringBuilder sb = new StringBuilder(s.length()); int startIdx = 0; while (startIdx >= 0) { int idx = s.indexOf(ESCAPE_CHAR, startIdx); if (idx >= 0) { sb.append(s.subSequence(startIdx, idx)); idx++; char c = getNextChar(s, idx); switch (c) { case ESCAPE_CHAR: idx++; sb.append(c); break; case 'x': idx++; sb.append(readNextCharFromHexCode(s, idx, 2)); idx += 2; break; case 'u': idx++; sb.append(readNextCharFromHexCode(s, idx, 4)); idx += 4; break; default: throw new IllegalArgumentException(MessageFormat.format( "Unexpected char ''{0}'' in ''{1}'' string located at index {2}", c, s, idx)); } } else { sb.append(s.subSequence(startIdx, s.length())); } startIdx = idx; } return sb.toString(); }
TreeConverterUtil { public static String unescape(String s) { StringBuilder sb = new StringBuilder(s.length()); int startIdx = 0; while (startIdx >= 0) { int idx = s.indexOf(ESCAPE_CHAR, startIdx); if (idx >= 0) { sb.append(s.subSequence(startIdx, idx)); idx++; char c = getNextChar(s, idx); switch (c) { case ESCAPE_CHAR: idx++; sb.append(c); break; case 'x': idx++; sb.append(readNextCharFromHexCode(s, idx, 2)); idx += 2; break; case 'u': idx++; sb.append(readNextCharFromHexCode(s, idx, 4)); idx += 4; break; default: throw new IllegalArgumentException(MessageFormat.format( "Unexpected char ''{0}'' in ''{1}'' string located at index {2}", c, s, idx)); } } else { sb.append(s.subSequence(startIdx, s.length())); } startIdx = idx; } return sb.toString(); } }
TreeConverterUtil { public static String unescape(String s) { StringBuilder sb = new StringBuilder(s.length()); int startIdx = 0; while (startIdx >= 0) { int idx = s.indexOf(ESCAPE_CHAR, startIdx); if (idx >= 0) { sb.append(s.subSequence(startIdx, idx)); idx++; char c = getNextChar(s, idx); switch (c) { case ESCAPE_CHAR: idx++; sb.append(c); break; case 'x': idx++; sb.append(readNextCharFromHexCode(s, idx, 2)); idx += 2; break; case 'u': idx++; sb.append(readNextCharFromHexCode(s, idx, 4)); idx += 4; break; default: throw new IllegalArgumentException(MessageFormat.format( "Unexpected char ''{0}'' in ''{1}'' string located at index {2}", c, s, idx)); } } else { sb.append(s.subSequence(startIdx, s.length())); } startIdx = idx; } return sb.toString(); } private  TreeConverterUtil(); }
TreeConverterUtil { public static String unescape(String s) { StringBuilder sb = new StringBuilder(s.length()); int startIdx = 0; while (startIdx >= 0) { int idx = s.indexOf(ESCAPE_CHAR, startIdx); if (idx >= 0) { sb.append(s.subSequence(startIdx, idx)); idx++; char c = getNextChar(s, idx); switch (c) { case ESCAPE_CHAR: idx++; sb.append(c); break; case 'x': idx++; sb.append(readNextCharFromHexCode(s, idx, 2)); idx += 2; break; case 'u': idx++; sb.append(readNextCharFromHexCode(s, idx, 4)); idx += 4; break; default: throw new IllegalArgumentException(MessageFormat.format( "Unexpected char ''{0}'' in ''{1}'' string located at index {2}", c, s, idx)); } } else { sb.append(s.subSequence(startIdx, s.length())); } startIdx = idx; } return sb.toString(); } private  TreeConverterUtil(); static String unescape(String s); static String escape(String s); }
TreeConverterUtil { public static String unescape(String s) { StringBuilder sb = new StringBuilder(s.length()); int startIdx = 0; while (startIdx >= 0) { int idx = s.indexOf(ESCAPE_CHAR, startIdx); if (idx >= 0) { sb.append(s.subSequence(startIdx, idx)); idx++; char c = getNextChar(s, idx); switch (c) { case ESCAPE_CHAR: idx++; sb.append(c); break; case 'x': idx++; sb.append(readNextCharFromHexCode(s, idx, 2)); idx += 2; break; case 'u': idx++; sb.append(readNextCharFromHexCode(s, idx, 4)); idx += 4; break; default: throw new IllegalArgumentException(MessageFormat.format( "Unexpected char ''{0}'' in ''{1}'' string located at index {2}", c, s, idx)); } } else { sb.append(s.subSequence(startIdx, s.length())); } startIdx = idx; } return sb.toString(); } private  TreeConverterUtil(); static String unescape(String s); static String escape(String s);  }