Skip to content

Commit 64dd6f9

Browse files
committed
remove unused code
1 parent a6010b7 commit 64dd6f9

File tree

4 files changed

+67
-40
lines changed

4 files changed

+67
-40
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1431,13 +1431,13 @@ public final String readString() {
14311431
int valueLength;
14321432
boolean valueEscape = false;
14331433

1434-
int index = IOUtils.indexOfChar(bytes, quote, offset, end);
1434+
int index = IOUtils.indexOfQuote(bytes, quote, offset, end);
14351435
if (index == -1) {
14361436
throw error("invalid escape character EOI");
14371437
}
14381438
int slashIndex = nextEscapeIndex;
14391439
if (slashIndex == ESCAPE_INDEX_NOT_SET || (slashIndex != -1 && slashIndex < offset)) {
1440-
nextEscapeIndex = slashIndex = IOUtils.indexOfChar(bytes, '\\', offset, end);
1440+
nextEscapeIndex = slashIndex = IOUtils.indexOfSlash(bytes, offset, end);
14411441
}
14421442
if (slashIndex == -1 || slashIndex > index) {
14431443
valueLength = index - offset;

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

+29-14
Original file line numberDiff line numberDiff line change
@@ -1621,19 +1621,23 @@ public static int digit1(byte[] bytes, int off) {
16211621
return d >= 0 && d <= 9 ? d : -1;
16221622
}
16231623

1624-
public static int indexOfChar(byte[] value, int ch, int fromIndex) {
1625-
return indexOfChar(value, ch, fromIndex, value.length);
1624+
public static int indexOfQuote(byte[] value, int quote, int fromIndex, int max) {
1625+
int i = fromIndex;
1626+
int upperBound = fromIndex + ((max - fromIndex) & ~7);
1627+
long vectorQuote = quote == '\'' ? 0x2727_2727_2727_2727L : 0x2222_2222_2222_2222L;
1628+
while (i < upperBound && notContains(getLongLE(value, i), vectorQuote)) {
1629+
i += 8;
1630+
}
1631+
return indexOfChar0(value, quote, i, max);
16261632
}
16271633

1628-
public static int indexOfChar(byte[] value, int ch, int fromIndex, int max) {
1629-
if (INDEX_OF_CHAR_LATIN1 == null) {
1630-
return indexOfChar0(value, ch, fromIndex, max);
1631-
}
1632-
try {
1633-
return (int) INDEX_OF_CHAR_LATIN1.invokeExact(value, ch, fromIndex, max);
1634-
} catch (Throwable e) {
1635-
throw new JSONException(e.getMessage());
1634+
public static int indexOfSlash(byte[] value, int fromIndex, int max) {
1635+
int i = fromIndex;
1636+
int upperBound = fromIndex + ((max - fromIndex) & ~7);
1637+
while (i < upperBound && notContains(getLongLE(value, i), 0x5C5C5C5C5C5C5C5CL)) {
1638+
i += 8;
16361639
}
1640+
return indexOfChar0(value, '\\', i, max);
16371641
}
16381642

16391643
private static int indexOfChar0(byte[] value, int ch, int fromIndex, int max) {
@@ -1645,6 +1649,21 @@ private static int indexOfChar0(byte[] value, int ch, int fromIndex, int max) {
16451649
return -1;
16461650
}
16471651

1652+
private static boolean notContains(long v, long quote) {
1653+
/*
1654+
for (int i = 0; i < 8; ++i) {
1655+
byte c = (byte) v;
1656+
if (c == quote) {
1657+
return true;
1658+
}
1659+
v >>>= 8;
1660+
}
1661+
return false;
1662+
*/
1663+
long x = v ^ quote;
1664+
return (((x - 0x0101010101010101L) & ~x) & 0x8080808080808080L) == 0;
1665+
}
1666+
16481667
public static int hexDigit4(byte[] bytes, int offset) {
16491668
int v = getIntLE(bytes, offset);
16501669
v = (v & 0x0F0F0F0F) + ((((v & 0x40404040) >> 2) | ((v & 0x40404040) << 1)) >>> 4);
@@ -1661,10 +1680,6 @@ public static boolean isDigit(int ch) {
16611680
return ch >= '0' && ch <= '9';
16621681
}
16631682

1664-
public static short getShortUnaligned(byte[] bytes, int offset) {
1665-
return UNSAFE.getShort(bytes, ARRAY_BYTE_BASE_OFFSET + offset);
1666-
}
1667-
16681683
public static short getShortBE(byte[] bytes, int offset) {
16691684
return convEndian(true,
16701685
UNSAFE.getShort(bytes, ARRAY_BYTE_BASE_OFFSET + offset));

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

-16
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public class JDKUtils {
5959

6060
public static final MethodHandle METHOD_HANDLE_HAS_NEGATIVE;
6161
public static final Predicate<byte[]> PREDICATE_IS_ASCII;
62-
public static final MethodHandle INDEX_OF_CHAR_LATIN1;
6362

6463
static final MethodHandles.Lookup IMPL_LOOKUP;
6564
static volatile MethodHandle CONSTRUCTOR_LOOKUP;
@@ -338,21 +337,6 @@ public class JDKUtils {
338337
METHOD_HANDLE_HAS_NEGATIVE = handle;
339338
}
340339

341-
MethodHandle indexOfCharLatin1 = null;
342-
if (JVM_VERSION > 9) {
343-
try {
344-
Class<?> cStringLatin1 = Class.forName("java.lang.StringLatin1");
345-
MethodHandles.Lookup lookup = trustedLookup(cStringLatin1);
346-
indexOfCharLatin1 = lookup.findStatic(
347-
cStringLatin1,
348-
"indexOfChar",
349-
MethodType.methodType(int.class, byte[].class, int.class, int.class, int.class));
350-
} catch (Throwable ignored) {
351-
// ignore
352-
}
353-
}
354-
INDEX_OF_CHAR_LATIN1 = indexOfCharLatin1;
355-
356340
Boolean compact_strings = null;
357341
try {
358342
if (JVM_VERSION == 8) {

core/src/test/java/com/alibaba/fastjson2/util/IOUtilsTest.java

+36-8
Original file line numberDiff line numberDiff line change
@@ -365,17 +365,45 @@ static int hexDigit4(byte[] bytes, int offset) {
365365
}
366366

367367
@Test
368-
public void indexOf() throws Throwable {
369-
byte[] bytes = "abcda".getBytes(StandardCharsets.UTF_8);
368+
public void indexOf() {
369+
byte[] bytes = "'b'd'".getBytes(StandardCharsets.UTF_8);
370370
assertEquals(2,
371-
IOUtils.indexOfChar(
372-
bytes, 'c', 0));
371+
IOUtils.indexOfQuote(
372+
bytes, '\'', 1, bytes.length));
373373
assertEquals(0,
374-
IOUtils.indexOfChar(
375-
bytes, 'a', 0));
374+
IOUtils.indexOfQuote(
375+
bytes, '\'', 0, bytes.length));
376376
assertEquals(4,
377-
IOUtils.indexOfChar(
378-
bytes, 'a', 1));
377+
IOUtils.indexOfQuote(
378+
bytes, '\'', 3, bytes.length));
379+
}
380+
381+
@Test
382+
public void indexOf1() {
383+
byte[] bytes = "\"b\"d\"".getBytes(StandardCharsets.UTF_8);
384+
assertEquals(2,
385+
IOUtils.indexOfQuote(
386+
bytes, '"', 1, bytes.length));
387+
assertEquals(0,
388+
IOUtils.indexOfQuote(
389+
bytes, '"', 0, bytes.length));
390+
assertEquals(4,
391+
IOUtils.indexOfQuote(
392+
bytes, '"', 3, bytes.length));
393+
}
394+
395+
@Test
396+
public void indexOfSlash() {
397+
byte[] bytes = "\\b\\d\\".getBytes(StandardCharsets.UTF_8);
398+
assertEquals(2,
399+
IOUtils.indexOfSlash(
400+
bytes, 1, bytes.length));
401+
assertEquals(0,
402+
IOUtils.indexOfSlash(
403+
bytes, 0, bytes.length));
404+
assertEquals(4,
405+
IOUtils.indexOfSlash(
406+
bytes, 3, bytes.length));
379407
}
380408

381409
@Test

0 commit comments

Comments
 (0)