Skip to content

Commit

Permalink
Optimize MUTF8.stringFromMUTF8
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jan 19, 2024
1 parent fed889c commit e841267
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion base/src/main/java/org/glavo/japp/util/MUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,26 @@
*/
package org.glavo.japp.util;

import java.nio.charset.StandardCharsets;

public final class MUTF8 {
public static String stringFromMUTF8(byte[] bytes, int offset, int count) {
final int end = offset + count;

int i;
for (i = offset; i < end; i++) {
if ((bytes[i] & 0x80) != 0) {
break;
}
}

if (i == end) {
return new String(bytes, offset, count, StandardCharsets.US_ASCII);
}

StringBuilder builder = new StringBuilder(count);

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

if (ch == 0) {
Expand Down

0 comments on commit e841267

Please sign in to comment.