@Test @DisplayName("Test some extra corner cases") void tetsCornerCases() { final var sut = new Deunicode(); Assertions.assertEquals("\\", sut.apply("\\")); Assertions.assertEquals("\\t", sut.apply("\\t")); }
@Override public String apply(String s) { final var output = new StringBuilder(); for (int index = 0; index < s.length(); index++) { final var currentChar = s.charAt(index); if (currentChar == '\\' && index + 1 < s.length() && s.charAt(index + 1) == 'u') { index++; while (index < s.length() && s.charAt(index) == 'u') { index++; } if (index + 3 < s.length()) { try { final var hex = Integer.parseInt(s.substring(index, index + 4), 16); index += 3; output.append((char) hex); } catch (NumberFormatException e) { throw new GeciException( "The Java source starts a unicode escape that is malformed at position " + index + " ... " + s.substring(index, index + 4)); } } else { throw new GeciException( "The Java source input ended premature at position " + index + " ... " + s.substring(index)); } } else { output.append(currentChar); if (currentChar == '\\' && index + 1 < s.length()) { output.append(s.charAt(index + 1)); index++; } } } return output.toString(); }
Deunicode implements Function<String, String> { @Override public String apply(String s) { final var output = new StringBuilder(); for (int index = 0; index < s.length(); index++) { final var currentChar = s.charAt(index); if (currentChar == '\\' && index + 1 < s.length() && s.charAt(index + 1) == 'u') { index++; while (index < s.length() && s.charAt(index) == 'u') { index++; } if (index + 3 < s.length()) { try { final var hex = Integer.parseInt(s.substring(index, index + 4), 16); index += 3; output.append((char) hex); } catch (NumberFormatException e) { throw new GeciException( "The Java source starts a unicode escape that is malformed at position " + index + " ... " + s.substring(index, index + 4)); } } else { throw new GeciException( "The Java source input ended premature at position " + index + " ... " + s.substring(index)); } } else { output.append(currentChar); if (currentChar == '\\' && index + 1 < s.length()) { output.append(s.charAt(index + 1)); index++; } } } return output.toString(); } }
Deunicode implements Function<String, String> { @Override public String apply(String s) { final var output = new StringBuilder(); for (int index = 0; index < s.length(); index++) { final var currentChar = s.charAt(index); if (currentChar == '\\' && index + 1 < s.length() && s.charAt(index + 1) == 'u') { index++; while (index < s.length() && s.charAt(index) == 'u') { index++; } if (index + 3 < s.length()) { try { final var hex = Integer.parseInt(s.substring(index, index + 4), 16); index += 3; output.append((char) hex); } catch (NumberFormatException e) { throw new GeciException( "The Java source starts a unicode escape that is malformed at position " + index + " ... " + s.substring(index, index + 4)); } } else { throw new GeciException( "The Java source input ended premature at position " + index + " ... " + s.substring(index)); } } else { output.append(currentChar); if (currentChar == '\\' && index + 1 < s.length()) { output.append(s.charAt(index + 1)); index++; } } } return output.toString(); }  }
Deunicode implements Function<String, String> { @Override public String apply(String s) { final var output = new StringBuilder(); for (int index = 0; index < s.length(); index++) { final var currentChar = s.charAt(index); if (currentChar == '\\' && index + 1 < s.length() && s.charAt(index + 1) == 'u') { index++; while (index < s.length() && s.charAt(index) == 'u') { index++; } if (index + 3 < s.length()) { try { final var hex = Integer.parseInt(s.substring(index, index + 4), 16); index += 3; output.append((char) hex); } catch (NumberFormatException e) { throw new GeciException( "The Java source starts a unicode escape that is malformed at position " + index + " ... " + s.substring(index, index + 4)); } } else { throw new GeciException( "The Java source input ended premature at position " + index + " ... " + s.substring(index)); } } else { output.append(currentChar); if (currentChar == '\\' && index + 1 < s.length()) { output.append(s.charAt(index + 1)); index++; } } } return output.toString(); }  @Override String apply(String s); }
Deunicode implements Function<String, String> { @Override public String apply(String s) { final var output = new StringBuilder(); for (int index = 0; index < s.length(); index++) { final var currentChar = s.charAt(index); if (currentChar == '\\' && index + 1 < s.length() && s.charAt(index + 1) == 'u') { index++; while (index < s.length() && s.charAt(index) == 'u') { index++; } if (index + 3 < s.length()) { try { final var hex = Integer.parseInt(s.substring(index, index + 4), 16); index += 3; output.append((char) hex); } catch (NumberFormatException e) { throw new GeciException( "The Java source starts a unicode escape that is malformed at position " + index + " ... " + s.substring(index, index + 4)); } } else { throw new GeciException( "The Java source input ended premature at position " + index + " ... " + s.substring(index)); } } else { output.append(currentChar); if (currentChar == '\\' && index + 1 < s.length()) { output.append(s.charAt(index + 1)); index++; } } } return output.toString(); }  @Override String apply(String s);  }