| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,36 +33,41 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |||
| 33 | 33 | ||
| 34 | 34 | import java.io.IOException; | |
| 35 | 35 | ||
| 36 | + // TODO: make separate implementation for streaming and non-streaming | ||
| 36 | 37 | class IterImplNumber { | |
| 37 | 38 | ||
| 38 | 39 | final static int[] digits = new int[256]; | |
| 39 | - final static int[] zeroToNineDigits = new int[256]; | ||
| 40 | - final static int END_OF_NUMBER = -2; | ||
| 41 | - final static int DOT_IN_NUMBER = -3; | ||
| 42 | - final static int INVALID_CHAR_FOR_NUMBER = -1; | ||
| 40 | + private final static int[] intDigits = new int[256]; | ||
| 41 | + private final static int[] floatDigits = new int[256]; | ||
| 42 | + private final static int END_OF_NUMBER = -2; | ||
| 43 | + private final static int DOT_IN_NUMBER = -3; | ||
| 44 | + private final static int INVALID_CHAR_FOR_NUMBER = -1; | ||
| 43 | 45 | private static final int POW10[] = {1, 10, 100, 1000, 10000, 100000, 1000000}; | |
| 44 | - private final static long SAFE_TO_MULTIPLY_10 = (Long.MAX_VALUE / 10) - 10; | ||
| 46 | + private final static long LONG_SAFE_TO_MULTIPLY_10 = (Long.MAX_VALUE / 10) - 10; | ||
| 47 | + private final static int INT_SAFE_TO_MULTIPLY_10 = (Integer.MAX_VALUE / 10) - 10; | ||
| 45 | 48 | ||
| 46 | 49 | static { | |
| 47 | 50 | for (int i = 0; i < digits.length; i++) { | |
| 48 | 51 | digits[i] = INVALID_CHAR_FOR_NUMBER; | |
| 49 | - zeroToNineDigits[i] = INVALID_CHAR_FOR_NUMBER; | ||
| 52 | + floatDigits[i] = INVALID_CHAR_FOR_NUMBER; | ||
| 53 | + intDigits[i] = INVALID_CHAR_FOR_NUMBER; | ||
| 50 | 54 | } | |
| 51 | 55 | for (int i = '0'; i <= '9'; ++i) { | |
| 52 | 56 | digits[i] = (i - '0'); | |
| 53 | - zeroToNineDigits[i] = (i - '0'); | ||
| 57 | + floatDigits[i] = (i - '0'); | ||
| 58 | + intDigits[i] = (i - '0'); | ||
| 54 | 59 | } | |
| 55 | 60 | for (int i = 'a'; i <= 'f'; ++i) { | |
| 56 | 61 | digits[i] = ((i - 'a') + 10); | |
| 57 | 62 | } | |
| 58 | 63 | for (int i = 'A'; i <= 'F'; ++i) { | |
| 59 | 64 | digits[i] = ((i - 'A') + 10); | |
| 60 | 65 | } | |
| 61 | - zeroToNineDigits[','] = END_OF_NUMBER; | ||
| 62 | - zeroToNineDigits[']'] = END_OF_NUMBER; | ||
| 63 | - zeroToNineDigits['}'] = END_OF_NUMBER; | ||
| 64 | - zeroToNineDigits[' '] = END_OF_NUMBER; | ||
| 65 | - zeroToNineDigits['.'] = DOT_IN_NUMBER; | ||
| 66 | + floatDigits[','] = END_OF_NUMBER; | ||
| 67 | + floatDigits[']'] = END_OF_NUMBER; | ||
| 68 | + floatDigits['}'] = END_OF_NUMBER; | ||
| 69 | + floatDigits[' '] = END_OF_NUMBER; | ||
| 70 | + floatDigits['.'] = DOT_IN_NUMBER; | ||
| 66 | 71 | } | |
| 67 | 72 | ||
| 68 | 73 | public static final double readDouble(final JsonIterator iter) throws IOException { | |
@@ -82,7 +87,7 @@ private static final double readPositiveDouble(final JsonIterator iter) throws I | |||
| 82 | 87 | non_decimal_loop: | |
| 83 | 88 | for (; i < iter.tail; i++) { | |
| 84 | 89 | c = iter.buf[i]; | |
| 85 | - final int ind = zeroToNineDigits[c]; | ||
| 90 | + final int ind = floatDigits[c]; | ||
| 86 | 91 | switch (ind) { | |
| 87 | 92 | case INVALID_CHAR_FOR_NUMBER: | |
| 88 | 93 | return readDoubleSlowPath(iter); | |
@@ -92,7 +97,7 @@ private static final double readPositiveDouble(final JsonIterator iter) throws I | |||
| 92 | 97 | case DOT_IN_NUMBER: | |
| 93 | 98 | break non_decimal_loop; | |
| 94 | 99 | } | |
| 95 | - if (value > SAFE_TO_MULTIPLY_10) { | ||
| 100 | + if (value > LONG_SAFE_TO_MULTIPLY_10) { | ||
| 96 | 101 | return readDoubleSlowPath(iter); | |
| 97 | 102 | } | |
| 98 | 103 | value = (value << 3) + (value << 1) + ind; // value = value * 10 + ind; | |
@@ -102,7 +107,7 @@ private static final double readPositiveDouble(final JsonIterator iter) throws I | |||
| 102 | 107 | int decimalPlaces = 0; | |
| 103 | 108 | for (; i < iter.tail; i++) { | |
| 104 | 109 | c = iter.buf[i]; | |
| 105 | - final int ind = zeroToNineDigits[c]; | ||
| 110 | + final int ind = floatDigits[c]; | ||
| 106 | 111 | switch (ind) { | |
| 107 | 112 | case END_OF_NUMBER: | |
| 108 | 113 | if (decimalPlaces > 0 && decimalPlaces < POW10.length) { | |
@@ -116,7 +121,7 @@ private static final double readPositiveDouble(final JsonIterator iter) throws I | |||
| 116 | 121 | return readDoubleSlowPath(iter); | |
| 117 | 122 | } | |
| 118 | 123 | decimalPlaces++; | |
| 119 | - if (value > SAFE_TO_MULTIPLY_10) { | ||
| 124 | + if (value > LONG_SAFE_TO_MULTIPLY_10) { | ||
| 120 | 125 | return readDoubleSlowPath(iter); | |
| 121 | 126 | } | |
| 122 | 127 | value = (value << 3) + (value << 1) + ind; // value = value * 10 + ind; | |
@@ -150,7 +155,7 @@ private static final float readPositiveFloat(final JsonIterator iter) throws IOE | |||
| 150 | 155 | non_decimal_loop: | |
| 151 | 156 | for (; i < iter.tail; i++) { | |
| 152 | 157 | c = iter.buf[i]; | |
| 153 | - final int ind = zeroToNineDigits[c]; | ||
| 158 | + final int ind = floatDigits[c]; | ||
| 154 | 159 | switch (ind) { | |
| 155 | 160 | case INVALID_CHAR_FOR_NUMBER: | |
| 156 | 161 | return readFloatSlowPath(iter); | |
@@ -160,7 +165,7 @@ private static final float readPositiveFloat(final JsonIterator iter) throws IOE | |||
| 160 | 165 | case DOT_IN_NUMBER: | |
| 161 | 166 | break non_decimal_loop; | |
| 162 | 167 | } | |
| 163 | - if (value > SAFE_TO_MULTIPLY_10) { | ||
| 168 | + if (value > LONG_SAFE_TO_MULTIPLY_10) { | ||
| 164 | 169 | return readFloatSlowPath(iter); | |
| 165 | 170 | } | |
| 166 | 171 | value = (value << 3) + (value << 1) + ind; // value = value * 10 + ind; | |
@@ -170,7 +175,7 @@ private static final float readPositiveFloat(final JsonIterator iter) throws IOE | |||
| 170 | 175 | int decimalPlaces = 0; | |
| 171 | 176 | for (; i < iter.tail; i++) { | |
| 172 | 177 | c = iter.buf[i]; | |
| 173 | - final int ind = zeroToNineDigits[c]; | ||
| 178 | + final int ind = floatDigits[c]; | ||
| 174 | 179 | switch (ind) { | |
| 175 | 180 | case END_OF_NUMBER: | |
| 176 | 181 | if (decimalPlaces > 0 && decimalPlaces < POW10.length) { | |
@@ -184,7 +189,7 @@ private static final float readPositiveFloat(final JsonIterator iter) throws IOE | |||
| 184 | 189 | return readFloatSlowPath(iter); | |
| 185 | 190 | } | |
| 186 | 191 | decimalPlaces++; | |
| 187 | - if (value > SAFE_TO_MULTIPLY_10) { | ||
| 192 | + if (value > LONG_SAFE_TO_MULTIPLY_10) { | ||
| 188 | 193 | return readFloatSlowPath(iter); | |
| 189 | 194 | } | |
| 190 | 195 | value = (value << 3) + (value << 1) + ind; // value = value * 10 + ind; | |
@@ -239,37 +244,39 @@ public static final String readNumber(final JsonIterator iter) throws IOExceptio | |||
| 239 | 244 | } | |
| 240 | 245 | } | |
| 241 | 246 | ||
| 242 | - public static final int readInt(JsonIterator iter) throws IOException { | ||
| 247 | + public static final int readInt(final JsonIterator iter) throws IOException { | ||
| 243 | 248 | byte c = IterImpl.nextToken(iter); | |
| 244 | 249 | if (c == '-') { | |
| 245 | - return -readUnsignedInt(iter); | ||
| 250 | + return -readUnsignedInt(iter, IterImpl.readByte(iter)); | ||
| 246 | 251 | } else { | |
| 247 | - iter.unreadByte(); | ||
| 248 | - return readUnsignedInt(iter); | ||
| 252 | + return readUnsignedInt(iter, c); | ||
| 249 | 253 | } | |
| 250 | 254 | } | |
| 251 | 255 | ||
| 252 | - public static final int readUnsignedInt(JsonIterator iter) throws IOException { | ||
| 253 | - // TODO: throw overflow | ||
| 254 | - byte c = IterImpl.readByte(iter); | ||
| 255 | - int v = digits[c]; | ||
| 256 | - if (v == 0) { | ||
| 256 | + public static final int readUnsignedInt(final JsonIterator iter, byte c) throws IOException { | ||
| 257 | + int result = intDigits[c]; | ||
| 258 | + if (result == 0) { | ||
| 257 | 259 | return 0; | |
| 258 | 260 | } | |
| 259 | - if (v == -1) { | ||
| 261 | + if (result == INVALID_CHAR_FOR_NUMBER) { | ||
| 260 | 262 | throw iter.reportError("readUnsignedInt", "expect 0~9"); | |
| 261 | 263 | } | |
| 262 | - int result = 0; | ||
| 263 | - for (; ; ) { | ||
| 264 | - result = result * 10 + v; | ||
| 265 | - c = IterImpl.readByte(iter); | ||
| 266 | - v = digits[c]; | ||
| 267 | - if (v == -1) { | ||
| 268 | - iter.unreadByte(); | ||
| 269 | - break; | ||
| 264 | + for (;;) { | ||
| 265 | + for (int i = iter.head; i < iter.tail; i++) { | ||
| 266 | + int ind = intDigits[iter.buf[i]]; | ||
| 267 | + if (ind == INVALID_CHAR_FOR_NUMBER) { | ||
| 268 | + iter.head = i; | ||
| 269 | + return result; | ||
| 270 | + } | ||
| 271 | + if (result > INT_SAFE_TO_MULTIPLY_10) { | ||
| 272 | + throw iter.reportError("readUnsignedInt", "value is too large for int"); | ||
| 273 | + } | ||
| 274 | + result = (result << 3) + (result << 1) + ind; | ||
| 275 | + } | ||
| 276 | + if (!IterImpl.loadMore(iter)) { | ||
| 277 | + return result; | ||
| 270 | 278 | } | |
| 271 | 279 | } | |
| 272 | - return result; | ||
| 273 | 280 | } | |
| 274 | 281 | ||
| 275 | 282 | public static final long readLong(JsonIterator iter) throws IOException { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -168,7 +168,7 @@ public final short readShort() throws IOException { | |||
| 168 | 168 | if (Short.MIN_VALUE <= v && v <= Short.MAX_VALUE) { | |
| 169 | 169 | return (short) v; | |
| 170 | 170 | } else { | |
| 171 | - throw new JsonException("short overflow: " + v); | ||
| 171 | + throw reportError("readShort", "short overflow: " + v); | ||
| 172 | 172 | } | |
| 173 | 173 | } | |
| 174 | 174 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,43 @@ | |||
| 1 | + package com.jsoniter; | ||
| 2 | + | ||
| 3 | + import com.jsoniter.spi.JsonException; | ||
| 4 | + import junit.framework.TestCase; | ||
| 5 | + import org.junit.experimental.categories.Category; | ||
| 6 | + | ||
| 7 | + import java.io.ByteArrayInputStream; | ||
| 8 | + import java.io.IOException; | ||
| 9 | + | ||
| 10 | + public class TestInteger extends TestCase { | ||
| 11 | + | ||
| 12 | + private boolean isStreaming; | ||
| 13 | + | ||
| 14 | + public void test_positive_negative() throws IOException { | ||
| 15 | + assertEquals(4321, parseInt("4321")); | ||
| 16 | + assertEquals(-4321, parseInt("-4321")); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + public void test_large_number() throws IOException { | ||
| 20 | + try { | ||
| 21 | + parseInt("123456789123456789"); | ||
| 22 | + fail(); | ||
| 23 | + } catch (JsonException e) { | ||
| 24 | + } | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + @Category(StreamingCategory.class) | ||
| 28 | + public void test_streaming() throws IOException { | ||
| 29 | + isStreaming = true; | ||
| 30 | + test_positive_negative(); | ||
| 31 | + test_large_number(); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + private int parseInt(String input) throws IOException { | ||
| 35 | + if (isStreaming) { | ||
| 36 | + JsonIterator iter = JsonIterator.parse(new ByteArrayInputStream(input.getBytes()), 2); | ||
| 37 | + return iter.readInt(); | ||
| 38 | + } else { | ||
| 39 | + JsonIterator iter = JsonIterator.parse(input); | ||
| 40 | + return iter.readInt(); | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments