From 8df00ed64e8c376c69cd6a08bdb2f98203f28d1b Mon Sep 17 00:00:00 2001 From: xtonik Date: Thu, 25 Jan 2024 21:18:01 +0100 Subject: [PATCH 1/3] optimized char check for 'dDfF' --- .../jackson/core/json/async/NonBlockingUtf8JsonParserBase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java b/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java index 55563b061c..61cf441a6b 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java +++ b/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java @@ -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) { From 719ab03c49a0a426cff9f61120cb25e6ca74227f Mon Sep 17 00:00:00 2001 From: xtonik Date: Thu, 25 Jan 2024 21:30:38 +0100 Subject: [PATCH 2/3] optimized char check for 'eE' --- .../core/json/ReaderBasedJsonParser.java | 8 ++++---- .../core/json/UTF8DataInputJsonParser.java | 6 +++--- .../core/json/UTF8StreamJsonParser.java | 8 ++++---- .../async/NonBlockingUtf8JsonParserBase.java | 18 +++++++++--------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java b/src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java index 18583a147a..42e61c9baa 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java @@ -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); } @@ -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); @@ -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); } @@ -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(); diff --git a/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java b/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java index 39e73dae1a..d4e700cff5 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java @@ -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); @@ -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); @@ -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; diff --git a/src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java b/src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java index 5fd157a63c..128cbeed8f 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java @@ -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) @@ -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); } @@ -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; @@ -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; diff --git a/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java b/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java index 61cf441a6b..4541ba8959 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java +++ b/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java @@ -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); @@ -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); @@ -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); @@ -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(); @@ -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; @@ -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'; @@ -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); @@ -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(); } @@ -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) { From 9bcffbb3b73e99a4c4e31fe81bf0917ccebc982e Mon Sep 17 00:00:00 2001 From: xtonik Date: Thu, 25 Jan 2024 21:40:56 +0100 Subject: [PATCH 3/3] optimized char check for ']}' --- .../jackson/core/json/ReaderBasedJsonParser.java | 12 ++++++------ .../core/json/UTF8DataInputJsonParser.java | 8 ++++---- .../jackson/core/json/UTF8StreamJsonParser.java | 6 +++--- .../async/NonBlockingUtf8JsonParserBase.java | 16 ++++++++-------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java b/src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java index 42e61c9baa..2b11db17e1 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java b/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java index d4e700cff5..625dbd2463 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java b/src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java index 128cbeed8f..458425d1b9 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java @@ -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; } @@ -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; } @@ -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; } diff --git a/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java b/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java index 4541ba8959..52aff03e4c 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java +++ b/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingUtf8JsonParserBase.java @@ -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); } @@ -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); } @@ -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); } @@ -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; @@ -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; @@ -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'"); @@ -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'"); @@ -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'");