Skip to content

Commit

Permalink
Optimize surrogate output, verify output goodness
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 18, 2024
1 parent 90bf947 commit 30a4797
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.CharTypes;
Expand Down Expand Up @@ -2154,11 +2153,17 @@ protected final void _outputSurrogates(int surr1, int surr2) throws IOException
bbuf[_outputTail++] = (byte) (0x80 | (c & 0x3f));
}

// @since 2.18
private int _outputSurrogatePair(char highSurrogate, char lowSurrogate, int outputPtr) {
String s = String.valueOf(highSurrogate) + lowSurrogate;
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
System.arraycopy(bytes, 0, _outputBuffer, outputPtr, bytes.length);
return outputPtr + bytes.length;
final int unicode = 0x10000 + ((highSurrogate & 0x03FF) << 10)
+ (lowSurrogate & 0x03FF);

_outputBuffer[outputPtr++] = (byte) (0xF0 + ((unicode & 0b00000000_00011100_00000000_00000000) >> 18));
_outputBuffer[outputPtr++] = (byte) (0x80 + ((unicode & 0b00000000_00000011_11110000_00000000) >> 12));
_outputBuffer[outputPtr++] = (byte) (0x80 + ((unicode & 0b00000000_00000000_00001111_11000000) >> 6));
_outputBuffer[outputPtr++] = (byte) (0x80 + (unicode & 0b00000000_00000000_00000000_00111111));

return outputPtr;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void surrogatesByteBacked() throws Exception
JsonParser p = f.createParser(out.toByteArray());
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals(toQuote, p.getText());
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();

Expand Down

0 comments on commit 30a4797

Please sign in to comment.