Skip to content

Commit 2b50ce9

Browse files
committed
PREDICATE_IS_ASCII
1 parent 64dd6f9 commit 2b50ce9

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

core/src/main/java/com/alibaba/fastjson2/JSONReader.java

+4-19
Original file line numberDiff line numberDiff line change
@@ -3240,13 +3240,8 @@ protected final String toString(Map object) {
32403240
}
32413241

32423242
public static JSONReader of(byte[] utf8Bytes) {
3243-
boolean ascii = false;
3244-
if (PREDICATE_IS_ASCII != null) {
3245-
ascii = PREDICATE_IS_ASCII.test(utf8Bytes);
3246-
}
3247-
32483243
Context context = createReadContext();
3249-
if (ascii) {
3244+
if (PREDICATE_IS_ASCII.test(utf8Bytes)) {
32503245
return new JSONReaderASCII(context, null, utf8Bytes, 0, utf8Bytes.length);
32513246
}
32523247

@@ -3255,25 +3250,15 @@ public static JSONReader of(byte[] utf8Bytes) {
32553250

32563251
@Deprecated
32573252
public static JSONReader of(Context context, byte[] utf8Bytes) {
3258-
boolean ascii = false;
3259-
if (PREDICATE_IS_ASCII != null) {
3260-
ascii = PREDICATE_IS_ASCII.test(utf8Bytes);
3261-
}
3262-
3263-
if (ascii) {
3253+
if (PREDICATE_IS_ASCII.test(utf8Bytes)) {
32643254
return new JSONReaderASCII(context, null, utf8Bytes, 0, utf8Bytes.length);
32653255
}
32663256

32673257
return new JSONReaderUTF8(context, null, utf8Bytes, 0, utf8Bytes.length);
32683258
}
32693259

32703260
public static JSONReader of(byte[] utf8Bytes, Context context) {
3271-
boolean ascii = false;
3272-
if (PREDICATE_IS_ASCII != null) {
3273-
ascii = PREDICATE_IS_ASCII.test(utf8Bytes);
3274-
}
3275-
3276-
if (ascii) {
3261+
if (PREDICATE_IS_ASCII.test(utf8Bytes)) {
32773262
return new JSONReaderASCII(context, null, utf8Bytes, 0, utf8Bytes.length);
32783263
}
32793264

@@ -3510,7 +3495,7 @@ public static JSONReader of(String str) {
35103495
}
35113496

35123497
Context context = JSONFactory.createReadContext();
3513-
if (STRING_VALUE != null && STRING_CODER != null && PREDICATE_IS_ASCII != null) {
3498+
if (STRING_VALUE != null && STRING_CODER != null) {
35143499
try {
35153500
final int LATIN1 = 0;
35163501
int coder = STRING_CODER.applyAsInt(str);

core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java

+20
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ public class JDKUtils {
316316
initErrorLast = e;
317317
}
318318
}
319+
if (isAscii == null) {
320+
isAscii = JDKUtils::isASCII;
321+
}
319322

320323
PREDICATE_IS_ASCII = isAscii;
321324
}
@@ -506,4 +509,21 @@ public static String latin1StringJDK8(byte[] bytes, int offset, int strlen) {
506509
}
507510
return STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
508511
}
512+
513+
public static boolean isASCII(byte[] chars) {
514+
int i = 0;
515+
int strlen = chars.length;
516+
for (int upperBound = (strlen & ~7); i < upperBound; i += 8) {
517+
if ((UNSAFE.getLong(chars, ARRAY_BYTE_BASE_OFFSET + i) & 0x8080808080808080L) != 0) {
518+
return false;
519+
}
520+
}
521+
522+
for (; i < strlen; ++i) {
523+
if (UNSAFE.getByte(chars, ARRAY_BYTE_BASE_OFFSET + i) < 0) {
524+
return false;
525+
}
526+
}
527+
return true;
528+
}
509529
}

0 commit comments

Comments
 (0)