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

Prefer bitwise OR over addition for UTF-8 multi-byte encoding #1220

Open
wants to merge 1 commit into
base: 2.17
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -533,8 +533,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 0x10000 + ((surr1 - SURR1_FIRST) << 10) | (surr2 - SURR2_FIRST);
}

/*
Expand Down
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 0x10000 + ((p1 - SURR1_FIRST) << 10) | (p2 - SURR2_FIRST);
}

private static void _illegal(int c) {
Expand Down
Expand Up @@ -159,7 +159,7 @@ public int read(char[] cbuf, int start, int len) throws IOException
reportInvalid(ch, outPtr-start,
String.format(" (above 0x%08x)", LAST_VALID_UNICODE_CHAR));
}
cbuf[outPtr++] = (char) (0xD800 + (ch >> 10));
cbuf[outPtr++] = (char) (0xD800 | (ch >> 10));
// hmmh. can this ever be 0? (not legal, at least?)
lo = (0xDC00 | (ch & 0x03FF));
// Room for second part?
Expand Down
Expand Up @@ -369,7 +369,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 0x10000 + ((firstPart - SURR1_FIRST) << 10) | (secondPart - SURR2_FIRST);
}

protected static void illegalSurrogate(int code) throws IOException {
Expand Down
Expand Up @@ -1919,7 +1919,7 @@ private final String addName(int[] quads, int qlen, int lastQuadBytes)
if (cix >= cbuf.length) {
cbuf = _textBuffer.expandCurrentSegment();
}
cbuf[cix++] = (char) (0xD800 + (ch >> 10));
cbuf[cix++] = (char) (0xD800 | (ch >> 10));
ch = 0xDC00 | (ch & 0x03FF);
}
}
Expand Down
Expand Up @@ -2474,7 +2474,7 @@ private final String addName(int[] quads, int qlen, int lastQuadBytes)
if (cix >= cbuf.length) {
cbuf = _textBuffer.expandCurrentSegment();
}
cbuf[cix++] = (char) (0xD800 + (ch >> 10));
cbuf[cix++] = (char) (0xD800 | (ch >> 10));
ch = 0xDC00 | (ch & 0x03FF);
}
}
Expand Down
Expand Up @@ -793,7 +793,7 @@ protected final String _addName(int[] quads, int qlen, int lastQuadBytes)
if (cix >= cbuf.length) {
cbuf = _textBuffer.expandCurrentSegment();
}
cbuf[cix++] = (char) (0xD800 + (ch >> 10));
cbuf[cix++] = (char) (0xD800 | (ch >> 10));
ch = 0xDC00 | (ch & 0x03FF);
}
}
Expand Down