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

Optimize char comparison using bitwise OR #1217

Merged
merged 3 commits 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 @@ -713,7 +713,7 @@ public final JsonToken nextToken() throws IOException
_binaryValue = null;

// Closing scope?
if (i == INT_RBRACKET || i == INT_RCURLY) {
if ((i | 0x20) == INT_RCURLY) { // ~ '}]'
_closeScope(i);
return _currToken;
}
Expand All @@ -724,7 +724,7 @@ public final JsonToken nextToken() throws IOException

// Was that a trailing comma?
if ((_features & FEAT_MASK_TRAILING_COMMA) != 0) {
if ((i == INT_RBRACKET) || (i == INT_RCURLY)) {
if ((i | 0x20) == INT_RCURLY) { // ~ '}]'
_closeScope(i);
return _currToken;
}
Expand Down Expand Up @@ -876,7 +876,7 @@ public boolean nextFieldName(SerializableString sstr) throws IOException
_binaryValue = null;

// Closing scope?
if (i == INT_RBRACKET || i == INT_RCURLY) {
if ((i | 0x20) == INT_RCURLY) { // ~ '}]'
_closeScope(i);
return false;
}
Expand All @@ -886,7 +886,7 @@ public boolean nextFieldName(SerializableString sstr) throws IOException

// Was that a trailing comma?
if ((_features & FEAT_MASK_TRAILING_COMMA) != 0) {
if ((i == INT_RBRACKET) || (i == INT_RCURLY)) {
if ((i | 0x20) == INT_RCURLY) { // ~ '}]'
_closeScope(i);
return false;
}
Expand Down Expand Up @@ -950,14 +950,14 @@ public String nextFieldName() throws IOException
return null;
}
_binaryValue = null;
if (i == INT_RBRACKET || i == INT_RCURLY) {
if ((i | 0x20) == INT_RCURLY) { // ~ '}]'
_closeScope(i);
return null;
}
if (_parsingContext.expectComma()) {
i = _skipComma(i);
if ((_features & FEAT_MASK_TRAILING_COMMA) != 0) {
if ((i == INT_RBRACKET) || (i == INT_RCURLY)) {
if ((i | 0x20) == INT_RCURLY) { // ~ '}]'
_closeScope(i);
return null;
}
Expand Down Expand Up @@ -1418,7 +1418,7 @@ protected final JsonToken _parseUnsignedNumber(int ch) throws IOException
}
++intLen;
}
if (ch == INT_PERIOD || ch == INT_e || ch == INT_E) {
if (ch == INT_PERIOD || (ch | 0x20) == INT_e) { // ~ '.eE'
_inputPtr = ptr;
return _parseFloat(ch, startPtr, ptr, false, intLen);
}
Expand Down Expand Up @@ -1462,7 +1462,7 @@ private final JsonToken _parseFloat(int ch, int startPtr, int ptr, boolean neg,
}
}
int expLen = 0;
if (ch == 'e' || ch == 'E') { // and/or exponent
if ((ch | 0x20) == INT_e) { // ~ 'eE' and/or exponent
if (ptr >= inputLen) {
_inputPtr = startPtr;
return _parseNumber2(neg, startPtr);
Expand Down Expand Up @@ -1541,7 +1541,7 @@ private final JsonToken _parseSignedNumber(final boolean negative) throws IOExce
++intLen;
}

if (ch == INT_PERIOD || ch == INT_e || ch == INT_E) {
if (ch == INT_PERIOD || (ch | 0x20) == INT_e) { // ~ '.eE'
_inputPtr = ptr;
return _parseFloat(ch, startPtr, ptr, negative, intLen);
}
Expand Down Expand Up @@ -1653,7 +1653,7 @@ private final JsonToken _parseNumber2(boolean neg, int startPtr) throws IOExcept
}

int expLen = -1;
if (c == 'e' || c == 'E') { // exponent?
if ((c | 0x20) == INT_e) { // ~ 'eE' exponent?
expLen = 0;
if (outPtr >= outBuf.length) {
outBuf = _textBuffer.finishCurrentSegment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ public JsonToken nextToken() throws IOException
_tokenInputRow = _currInputRow;

// Closing scope?
if (i == INT_RBRACKET || i == INT_RCURLY) {
if ((i | 0x20) == INT_RCURLY) { // ~ '}]'
_closeScope(i);
return _currToken;
}
Expand All @@ -625,7 +625,7 @@ public JsonToken nextToken() throws IOException

// Was that a trailing comma?
if ((_features & FEAT_MASK_TRAILING_COMMA) != 0) {
if (i == INT_RBRACKET || i == INT_RCURLY) {
if ((i | 0x20) == INT_RCURLY) { // ~ '}]'
_closeScope(i);
return _currToken;
}
Expand Down Expand Up @@ -801,7 +801,7 @@ public String nextFieldName() throws IOException
_binaryValue = null;
_tokenInputRow = _currInputRow;

if (i == INT_RBRACKET || i == INT_RCURLY) {
if ((i | 0x20) == INT_RCURLY) { // ~ '}]'
_closeScope(i);
return null;
}
Expand All @@ -815,7 +815,7 @@ public String nextFieldName() throws IOException

// Was that a trailing comma?
if ((_features & FEAT_MASK_TRAILING_COMMA) != 0) {
if (i == INT_RBRACKET || i == INT_RCURLY) {
if ((i | 0x20) == INT_RCURLY) { // ~ '}]'
_closeScope(i);
return null;
}
Expand Down Expand Up @@ -1077,7 +1077,7 @@ protected JsonToken _parseUnsignedNumber(int c) throws IOException
outBuf[outPtr++] = (char) c;
c = _inputData.readUnsignedByte();
}
if (c == '.' || c == 'e' || c == 'E') {
if (c == '.' || (c | 0x20) == INT_e) { // ~ '.eE'
return _parseFloat(outBuf, outPtr, c, false, intLen);
}
_textBuffer.setCurrentLength(outPtr);
Expand Down Expand Up @@ -1140,7 +1140,7 @@ private final JsonToken _parseSignedNumber(boolean negative) throws IOException
outBuf[outPtr++] = (char) c;
c = _inputData.readUnsignedByte();
}
if (c == '.' || c == 'e' || c == 'E') {
if (c == '.' || (c | 0x20) == INT_e) { // ~ '.eE'
return _parseFloat(outBuf, outPtr, c, negative, intLen);
}
_textBuffer.setCurrentLength(outPtr);
Expand Down Expand Up @@ -1217,7 +1217,7 @@ private final JsonToken _parseFloat(char[] outBuf, int outPtr, int c,
}

int expLen = 0;
if (c == INT_e || c == INT_E) { // exponent?
if ((c | 0x20) == INT_e) { // ~ 'eE' exponent?
if (outPtr >= outBuf.length) {
outBuf = _textBuffer.finishCurrentSegment();
outPtr = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ protected JsonToken _parseUnsignedNumber(int c) throws IOException
++intLen;
outBuf[outPtr++] = (char) c;
}
if (c == INT_PERIOD || c == INT_e || c == INT_E) {
if (c == INT_PERIOD || (c | 0x20) == INT_e) { // ~ '.eE'
return _parseFloat(outBuf, outPtr, c, false, intLen);
}
--_inputPtr; // to push back trailing char (comma etc)
Expand Down Expand Up @@ -1568,7 +1568,7 @@ private final JsonToken _parseSignedNumber(boolean negative) throws IOException
++intLen;
outBuf[outPtr++] = (char) c;
}
if (c == INT_PERIOD || c == INT_e || c == INT_E) {
if (c == INT_PERIOD || (c | 0x20) == INT_e) { // ~ '.eE'
return _parseFloat(outBuf, outPtr, c, negative, intLen);
}

Expand Down Expand Up @@ -1596,7 +1596,7 @@ private final JsonToken _parseNumber2(char[] outBuf, int outPtr, boolean negativ
}
int c = (int) _inputBuffer[_inputPtr++] & 0xFF;
if (c > INT_9 || c < INT_0) {
if (c == INT_PERIOD || c == INT_e || c == INT_E) {
if (c == INT_PERIOD || (c | 0x20) == INT_e) { // ~ '.eE'
return _parseFloat(outBuf, outPtr, c, negative, intPartLength);
}
break;
Expand Down Expand Up @@ -1695,7 +1695,7 @@ private final JsonToken _parseFloat(char[] outBuf, int outPtr, int c,
}

int expLen = 0;
if (c == INT_e || c == INT_E) { // exponent?
if ((c | 0x20) == INT_e) { // ~ 'eE' exponent?
if (outPtr >= outBuf.length) {
outBuf = _textBuffer.finishCurrentSegment();
outPtr = 0;
Expand Down Expand Up @@ -2953,7 +2953,7 @@ protected final void _matchTrue() throws IOException
&& (buf[ptr++] == 'u')
&& (buf[ptr++] == 'e')) {
int ch = buf[ptr] & 0xFF;
if (ch < INT_0 || (ch == INT_RBRACKET) || (ch == INT_RCURLY)) { // expected/allowed chars
if (ch < INT_0 || (ch | 0x20) == INT_RCURLY) { // < '0' || ~ '}]' expected/allowed chars
_inputPtr = ptr;
return;
}
Expand All @@ -2972,7 +2972,7 @@ protected final void _matchFalse() throws IOException
&& (buf[ptr++] == 's')
&& (buf[ptr++] == 'e')) {
int ch = buf[ptr] & 0xFF;
if (ch < INT_0 || (ch == INT_RBRACKET) || (ch == INT_RCURLY)) { // expected/allowed chars
if (ch < INT_0 || (ch | 0x20) == INT_RCURLY) { // < '0' || ~ '}]' expected/allowed chars
_inputPtr = ptr;
return;
}
Expand All @@ -2990,7 +2990,7 @@ protected final void _matchNull() throws IOException
&& (buf[ptr++] == 'l')
&& (buf[ptr++] == 'l')) {
int ch = buf[ptr] & 0xFF;
if (ch < INT_0 || (ch == INT_RBRACKET) || (ch == INT_RCURLY)) { // expected/allowed chars
if (ch < INT_0 || (ch | 0x20) == INT_RCURLY) { // < '0' || ~ '}]' expected/allowed chars
_inputPtr = ptr;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ protected JsonToken _startFalseToken() throws IOException
&& (getByteFromBuffer(ptr++) == 's')
&& (getByteFromBuffer(ptr++) == 'e')) {
int ch = getByteFromBuffer(ptr) & 0xFF;
if (ch < INT_0 || (ch == INT_RBRACKET) || (ch == INT_RCURLY)) { // expected/allowed chars
if (ch < INT_0 || (ch | 0x20) == INT_RCURLY) { // < '0' || ~ '}]' expected/allowed chars
_inputPtr = ptr;
return _valueComplete(JsonToken.VALUE_FALSE);
}
Expand All @@ -1132,7 +1132,7 @@ protected JsonToken _startTrueToken() throws IOException
&& (getByteFromBuffer(ptr++) == 'u')
&& (getByteFromBuffer(ptr++) == 'e')) {
int ch = getByteFromBuffer(ptr) & 0xFF;
if (ch < INT_0 || (ch == INT_RBRACKET) || (ch == INT_RCURLY)) { // expected/allowed chars
if (ch < INT_0 || (ch | 0x20) == INT_RCURLY) { // < '0' || ~ '}]' expected/allowed chars
_inputPtr = ptr;
return _valueComplete(JsonToken.VALUE_TRUE);
}
Expand All @@ -1150,7 +1150,7 @@ protected JsonToken _startNullToken() throws IOException
&& (getByteFromBuffer(ptr++) == 'l')
&& (getByteFromBuffer(ptr++) == 'l')) {
int ch = getByteFromBuffer(ptr) & 0xFF;
if (ch < INT_0 || (ch == INT_RBRACKET) || (ch == INT_RCURLY)) { // expected/allowed chars
if (ch < INT_0 || (ch | 0x20) == INT_RCURLY) { // < '0' || ~ '}]' expected/allowed chars
_inputPtr = ptr;
return _valueComplete(JsonToken.VALUE_NULL);
}
Expand All @@ -1172,7 +1172,7 @@ protected JsonToken _finishKeywordToken(String expToken, int matched,
}
int ch = getByteFromBuffer(_inputPtr);
if (matched == end) { // need to verify trailing separator
if (ch < INT_0 || (ch == INT_RBRACKET) || (ch == INT_RCURLY)) { // expected/allowed chars
if (ch < INT_0 || (ch | 0x20) == INT_RCURLY) { // < '0' || ~ '}]' expected/allowed chars
return _valueComplete(result);
}
break;
Expand Down Expand Up @@ -1212,7 +1212,7 @@ protected JsonToken _finishNonStdToken(int type, int matched) throws IOException
}
int ch = getByteFromBuffer(_inputPtr);
if (matched == end) { // need to verify trailing separator
if (ch < INT_0 || (ch == INT_RBRACKET) || (ch == INT_RCURLY)) { // expected/allowed chars
if (ch < INT_0 || (ch | 0x20) == INT_RCURLY) { // < '0' || ~ '}]' expected/allowed chars
return _valueNonStdNumberComplete(type);
}
break;
Expand Down Expand Up @@ -1313,7 +1313,7 @@ protected JsonToken _startPositiveNumber(int ch) throws IOException
break;
}
if (ch > INT_9) {
if (ch == INT_e || ch == INT_E) {
if ((ch | 0x20) == INT_e) { // ~ 'eE'
_intLength = outPtr;
++_inputPtr;
return _startFloat(outBuf, outPtr, ch);
Expand Down Expand Up @@ -1382,7 +1382,7 @@ protected JsonToken _startNegativeNumber() throws IOException
break;
}
if (ch > INT_9) {
if (ch == INT_e || ch == INT_E) {
if ((ch | 0x20) == INT_e) { // ~ 'eE'
_intLength = outPtr-1;
++_inputPtr;
return _startFloat(outBuf, outPtr, ch);
Expand Down Expand Up @@ -1458,7 +1458,7 @@ protected JsonToken _startPositiveNumber() throws IOException
break;
}
if (ch > INT_9) {
if (ch == INT_e || ch == INT_E) {
if ((ch | 0x20) == INT_e) { // ~ 'eE'
_intLength = outPtr-1;
++_inputPtr;
return _startFloat(outBuf, outPtr, ch);
Expand Down Expand Up @@ -1505,7 +1505,7 @@ protected JsonToken _startNumberLeadingZero() throws IOException
return _startFloat(outBuf, 1, ch);
}
} else if (ch > INT_9) {
if (ch == INT_e || ch == INT_E) {
if ((ch | 0x20) == INT_e) { // ~ 'eE'
_inputPtr = ptr;
_intLength = 1;
char[] outBuf = _textBuffer.emptyAndGetCurrentSegment();
Expand All @@ -1515,7 +1515,7 @@ protected JsonToken _startNumberLeadingZero() throws IOException
// Ok; unfortunately we have closing bracket/curly that are valid so need
// (colon not possible since this is within value, not after key)
//
if ((ch != INT_RBRACKET) && (ch != INT_RCURLY)) {
if ((ch | 0x20) != INT_RCURLY) { // ~ '}]'
--_inputPtr; // for correct error reporting
_reportUnexpectedNumberChar(ch,
"expected digit (0-9), decimal point (.) or exponent indicator (e/E) to follow '0'");
Expand Down Expand Up @@ -1608,7 +1608,7 @@ protected JsonToken _finishNumberLeadingZeroes() throws IOException
return _startFloat(outBuf, 1, ch);
}
} else if (ch > INT_9) {
if (ch == INT_e || ch == INT_E) {
if ((ch | 0x20) == INT_e) { // ~ 'eE'
char[] outBuf = _textBuffer.emptyAndGetCurrentSegment();
outBuf[0] = '0';
_intLength = 1;
Expand All @@ -1617,7 +1617,7 @@ protected JsonToken _finishNumberLeadingZeroes() throws IOException
// Ok; unfortunately we have closing bracket/curly that are valid so need
// (colon not possible since this is within value, not after key)
//
if ((ch != INT_RBRACKET) && (ch != INT_RCURLY)) {
if ((ch | 0x20) != INT_RCURLY) { // ~ '}]'
--_inputPtr; // for correct error reporting
_reportUnexpectedNumberChar(ch,
"expected digit (0-9), decimal point (.) or exponent indicator (e/E) to follow '0'");
Expand Down Expand Up @@ -1668,7 +1668,7 @@ protected JsonToken _finishNumberLeadingPosNegZeroes(final boolean negative) thr
return _startFloat(outBuf, 2, ch);
}
} else if (ch > INT_9) {
if (ch == INT_e || ch == INT_E) {
if ((ch | 0x20) == INT_e) { // ~ 'eE'
char[] outBuf = _textBuffer.emptyAndGetCurrentSegment();
outBuf[0] = negative ? '-' : '+';
outBuf[1] = '0';
Expand All @@ -1678,7 +1678,7 @@ protected JsonToken _finishNumberLeadingPosNegZeroes(final boolean negative) thr
// Ok; unfortunately we have closing bracket/curly that are valid so need
// (colon not possible since this is within value, not after key)
//
if ((ch != INT_RBRACKET) && (ch != INT_RCURLY)) {
if ((ch | 0x20) != INT_RCURLY) { // ~ '}]'
--_inputPtr; // for correct error reporting
_reportUnexpectedNumberChar(ch,
"expected digit (0-9), decimal point (.) or exponent indicator (e/E) to follow '0'");
Expand Down Expand Up @@ -1723,7 +1723,7 @@ protected JsonToken _finishNumberIntegralPart(char[] outBuf, int outPtr) throws
break;
}
if (ch > INT_9) {
if (ch == INT_e || ch == INT_E) {
if ((ch | 0x20) == INT_e) { // ~ 'eE'
_intLength = outPtr+negMod;
++_inputPtr;
return _startFloat(outBuf, outPtr, ch);
Expand Down Expand Up @@ -1779,7 +1779,7 @@ protected JsonToken _startFloat(char[] outBuf, int outPtr, int ch) throws IOExce
}
_fractLength = fractLen;
int expLen = 0;
if (ch == INT_e || ch == INT_E) { // exponent?
if ((ch | 0x20) == INT_e) { // ~ 'eE' exponent?
if (outPtr >= outBuf.length) {
outBuf = _textBuffer.expandCurrentSegment();
}
Expand Down Expand Up @@ -1855,7 +1855,7 @@ protected JsonToken _finishFloatFraction() throws IOException
return JsonToken.NOT_AVAILABLE;
}
ch = getNextSignedByteFromBuffer();
} else if (ch == 'f' || ch == 'd' || ch == 'F' || ch == 'D') {
} else if ((ch | 0x22) == 'f') { // ~ fFdD
--_inputPtr; // for correct error reporting
_reportUnexpectedNumberChar(ch, "JSON does not support parsing numbers that have 'f' or 'd' suffixes");
} else if (ch == INT_PERIOD) {
Expand All @@ -1878,7 +1878,7 @@ protected JsonToken _finishFloatFraction() throws IOException
_textBuffer.setCurrentLength(outPtr);

// Ok: end of floating point number or exponent?
if (ch == INT_e || ch == INT_E) { // exponent?
if ((ch | 0x20) == INT_e) { // ~ 'eE' exponent?
_textBuffer.append((char) ch);
_expLength = 0;
if (_inputPtr >= _inputEnd) {
Expand Down