Skip to content

Commit 11280d4

Browse files
surrogate conversion simplified (#1218)
Co-authored-by: xtonik <[email protected]>
1 parent 09d2472 commit 11280d4

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

src/main/java/com/fasterxml/jackson/core/base/GeneratorBase.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.fasterxml.jackson.core.*;
88
import com.fasterxml.jackson.core.io.IOContext;
9+
import com.fasterxml.jackson.core.io.UTF8Writer;
910
import com.fasterxml.jackson.core.json.DupDetector;
1011
import com.fasterxml.jackson.core.json.JsonWriteContext;
1112
import com.fasterxml.jackson.core.json.PackageVersion;
@@ -533,8 +534,7 @@ protected final int _decodeSurrogate(int surr1, int surr2) throws IOException
533534
"Incomplete surrogate pair: first char 0x%04X, second 0x%04X", surr1, surr2);
534535
_reportError(msg);
535536
}
536-
int c = 0x10000 + ((surr1 - SURR1_FIRST) << 10) + (surr2 - SURR2_FIRST);
537-
return c;
537+
return (surr1 << 10) + surr2 + UTF8Writer.SURROGATE_BASE;
538538
}
539539

540540
/*

src/main/java/com/fasterxml/jackson/core/io/JsonStringEncoder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ private static int _convert(int p1, int p2) {
659659
if (p2 < SURR2_FIRST || p2 > SURR2_LAST) {
660660
throw new IllegalArgumentException("Broken surrogate pair: first char 0x"+Integer.toHexString(p1)+", second 0x"+Integer.toHexString(p2)+"; illegal combination");
661661
}
662-
return 0x10000 + ((p1 - SURR1_FIRST) << 10) + (p2 - SURR2_FIRST);
662+
return (p1 << 10) + p2 + UTF8Writer.SURROGATE_BASE;
663663
}
664664

665665
private static void _illegal(int c) {

src/main/java/com/fasterxml/jackson/core/io/UTF8Writer.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public final class UTF8Writer extends Writer
99
final static int SURR2_FIRST = 0xDC00;
1010
final static int SURR2_LAST = 0xDFFF;
1111

12+
public static final int SURROGATE_BASE = 0x10000 - UTF8Writer.SURR2_FIRST - (UTF8Writer.SURR1_FIRST << 10);
13+
1214
final private IOContext _context;
1315

1416
private OutputStream _out;
@@ -369,7 +371,7 @@ protected int convertSurrogate(int secondPart)
369371
if (secondPart < SURR2_FIRST || secondPart > SURR2_LAST) {
370372
throw new IOException("Broken surrogate pair: first char 0x"+Integer.toHexString(firstPart)+", second 0x"+Integer.toHexString(secondPart)+"; illegal combination");
371373
}
372-
return 0x10000 + ((firstPart - SURR1_FIRST) << 10) + (secondPart - SURR2_FIRST);
374+
return (firstPart << 10) + secondPart + UTF8Writer.SURROGATE_BASE;
373375
}
374376

375377
protected static void illegalSurrogate(int code) throws IOException {

src/test/java/com/fasterxml/jackson/core/io/UTF8WriterTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44

55
import org.junit.Assert;
6+
import org.junit.Ignore;
67

78
public class UTF8WriterTest
89
extends com.fasterxml.jackson.core.BaseTest
@@ -136,6 +137,17 @@ public void testSurrogatesFail() throws Exception
136137
}
137138
}
138139

140+
@Ignore
141+
public void testSurrogateConversion() {
142+
for (int first = UTF8Writer.SURR1_FIRST; first <= UTF8Writer.SURR1_LAST; first++) {
143+
for (int second = UTF8Writer.SURR2_FIRST; second <= UTF8Writer.SURR2_LAST; second++) {
144+
int expected = 0x10000 + ((first - UTF8Writer.SURR1_FIRST) << 10) + (second - UTF8Writer.SURR2_FIRST);
145+
int actual = (first << 10) + second + UTF8Writer.SURROGATE_BASE;
146+
assertEquals(Integer.toHexString(first) + " " + Integer.toHexString(second), expected, actual);
147+
}
148+
}
149+
}
150+
139151
private IOContext _ioContext() {
140152
return testIOContext();
141153
}

0 commit comments

Comments
 (0)