Skip to content

Commit

Permalink
remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 13, 2025
1 parent ac341c5 commit 296a7c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
28 changes: 3 additions & 25 deletions core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1668,28 +1668,6 @@ public static int utf16Hex2(int i) {
+ 0x00300030 + i;
}

public static int hex4(int i) {
i = reverseBytesExpand(i);
/*
0 = 0b0000_0000 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 0 + 0x30 + (x & 0xF) => 0
1 = 0b0000_0001 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 0 + 0x30 + (x & 0xF) => 1
2 = 0b0000_0010 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 0 + 0x30 + (x & 0xF) => 2
3 = 0b0000_0011 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 0 + 0x30 + (x & 0xF) => 3
4 = 0b0000_0100 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 0 + 0x30 + (x & 0xF) => 4
5 = 0b0000_0101 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 0 + 0x30 + (x & 0xF) => 5
6 = 0b0000_0110 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 0 + 0x30 + (x & 0xF) => 6
7 = 0b0000_0111 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 0 + 0x30 + (x & 0xF) => 7
8 = 0b0000_1000 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 0 + 0x30 + (x & 0xF) => 8
9 = 0b0000_1001 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 0 + 0x30 + (x & 0xF) => 9
10 = 0b0000_1010 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 39 + 0x30 + (x & 0xF) => a
11 = 0b0000_1011 => m = ((x + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 39 + 0x30 + (x & 0xF) => b
*/
int m = (i + 0x06060606) & 0x10101010;
return ((m << 1) + (m >> 1) - (m >> 4))
+ 0x30303030
+ i;
}

public static int hex4U(int i) {
i = reverseBytesExpand(i);
/*
Expand Down Expand Up @@ -1753,15 +1731,15 @@ private static long utf16ReverseBytesExpand(long i) {
return ((i & 0xF000L) >> 12) | ((i & 0xF00L) << 8) | ((i & 0xF0L) << 28) | ((i & 0xFL) << 48);
}

private static int convEndian(boolean big, int n) {
static int convEndian(boolean big, int n) {
return big == BIG_ENDIAN ? n : Integer.reverseBytes(n);
}

private static long convEndian(boolean big, long n) {
static long convEndian(boolean big, long n) {
return big == BIG_ENDIAN ? n : Long.reverseBytes(n);
}

private static short convEndian(boolean big, short n) {
static short convEndian(boolean big, short n) {
return big == BIG_ENDIAN ? n : Short.reverseBytes(n);
}
}
21 changes: 19 additions & 2 deletions core/src/test/java/com/alibaba/fastjson2/util/IOUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.Random;

import static com.alibaba.fastjson2.util.JDKUtils.ARRAY_BYTE_BASE_OFFSET;
import static com.alibaba.fastjson2.util.JDKUtils.UNSAFE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

public class IOUtilsTest {
@Test
Expand Down Expand Up @@ -373,4 +373,21 @@ public void indexOf() throws Throwable {
IOUtils.indexOfChar(
bytes, 'a', 1));
}

@Test
public void convEndian() throws Throwable {
Random r = new Random();
long i64 = r.nextLong();
int i32 = r.nextInt();
short i16 = (short) r.nextInt();

assertEquals(i64, IOUtils.convEndian(false, i64));
assertEquals(Long.reverseBytes(i64), IOUtils.convEndian(true, i64));

assertEquals(i32, IOUtils.convEndian(false, i32));
assertEquals(Integer.reverseBytes(i32), IOUtils.convEndian(true, i32));

assertEquals(i16, IOUtils.convEndian(false, i16));
assertEquals(Short.reverseBytes(i16), IOUtils.convEndian(true, i16));
}
}

0 comments on commit 296a7c3

Please sign in to comment.