while(start < stringMessage.length() && matchChar(stringMessage, start, '[') == start) { Preconditions.checkArgument(stringMessage.charAt(start) == '[', "Invalid structured data in syslog message '%s'", stringMessage); end = matchChar(stringMessage, start, ']'); Preconditions.checkArgument(end != -1 && stringMessage.charAt(end) == ']', "Invalid structured data in syslog message '%s'", stringMessage); while (start < end) { if (key == null) { final int keyEnd = matchChar(stringMessage, ++start, ']', ' '); // Key can be terminated by a space (then more fields to follow) or a ] key = stringMessage.substring(start, keyEnd); final int valueEnd = matchChar(stringMessage, equalsIndex + 2, '"'); Preconditions.checkArgument(valueEnd != -1 && stringMessage.charAt(valueEnd) == '"', "Invalid structured data in syslog message '%s'", stringMessage);
@Test public void testMatchChar() throws Exception { Assert.assertEquals(2, StructuredSyslogMessage.matchChar("hello", 0, 'l')); Assert.assertEquals(2, StructuredSyslogMessage.matchChar("hello", 2, 'l')); Assert.assertEquals(3, StructuredSyslogMessage.matchChar("hello", 3, 'l')); Assert.assertEquals(-1, StructuredSyslogMessage.matchChar("hello", 4, 'l')); Assert.assertEquals(-1, StructuredSyslogMessage.matchChar("hello", 10, 'l')); Assert.assertEquals(2, StructuredSyslogMessage.matchChar("\\ll", 0, 'l')); Assert.assertEquals(-1, StructuredSyslogMessage.matchChar("\\l\\l", 0, 'l')); Assert.assertEquals(-1, StructuredSyslogMessage.matchChar("\\", 0, 'x')); Assert.assertEquals(-1, StructuredSyslogMessage.matchChar("foo\\", 0, 'x')); Assert.assertEquals(-1, StructuredSyslogMessage.matchChar("this\\\"is\\ a\\ test.", 0, ' ')); }