| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | namespace System.Buffers.Text |
| | | 5 | | { |
| | | 6 | | public static partial class Utf8Parser |
| | | 7 | | { |
| | | 8 | | private static bool TryParseSByteD(ReadOnlySpan<byte> source, out sbyte value, out int bytesConsumed) |
| | | 9 | | { |
| | 0 | 10 | | if (source.Length < 1) |
| | | 11 | | goto FalseExit; |
| | | 12 | | |
| | 0 | 13 | | int sign = 1; |
| | 0 | 14 | | int index = 0; |
| | 0 | 15 | | int num = source[index]; |
| | 0 | 16 | | if (num == '-') |
| | | 17 | | { |
| | 0 | 18 | | sign = -1; |
| | 0 | 19 | | index++; |
| | 0 | 20 | | if ((uint)index >= (uint)source.Length) |
| | | 21 | | goto FalseExit; |
| | 0 | 22 | | num = source[index]; |
| | | 23 | | } |
| | 0 | 24 | | else if (num == '+') |
| | | 25 | | { |
| | 0 | 26 | | index++; |
| | 0 | 27 | | if ((uint)index >= (uint)source.Length) |
| | | 28 | | goto FalseExit; |
| | 0 | 29 | | num = source[index]; |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | int answer = 0; |
| | | 33 | | |
| | 0 | 34 | | if (ParserHelpers.IsDigit(num)) |
| | | 35 | | { |
| | 0 | 36 | | if (num == '0') |
| | | 37 | | { |
| | | 38 | | do |
| | | 39 | | { |
| | 0 | 40 | | index++; |
| | 0 | 41 | | if ((uint)index >= (uint)source.Length) |
| | | 42 | | goto Done; |
| | 0 | 43 | | num = source[index]; |
| | 0 | 44 | | } while (num == '0'); |
| | 0 | 45 | | if (!ParserHelpers.IsDigit(num)) |
| | | 46 | | goto Done; |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | answer = num - '0'; |
| | 0 | 50 | | index++; |
| | | 51 | | |
| | 0 | 52 | | if ((uint)index >= (uint)source.Length) |
| | | 53 | | goto Done; |
| | 0 | 54 | | num = source[index]; |
| | 0 | 55 | | if (!ParserHelpers.IsDigit(num)) |
| | | 56 | | goto Done; |
| | 0 | 57 | | index++; |
| | 0 | 58 | | answer = 10 * answer + num - '0'; |
| | | 59 | | |
| | | 60 | | // Potential overflow |
| | 0 | 61 | | if ((uint)index >= (uint)source.Length) |
| | | 62 | | goto Done; |
| | 0 | 63 | | num = source[index]; |
| | 0 | 64 | | if (!ParserHelpers.IsDigit(num)) |
| | | 65 | | goto Done; |
| | 0 | 66 | | index++; |
| | 0 | 67 | | answer = answer * 10 + num - '0'; |
| | | 68 | | // if sign < 0, (-1 * sign + 1) / 2 = 1 |
| | | 69 | | // else, (-1 * sign + 1) / 2 = 0 |
| | 0 | 70 | | if ((uint)answer > (uint)sbyte.MaxValue + (-1 * sign + 1) / 2) |
| | | 71 | | goto FalseExit; // Overflow |
| | | 72 | | |
| | 0 | 73 | | if ((uint)index >= (uint)source.Length) |
| | | 74 | | goto Done; |
| | 0 | 75 | | if (!ParserHelpers.IsDigit(source[index])) |
| | | 76 | | goto Done; |
| | | 77 | | |
| | | 78 | | // Guaranteed overflow |
| | | 79 | | goto FalseExit; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | FalseExit: |
| | 0 | 83 | | bytesConsumed = default; |
| | 0 | 84 | | value = default; |
| | 0 | 85 | | return false; |
| | | 86 | | |
| | | 87 | | Done: |
| | 0 | 88 | | bytesConsumed = index; |
| | 0 | 89 | | value = (sbyte)(answer * sign); |
| | 0 | 90 | | return true; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | private static bool TryParseInt16D(ReadOnlySpan<byte> source, out short value, out int bytesConsumed) |
| | | 94 | | { |
| | 0 | 95 | | if (source.Length < 1) |
| | | 96 | | goto FalseExit; |
| | | 97 | | |
| | 0 | 98 | | int sign = 1; |
| | 0 | 99 | | int index = 0; |
| | 0 | 100 | | int num = source[index]; |
| | 0 | 101 | | if (num == '-') |
| | | 102 | | { |
| | 0 | 103 | | sign = -1; |
| | 0 | 104 | | index++; |
| | 0 | 105 | | if ((uint)index >= (uint)source.Length) |
| | | 106 | | goto FalseExit; |
| | 0 | 107 | | num = source[index]; |
| | | 108 | | } |
| | 0 | 109 | | else if (num == '+') |
| | | 110 | | { |
| | 0 | 111 | | index++; |
| | 0 | 112 | | if ((uint)index >= (uint)source.Length) |
| | | 113 | | goto FalseExit; |
| | 0 | 114 | | num = source[index]; |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | int answer = 0; |
| | | 118 | | |
| | 0 | 119 | | if (ParserHelpers.IsDigit(num)) |
| | | 120 | | { |
| | 0 | 121 | | if (num == '0') |
| | | 122 | | { |
| | | 123 | | do |
| | | 124 | | { |
| | 0 | 125 | | index++; |
| | 0 | 126 | | if ((uint)index >= (uint)source.Length) |
| | | 127 | | goto Done; |
| | 0 | 128 | | num = source[index]; |
| | 0 | 129 | | } while (num == '0'); |
| | 0 | 130 | | if (!ParserHelpers.IsDigit(num)) |
| | | 131 | | goto Done; |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | answer = num - '0'; |
| | 0 | 135 | | index++; |
| | | 136 | | |
| | 0 | 137 | | if ((uint)index >= (uint)source.Length) |
| | | 138 | | goto Done; |
| | 0 | 139 | | num = source[index]; |
| | 0 | 140 | | if (!ParserHelpers.IsDigit(num)) |
| | | 141 | | goto Done; |
| | 0 | 142 | | index++; |
| | 0 | 143 | | answer = 10 * answer + num - '0'; |
| | | 144 | | |
| | 0 | 145 | | if ((uint)index >= (uint)source.Length) |
| | | 146 | | goto Done; |
| | 0 | 147 | | num = source[index]; |
| | 0 | 148 | | if (!ParserHelpers.IsDigit(num)) |
| | | 149 | | goto Done; |
| | 0 | 150 | | index++; |
| | 0 | 151 | | answer = 10 * answer + num - '0'; |
| | | 152 | | |
| | 0 | 153 | | if ((uint)index >= (uint)source.Length) |
| | | 154 | | goto Done; |
| | 0 | 155 | | num = source[index]; |
| | 0 | 156 | | if (!ParserHelpers.IsDigit(num)) |
| | | 157 | | goto Done; |
| | 0 | 158 | | index++; |
| | 0 | 159 | | answer = 10 * answer + num - '0'; |
| | | 160 | | |
| | | 161 | | // Potential overflow |
| | 0 | 162 | | if ((uint)index >= (uint)source.Length) |
| | | 163 | | goto Done; |
| | 0 | 164 | | num = source[index]; |
| | 0 | 165 | | if (!ParserHelpers.IsDigit(num)) |
| | | 166 | | goto Done; |
| | 0 | 167 | | index++; |
| | 0 | 168 | | answer = answer * 10 + num - '0'; |
| | | 169 | | // if sign < 0, (-1 * sign + 1) / 2 = 1 |
| | | 170 | | // else, (-1 * sign + 1) / 2 = 0 |
| | 0 | 171 | | if ((uint)answer > (uint)short.MaxValue + (-1 * sign + 1) / 2) |
| | | 172 | | goto FalseExit; // Overflow |
| | | 173 | | |
| | 0 | 174 | | if ((uint)index >= (uint)source.Length) |
| | | 175 | | goto Done; |
| | 0 | 176 | | if (!ParserHelpers.IsDigit(source[index])) |
| | | 177 | | goto Done; |
| | | 178 | | |
| | | 179 | | // Guaranteed overflow |
| | | 180 | | goto FalseExit; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | FalseExit: |
| | 0 | 184 | | bytesConsumed = default; |
| | 0 | 185 | | value = default; |
| | 0 | 186 | | return false; |
| | | 187 | | |
| | | 188 | | Done: |
| | 0 | 189 | | bytesConsumed = index; |
| | 0 | 190 | | value = (short)(answer * sign); |
| | 0 | 191 | | return true; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | private static bool TryParseInt32D(ReadOnlySpan<byte> source, out int value, out int bytesConsumed) |
| | | 195 | | { |
| | 0 | 196 | | if (source.Length < 1) |
| | | 197 | | goto FalseExit; |
| | | 198 | | |
| | 0 | 199 | | int sign = 1; |
| | 0 | 200 | | int index = 0; |
| | 0 | 201 | | int num = source[index]; |
| | 0 | 202 | | if (num == '-') |
| | | 203 | | { |
| | 0 | 204 | | sign = -1; |
| | 0 | 205 | | index++; |
| | 0 | 206 | | if ((uint)index >= (uint)source.Length) |
| | | 207 | | goto FalseExit; |
| | 0 | 208 | | num = source[index]; |
| | | 209 | | } |
| | 0 | 210 | | else if (num == '+') |
| | | 211 | | { |
| | 0 | 212 | | index++; |
| | 0 | 213 | | if ((uint)index >= (uint)source.Length) |
| | | 214 | | goto FalseExit; |
| | 0 | 215 | | num = source[index]; |
| | | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | int answer = 0; |
| | | 219 | | |
| | 0 | 220 | | if (ParserHelpers.IsDigit(num)) |
| | | 221 | | { |
| | 0 | 222 | | if (num == '0') |
| | | 223 | | { |
| | | 224 | | do |
| | | 225 | | { |
| | 0 | 226 | | index++; |
| | 0 | 227 | | if ((uint)index >= (uint)source.Length) |
| | | 228 | | goto Done; |
| | 0 | 229 | | num = source[index]; |
| | 0 | 230 | | } while (num == '0'); |
| | 0 | 231 | | if (!ParserHelpers.IsDigit(num)) |
| | | 232 | | goto Done; |
| | | 233 | | } |
| | | 234 | | |
| | 0 | 235 | | answer = num - '0'; |
| | 0 | 236 | | index++; |
| | | 237 | | |
| | 0 | 238 | | if ((uint)index >= (uint)source.Length) |
| | | 239 | | goto Done; |
| | 0 | 240 | | num = source[index]; |
| | 0 | 241 | | if (!ParserHelpers.IsDigit(num)) |
| | | 242 | | goto Done; |
| | 0 | 243 | | index++; |
| | 0 | 244 | | answer = 10 * answer + num - '0'; |
| | | 245 | | |
| | 0 | 246 | | if ((uint)index >= (uint)source.Length) |
| | | 247 | | goto Done; |
| | 0 | 248 | | num = source[index]; |
| | 0 | 249 | | if (!ParserHelpers.IsDigit(num)) |
| | | 250 | | goto Done; |
| | 0 | 251 | | index++; |
| | 0 | 252 | | answer = 10 * answer + num - '0'; |
| | | 253 | | |
| | 0 | 254 | | if ((uint)index >= (uint)source.Length) |
| | | 255 | | goto Done; |
| | 0 | 256 | | num = source[index]; |
| | 0 | 257 | | if (!ParserHelpers.IsDigit(num)) |
| | | 258 | | goto Done; |
| | 0 | 259 | | index++; |
| | 0 | 260 | | answer = 10 * answer + num - '0'; |
| | | 261 | | |
| | 0 | 262 | | if ((uint)index >= (uint)source.Length) |
| | | 263 | | goto Done; |
| | 0 | 264 | | num = source[index]; |
| | 0 | 265 | | if (!ParserHelpers.IsDigit(num)) |
| | | 266 | | goto Done; |
| | 0 | 267 | | index++; |
| | 0 | 268 | | answer = 10 * answer + num - '0'; |
| | | 269 | | |
| | 0 | 270 | | if ((uint)index >= (uint)source.Length) |
| | | 271 | | goto Done; |
| | 0 | 272 | | num = source[index]; |
| | 0 | 273 | | if (!ParserHelpers.IsDigit(num)) |
| | | 274 | | goto Done; |
| | 0 | 275 | | index++; |
| | 0 | 276 | | answer = 10 * answer + num - '0'; |
| | | 277 | | |
| | 0 | 278 | | if ((uint)index >= (uint)source.Length) |
| | | 279 | | goto Done; |
| | 0 | 280 | | num = source[index]; |
| | 0 | 281 | | if (!ParserHelpers.IsDigit(num)) |
| | | 282 | | goto Done; |
| | 0 | 283 | | index++; |
| | 0 | 284 | | answer = 10 * answer + num - '0'; |
| | | 285 | | |
| | 0 | 286 | | if ((uint)index >= (uint)source.Length) |
| | | 287 | | goto Done; |
| | 0 | 288 | | num = source[index]; |
| | 0 | 289 | | if (!ParserHelpers.IsDigit(num)) |
| | | 290 | | goto Done; |
| | 0 | 291 | | index++; |
| | 0 | 292 | | answer = 10 * answer + num - '0'; |
| | | 293 | | |
| | 0 | 294 | | if ((uint)index >= (uint)source.Length) |
| | | 295 | | goto Done; |
| | 0 | 296 | | num = source[index]; |
| | 0 | 297 | | if (!ParserHelpers.IsDigit(num)) |
| | | 298 | | goto Done; |
| | 0 | 299 | | index++; |
| | 0 | 300 | | answer = 10 * answer + num - '0'; |
| | | 301 | | |
| | | 302 | | // Potential overflow |
| | 0 | 303 | | if ((uint)index >= (uint)source.Length) |
| | | 304 | | goto Done; |
| | 0 | 305 | | num = source[index]; |
| | 0 | 306 | | if (!ParserHelpers.IsDigit(num)) |
| | | 307 | | goto Done; |
| | 0 | 308 | | index++; |
| | 0 | 309 | | if (answer > int.MaxValue / 10) |
| | | 310 | | goto FalseExit; // Overflow |
| | 0 | 311 | | answer = answer * 10 + num - '0'; |
| | | 312 | | // if sign < 0, (-1 * sign + 1) / 2 = 1 |
| | | 313 | | // else, (-1 * sign + 1) / 2 = 0 |
| | 0 | 314 | | if ((uint)answer > (uint)int.MaxValue + (-1 * sign + 1) / 2) |
| | | 315 | | goto FalseExit; // Overflow |
| | | 316 | | |
| | 0 | 317 | | if ((uint)index >= (uint)source.Length) |
| | | 318 | | goto Done; |
| | 0 | 319 | | if (!ParserHelpers.IsDigit(source[index])) |
| | | 320 | | goto Done; |
| | | 321 | | |
| | | 322 | | // Guaranteed overflow |
| | | 323 | | goto FalseExit; |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | FalseExit: |
| | 0 | 327 | | bytesConsumed = default; |
| | 0 | 328 | | value = default; |
| | 0 | 329 | | return false; |
| | | 330 | | |
| | | 331 | | Done: |
| | 0 | 332 | | bytesConsumed = index; |
| | 0 | 333 | | value = answer * sign; |
| | 0 | 334 | | return true; |
| | | 335 | | } |
| | | 336 | | |
| | | 337 | | private static bool TryParseInt64D(ReadOnlySpan<byte> source, out long value, out int bytesConsumed) |
| | | 338 | | { |
| | 0 | 339 | | long sign = 0; // 0 if the value is positive, -1 if the value is negative |
| | 0 | 340 | | int idx = 0; |
| | | 341 | | |
| | | 342 | | // We use 'nuint' for the firstChar and nextChar data types in this method because |
| | | 343 | | // it gives us a free early zero-extension to 64 bits when running on a 64-bit platform. |
| | | 344 | | |
| | | 345 | | nuint firstChar; |
| | | 346 | | while (true) |
| | | 347 | | { |
| | 0 | 348 | | if ((uint)idx >= (uint)source.Length) { goto FalseExit; } |
| | 0 | 349 | | firstChar = (uint)source[idx] - '0'; |
| | 0 | 350 | | if ((uint)firstChar <= 9) { break; } |
| | | 351 | | |
| | | 352 | | // We saw something that wasn't a digit. If it's a '+' or a '-', |
| | | 353 | | // we'll set the 'sign' value appropriately and resume the "read |
| | | 354 | | // first char" loop from the next index. If this loops more than |
| | | 355 | | // once (idx != 0), it means we saw a sign character followed by |
| | | 356 | | // a non-digit character, which should be considered an error. |
| | | 357 | | |
| | 0 | 358 | | if (idx != 0) |
| | | 359 | | { |
| | | 360 | | goto FalseExit; |
| | | 361 | | } |
| | | 362 | | |
| | 0 | 363 | | idx++; |
| | | 364 | | |
| | 0 | 365 | | if ((uint)firstChar == unchecked((uint)('-' - '0'))) |
| | | 366 | | { |
| | 0 | 367 | | sign--; // set to -1 |
| | | 368 | | } |
| | 0 | 369 | | else if ((uint)firstChar != unchecked((uint)('+' - '0'))) |
| | | 370 | | { |
| | 0 | 371 | | goto FalseExit; // not a digit, not '-', and not '+'; fail |
| | | 372 | | } |
| | | 373 | | } |
| | | 374 | | |
| | 0 | 375 | | ulong parsedValue = firstChar; |
| | 0 | 376 | | int overflowLength = ParserHelpers.Int64OverflowLength + idx; // +idx to account for any sign char we read |
| | 0 | 377 | | idx++; |
| | | 378 | | |
| | | 379 | | // At this point, we successfully read a single digit character. |
| | | 380 | | // The only failure condition from here on out is integer overflow. |
| | | 381 | | |
| | 0 | 382 | | if (source.Length < overflowLength) |
| | | 383 | | { |
| | | 384 | | // If the input span is short enough such that integer overflow isn't an issue, |
| | | 385 | | // don't bother performing overflow checks. Just keep shifting in new digits |
| | | 386 | | // until we see a non-digit character or until we've exhausted our input buffer. |
| | | 387 | | |
| | 0 | 388 | | while (true) |
| | | 389 | | { |
| | 0 | 390 | | if ((uint)idx >= (uint)source.Length) { break; } // EOF |
| | 0 | 391 | | nuint nextChar = (uint)source[idx] - '0'; |
| | 0 | 392 | | if ((uint)nextChar > 9) { break; } // not a digit |
| | 0 | 393 | | parsedValue = parsedValue * 10 + nextChar; |
| | 0 | 394 | | idx++; |
| | | 395 | | } |
| | | 396 | | } |
| | | 397 | | else |
| | | 398 | | { |
| | 0 | 399 | | while (true) |
| | | 400 | | { |
| | 0 | 401 | | if ((uint)idx >= (uint)source.Length) { break; } // EOF |
| | 0 | 402 | | nuint nextChar = (uint)source[idx] - '0'; |
| | 0 | 403 | | if ((uint)nextChar > 9) { break; } // not a digit |
| | 0 | 404 | | idx++; |
| | | 405 | | |
| | | 406 | | // The const below is the smallest unsigned x for which "x * 10 + 9" |
| | | 407 | | // might overflow long.MaxValue. If the current accumulator is below |
| | | 408 | | // this const, there's no risk of overflowing. |
| | | 409 | | |
| | | 410 | | const ulong OverflowRisk = 0x0CCC_CCCC_CCCC_CCCCul; |
| | | 411 | | |
| | 0 | 412 | | if (parsedValue < OverflowRisk) |
| | | 413 | | { |
| | 0 | 414 | | parsedValue = parsedValue * 10 + nextChar; |
| | 0 | 415 | | continue; |
| | | 416 | | } |
| | | 417 | | |
| | | 418 | | // If the current accumulator is exactly equal to the const above, |
| | | 419 | | // then "accumulator * 10 + 7" is the highest we can go without overflowing |
| | | 420 | | // long.MaxValue. (If we know the value is negative, we can instead allow |
| | | 421 | | // +8, since the range of negative numbers is one higher than the range of |
| | | 422 | | // positive numbers.) This also implies that if the current accumulator |
| | | 423 | | // is higher than the const above, there's no hope that we'll succeed, |
| | | 424 | | // so we may as well just fail now. |
| | | 425 | | // |
| | | 426 | | // The (nextChar + sign) trick below works because sign is 0 or -1, |
| | | 427 | | // so if sign is -1 then this actually checks that nextChar > 8. |
| | | 428 | | // n.b. signed arithmetic below because nextChar may be 0. |
| | | 429 | | |
| | 0 | 430 | | if (parsedValue != OverflowRisk || (int)nextChar + (int)sign > 7) |
| | | 431 | | { |
| | | 432 | | goto FalseExit; |
| | | 433 | | } |
| | | 434 | | |
| | 0 | 435 | | parsedValue = OverflowRisk * 10 + nextChar; |
| | | 436 | | } |
| | | 437 | | } |
| | | 438 | | |
| | | 439 | | // 'sign' is 0 for non-negative and -1 for negative. This allows us to perform |
| | | 440 | | // cheap arithmetic + bitwise operations to mimic a multiplication by 1 or -1 |
| | | 441 | | // without incurring the cost of an actual multiplication operation. |
| | | 442 | | // |
| | | 443 | | // If sign = 0, this becomes value = (parsedValue ^ 0) - 0 = parsedValue |
| | | 444 | | // If sign = -1, this becomes value = (parsedValue ^ -1) - (-1) = ~parsedValue + 1 = -parsedValue |
| | | 445 | | |
| | 0 | 446 | | bytesConsumed = idx; |
| | 0 | 447 | | value = ((long)parsedValue ^ sign) - sign; |
| | 0 | 448 | | return true; |
| | | 449 | | |
| | | 450 | | FalseExit: |
| | 0 | 451 | | bytesConsumed = 0; |
| | 0 | 452 | | value = default; |
| | 0 | 453 | | return false; |
| | | 454 | | } |
| | | 455 | | } |
| | | 456 | | } |