Skip to content

Commit

Permalink
Cleanup MUTF8
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jan 19, 2024
1 parent 1d8b058 commit c11ef8f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 5 additions & 8 deletions base/src/main/java/org/glavo/japp/util/MUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@ public static String stringFromMUTF8(byte[] bytes, int offset, int count) {
}

StringBuilder builder = new StringBuilder(count);

for (i = offset; i < end; i++) {
byte ch = bytes[i];

if (ch == 0) {
break;
}

boolean isUnicode = (ch & 0x80) != 0;
int uch = ch & 0x7F;

if (isUnicode) {
if (ch > 0) {
builder.append((char) ch);
} else {
int uch = ch & 0x7F;
int mask = 0x40;

while ((uch & mask) != 0) {
Expand All @@ -61,11 +60,9 @@ public static String stringFromMUTF8(byte[] bytes, int offset, int count) {
if ((uch & 0xFFFF) != uch) {
throw new IllegalArgumentException("character out of range \\u" + Integer.toHexString(uch));
}
builder.appendCodePoint(uch);
}

builder.appendCodePoint(uch);
}

return builder.toString();
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/glavo/japp/util/MUTF8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ private static Stream<String> strs() {
"",
"\0\0\0",
"Hello World!",
"Hello\0测试字符串"
"Hello àáâãäå",
"Hello 测试字符串",
"测试测试ABC测试测试\0测试"
);
}

Expand Down

0 comments on commit c11ef8f

Please sign in to comment.