Skip to content

Commit ca43d31

Browse files
committed
#490 fixed parser to handle unicode.
1 parent 63e6066 commit ca43d31

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

tcMenuJavaApi/src/main/java/com/thecoderscorner/menu/remote/protocol/TagValTextParser.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
public class TagValTextParser {
2121
public static final char FIELD_TERMINATOR = '|';
22+
private static final int MAX_FIELD_EXPECTED = 256;
2223
private final Map<String, String> keyToValue = new HashMap<>(32);
2324

2425
/**
@@ -48,27 +49,27 @@ else if(key.charAt(0) == MenuCommandProtocol.PROTO_END_OF_MSG) {
4849
}
4950

5051
private String readString(ByteBuffer buffer) {
51-
StringBuilder sb = new StringBuilder(32);
52-
while(buffer.hasRemaining()) {
53-
char ch = (char) buffer.get();
54-
if(ch == MenuCommandProtocol.PROTO_END_OF_MSG) {
52+
byte rawData[] = new byte[MAX_FIELD_EXPECTED];
53+
int position = 0;
54+
while(buffer.hasRemaining() && position < MAX_FIELD_EXPECTED) {
55+
var by = buffer.get();
56+
if(by == MenuCommandProtocol.PROTO_END_OF_MSG) {
5557
return "\u0002";
5658
}
57-
else if(ch == '\\') {
59+
else if(by == '\\') {
5860
// special escape case allows anything to be sent
59-
ch = (char) buffer.get();
60-
sb.append(ch);
61+
rawData[position++] = buffer.get();
6162
}
62-
else if(ch == '=' || ch == TagValTextParser.FIELD_TERMINATOR) {
63+
else if(by == '=' || by == TagValTextParser.FIELD_TERMINATOR) {
6364
// end of current token
64-
return sb.toString();
65+
return new String(rawData, 0, position);
6566
}
6667
else {
6768
// within current token
68-
sb.append(ch);
69+
rawData[position++] = by;
6970
}
7071
}
71-
return sb.toString();
72+
return new String(rawData, 0, position);
7273
}
7374

7475
/**

tcMenuJavaApi/src/test/java/com/thecoderscorner/menu/remote/protocol/TagValMenuCommandProtocolTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,14 @@ public void testReceiveActionMenuItem() throws IOException {
299299
assertEquals(0, actMenu.getSubMenuId());
300300
}
301301

302+
@Test
303+
public void testReceiveUTF8Value() throws IOException {
304+
MenuCommand cmd = protocol.fromChannel(toBuffer(ACTION_BOOT_ITEM, "RO=0|PI=0|ID=1|NM=á č ň ť|\u0002"));
305+
assertTrue(cmd instanceof MenuActionBootCommand);
306+
MenuActionBootCommand actMenu = (MenuActionBootCommand) cmd;
307+
assertEquals("á č ň ť", actMenu.getMenuItem().getName());
308+
}
309+
302310
@Test
303311
public void testReceiveBooleanMenuItem() throws IOException {
304312
MenuCommand cmd = protocol.fromChannel(toBuffer(BOOLEAN_BOOT_ITEM, "PI=0|RO=1|VI=1|ID=1|BN=1|NM=BoolItem|VC=1|\u0002"));

0 commit comments

Comments
 (0)