Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Unicode surrogate pair conversion for generation #1218

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.io.UTF8Writer;
import com.fasterxml.jackson.core.json.DupDetector;
import com.fasterxml.jackson.core.json.JsonWriteContext;
import com.fasterxml.jackson.core.json.PackageVersion;
Expand Down Expand Up @@ -533,8 +534,7 @@ protected final int _decodeSurrogate(int surr1, int surr2) throws IOException
"Incomplete surrogate pair: first char 0x%04X, second 0x%04X", surr1, surr2);
_reportError(msg);
}
int c = 0x10000 + ((surr1 - SURR1_FIRST) << 10) + (surr2 - SURR2_FIRST);
return c;
return (surr1 << 10) + surr2 + UTF8Writer.SURROGATE_BASE;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ private static int _convert(int p1, int p2) {
if (p2 < SURR2_FIRST || p2 > SURR2_LAST) {
throw new IllegalArgumentException("Broken surrogate pair: first char 0x"+Integer.toHexString(p1)+", second 0x"+Integer.toHexString(p2)+"; illegal combination");
}
return 0x10000 + ((p1 - SURR1_FIRST) << 10) + (p2 - SURR2_FIRST);
return (p1 << 10) + p2 + UTF8Writer.SURROGATE_BASE;
}

private static void _illegal(int c) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/fasterxml/jackson/core/io/UTF8Writer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public final class UTF8Writer extends Writer
final static int SURR2_FIRST = 0xDC00;
final static int SURR2_LAST = 0xDFFF;

public static final int SURROGATE_BASE = 0x10000 - UTF8Writer.SURR2_FIRST - (UTF8Writer.SURR1_FIRST << 10);

final private IOContext _context;

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

protected static void illegalSurrogate(int code) throws IOException {
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/fasterxml/jackson/core/io/UTF8WriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.*;

import org.junit.Assert;
import org.junit.Ignore;

public class UTF8WriterTest
extends com.fasterxml.jackson.core.BaseTest
Expand Down Expand Up @@ -136,6 +137,17 @@ public void testSurrogatesFail() throws Exception
}
}

@Ignore
public void testSurrogateConversion() {
for (int first = UTF8Writer.SURR1_FIRST; first <= UTF8Writer.SURR1_LAST; first++) {
for (int second = UTF8Writer.SURR2_FIRST; second <= UTF8Writer.SURR2_LAST; second++) {
int expected = 0x10000 + ((first - UTF8Writer.SURR1_FIRST) << 10) + (second - UTF8Writer.SURR2_FIRST);
int actual = (first << 10) + second + UTF8Writer.SURROGATE_BASE;
assertEquals(Integer.toHexString(first) + " " + Integer.toHexString(second), expected, actual);
}
}
}

private IOContext _ioContext() {
return testIOContext();
}
Expand Down