PrintfMessageParser extends MessageParser { static int nextPrintfTerm(String message, int pos) throws ParseException { while (pos < message.length()) { if (message.charAt(pos++) != '%') { continue; } if (pos < message.length()) { char c = message.charAt(pos); if (c == '%' || c == 'n') { pos += 1; continue; } return pos - 1; } throw ParseException.withStartPosition("trailing unquoted '%' character", message, pos - 1); } return -1; }  @Override final void unescape(StringBuilder out, String message, int start, int end);  }
@Test public void testPrintfNextTerm() throws ParseException { assertThat(PrintfMessageParser.nextPrintfTerm("", 0)).isEqualTo(-1); assertThat(PrintfMessageParser.nextPrintfTerm("%X", 0)).isEqualTo(0); assertThat(PrintfMessageParser.nextPrintfTerm("Hello %X World %X", 0)).isEqualTo(6); assertThat(PrintfMessageParser.nextPrintfTerm("Hello %X World %X", 6)).isEqualTo(6); assertThat(PrintfMessageParser.nextPrintfTerm("Hello %X World %X", 7)).isEqualTo(15); assertThat(PrintfMessageParser.nextPrintfTerm("Hello %% World %X", 0)).isEqualTo(15); assertThat(PrintfMessageParser.nextPrintfTerm("Hello %X World %X", 16)).isEqualTo(-1); }
@Test public void testPrintfNextTermFails() { try { int unused = PrintfMessageParser.nextPrintfTerm("Hello %", 0); fail("expected ParseException"); } catch (ParseException expected) { assertThat(expected.getMessage()).contains("[%]"); } }