Skip to content

Commit

Permalink
Merge pull request #508 from TcMenu/main-all-builder-theme
Browse files Browse the repository at this point in the history
#490 fixed parser to handle unicode.
  • Loading branch information
davetcc authored Sep 7, 2024
2 parents f3eac4a + ca43d31 commit 73137be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
public class TagValTextParser {
public static final char FIELD_TERMINATOR = '|';
private static final int MAX_FIELD_EXPECTED = 256;
private final Map<String, String> keyToValue = new HashMap<>(32);

/**
Expand Down Expand Up @@ -48,27 +49,27 @@ else if(key.charAt(0) == MenuCommandProtocol.PROTO_END_OF_MSG) {
}

private String readString(ByteBuffer buffer) {
StringBuilder sb = new StringBuilder(32);
while(buffer.hasRemaining()) {
char ch = (char) buffer.get();
if(ch == MenuCommandProtocol.PROTO_END_OF_MSG) {
byte rawData[] = new byte[MAX_FIELD_EXPECTED];
int position = 0;
while(buffer.hasRemaining() && position < MAX_FIELD_EXPECTED) {
var by = buffer.get();
if(by == MenuCommandProtocol.PROTO_END_OF_MSG) {
return "\u0002";
}
else if(ch == '\\') {
else if(by == '\\') {
// special escape case allows anything to be sent
ch = (char) buffer.get();
sb.append(ch);
rawData[position++] = buffer.get();
}
else if(ch == '=' || ch == TagValTextParser.FIELD_TERMINATOR) {
else if(by == '=' || by == TagValTextParser.FIELD_TERMINATOR) {
// end of current token
return sb.toString();
return new String(rawData, 0, position);
}
else {
// within current token
sb.append(ch);
rawData[position++] = by;
}
}
return sb.toString();
return new String(rawData, 0, position);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ public void testReceiveActionMenuItem() throws IOException {
assertEquals(0, actMenu.getSubMenuId());
}

@Test
public void testReceiveUTF8Value() throws IOException {
MenuCommand cmd = protocol.fromChannel(toBuffer(ACTION_BOOT_ITEM, "RO=0|PI=0|ID=1|NM=á č ň ť|\u0002"));
assertTrue(cmd instanceof MenuActionBootCommand);
MenuActionBootCommand actMenu = (MenuActionBootCommand) cmd;
assertEquals("á č ň ť", actMenu.getMenuItem().getName());
}

@Test
public void testReceiveBooleanMenuItem() throws IOException {
MenuCommand cmd = protocol.fromChannel(toBuffer(BOOLEAN_BOOT_ITEM, "PI=0|RO=1|VI=1|ID=1|BN=1|NM=BoolItem|VC=1|\u0002"));
Expand Down

0 comments on commit 73137be

Please sign in to comment.