Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 15, 2025
1 parent 22817d0 commit 57d276f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class CSVWriterUTF16
extends CSVWriter {
final Writer out;
final char[] chars;
private static final int DOUBLE_QUOTE_2_UTF16 = '"' | ('"' << 16);

CSVWriterUTF16(
Writer out,
Expand Down Expand Up @@ -139,8 +140,7 @@ public void writeString(final String str) {
for (int i = 0; i < len; ) {
char ch = str.charAt(i++);
if (ch == '"') {
chars[off] = '"';
chars[off + 1] = '"';
IOUtils.putIntUnaligned(chars, off, DOUBLE_QUOTE_2_UTF16);
off += 2;
} else {
chars[off++] = ch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

final class CSVWriterUTF8
extends CSVWriter {
private static final short DOUBLE_QUOTE_2_LATIN1 = 0x22 | (0x22 << 8);

final OutputStream out;
final Charset charset;
final byte[] bytes;
Expand Down Expand Up @@ -171,8 +173,7 @@ public void writeString(byte[] utf8) {
bytes[off++] = '"';
for (byte ch : utf8) {
if (ch == '"') {
bytes[off] = '"';
bytes[off + 1] = '"';
IOUtils.putShortUnaligned(bytes, off, DOUBLE_QUOTE_2_LATIN1);
off += 2;
} else {
bytes[off++] = ch;
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,10 @@ public static boolean isDigit(int ch) {
return ch >= '0' && ch <= '9';
}

public static short getShortUnaligned(byte[] bytes, int offset) {
return UNSAFE.getShort(bytes, ARRAY_BYTE_BASE_OFFSET + offset);
}

public static short getShortLE(byte[] bytes, int offset) {
return convEndian(false,
UNSAFE.getShort(bytes, ARRAY_BYTE_BASE_OFFSET + offset));
Expand Down

0 comments on commit 57d276f

Please sign in to comment.