| | | 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 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | namespace System.Buffers.Text |
| | | 6 | | { |
| | | 7 | | public static partial class Utf8Parser |
| | | 8 | | { |
| | | 9 | | private static bool TryParseSByteD(ReadOnlySpan<byte> text, out sbyte value, out int bytesConsumed) |
| | 1 | 10 | | { |
| | 1 | 11 | | if (text.Length < 1) |
| | 1 | 12 | | goto FalseExit; |
| | | 13 | | |
| | 1 | 14 | | int sign = 1; |
| | 1 | 15 | | int index = 0; |
| | 1 | 16 | | int num = text[index]; |
| | 1 | 17 | | if (num == '-') |
| | 1 | 18 | | { |
| | 1 | 19 | | sign = -1; |
| | 1 | 20 | | index++; |
| | 1 | 21 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 22 | | goto FalseExit; |
| | 1 | 23 | | num = text[index]; |
| | 1 | 24 | | } |
| | 1 | 25 | | else if (num == '+') |
| | 1 | 26 | | { |
| | 1 | 27 | | index++; |
| | 1 | 28 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 29 | | goto FalseExit; |
| | 1 | 30 | | num = text[index]; |
| | 1 | 31 | | } |
| | | 32 | | |
| | 1 | 33 | | int answer = 0; |
| | | 34 | | |
| | 1 | 35 | | if (ParserHelpers.IsDigit(num)) |
| | 1 | 36 | | { |
| | 1 | 37 | | if (num == '0') |
| | 1 | 38 | | { |
| | | 39 | | do |
| | 1 | 40 | | { |
| | 1 | 41 | | index++; |
| | 1 | 42 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 43 | | goto Done; |
| | 1 | 44 | | num = text[index]; |
| | 2 | 45 | | } while (num == '0'); |
| | 1 | 46 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 47 | | goto Done; |
| | 1 | 48 | | } |
| | | 49 | | |
| | 1 | 50 | | answer = num - '0'; |
| | 1 | 51 | | index++; |
| | | 52 | | |
| | 1 | 53 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 54 | | goto Done; |
| | 1 | 55 | | num = text[index]; |
| | 1 | 56 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 57 | | goto Done; |
| | 1 | 58 | | index++; |
| | 1 | 59 | | answer = 10 * answer + num - '0'; |
| | | 60 | | |
| | | 61 | | // Potential overflow |
| | 1 | 62 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 63 | | goto Done; |
| | 1 | 64 | | num = text[index]; |
| | 1 | 65 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 66 | | goto Done; |
| | 1 | 67 | | index++; |
| | 1 | 68 | | answer = answer * 10 + num - '0'; |
| | | 69 | | // if sign < 0, (-1 * sign + 1) / 2 = 1 |
| | | 70 | | // else, (-1 * sign + 1) / 2 = 0 |
| | 1 | 71 | | if ((uint)answer > (uint)sbyte.MaxValue + (-1 * sign + 1) / 2) |
| | 1 | 72 | | goto FalseExit; // Overflow |
| | | 73 | | |
| | 1 | 74 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 75 | | goto Done; |
| | 1 | 76 | | if (!ParserHelpers.IsDigit(text[index])) |
| | 1 | 77 | | goto Done; |
| | | 78 | | |
| | | 79 | | // Guaranteed overflow |
| | 1 | 80 | | goto FalseExit; |
| | | 81 | | } |
| | | 82 | | |
| | 1 | 83 | | FalseExit: |
| | 1 | 84 | | bytesConsumed = default; |
| | 1 | 85 | | value = default; |
| | 1 | 86 | | return false; |
| | | 87 | | |
| | 1 | 88 | | Done: |
| | 1 | 89 | | bytesConsumed = index; |
| | 1 | 90 | | value = (sbyte)(answer * sign); |
| | 1 | 91 | | return true; |
| | 1 | 92 | | } |
| | | 93 | | |
| | | 94 | | private static bool TryParseInt16D(ReadOnlySpan<byte> text, out short value, out int bytesConsumed) |
| | 1 | 95 | | { |
| | 1 | 96 | | if (text.Length < 1) |
| | 1 | 97 | | goto FalseExit; |
| | | 98 | | |
| | 1 | 99 | | int sign = 1; |
| | 1 | 100 | | int index = 0; |
| | 1 | 101 | | int num = text[index]; |
| | 1 | 102 | | if (num == '-') |
| | 1 | 103 | | { |
| | 1 | 104 | | sign = -1; |
| | 1 | 105 | | index++; |
| | 1 | 106 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 107 | | goto FalseExit; |
| | 1 | 108 | | num = text[index]; |
| | 1 | 109 | | } |
| | 1 | 110 | | else if (num == '+') |
| | 1 | 111 | | { |
| | 1 | 112 | | index++; |
| | 1 | 113 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 114 | | goto FalseExit; |
| | 1 | 115 | | num = text[index]; |
| | 1 | 116 | | } |
| | | 117 | | |
| | 1 | 118 | | int answer = 0; |
| | | 119 | | |
| | 1 | 120 | | if (ParserHelpers.IsDigit(num)) |
| | 1 | 121 | | { |
| | 1 | 122 | | if (num == '0') |
| | 1 | 123 | | { |
| | | 124 | | do |
| | 1 | 125 | | { |
| | 1 | 126 | | index++; |
| | 1 | 127 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 128 | | goto Done; |
| | 1 | 129 | | num = text[index]; |
| | 2 | 130 | | } while (num == '0'); |
| | 1 | 131 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 132 | | goto Done; |
| | 1 | 133 | | } |
| | | 134 | | |
| | 1 | 135 | | answer = num - '0'; |
| | 1 | 136 | | index++; |
| | | 137 | | |
| | 1 | 138 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 139 | | goto Done; |
| | 1 | 140 | | num = text[index]; |
| | 1 | 141 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 142 | | goto Done; |
| | 1 | 143 | | index++; |
| | 1 | 144 | | answer = 10 * answer + num - '0'; |
| | | 145 | | |
| | 1 | 146 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 147 | | goto Done; |
| | 1 | 148 | | num = text[index]; |
| | 1 | 149 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 150 | | goto Done; |
| | 1 | 151 | | index++; |
| | 1 | 152 | | answer = 10 * answer + num - '0'; |
| | | 153 | | |
| | 1 | 154 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 155 | | goto Done; |
| | 1 | 156 | | num = text[index]; |
| | 1 | 157 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 158 | | goto Done; |
| | 1 | 159 | | index++; |
| | 1 | 160 | | answer = 10 * answer + num - '0'; |
| | | 161 | | |
| | | 162 | | // Potential overflow |
| | 1 | 163 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 164 | | goto Done; |
| | 1 | 165 | | num = text[index]; |
| | 1 | 166 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 167 | | goto Done; |
| | 1 | 168 | | index++; |
| | 1 | 169 | | answer = answer * 10 + num - '0'; |
| | | 170 | | // if sign < 0, (-1 * sign + 1) / 2 = 1 |
| | | 171 | | // else, (-1 * sign + 1) / 2 = 0 |
| | 1 | 172 | | if ((uint)answer > (uint)short.MaxValue + (-1 * sign + 1) / 2) |
| | 1 | 173 | | goto FalseExit; // Overflow |
| | | 174 | | |
| | 1 | 175 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 176 | | goto Done; |
| | 1 | 177 | | if (!ParserHelpers.IsDigit(text[index])) |
| | 1 | 178 | | goto Done; |
| | | 179 | | |
| | | 180 | | // Guaranteed overflow |
| | 1 | 181 | | goto FalseExit; |
| | | 182 | | } |
| | | 183 | | |
| | 1 | 184 | | FalseExit: |
| | 1 | 185 | | bytesConsumed = default; |
| | 1 | 186 | | value = default; |
| | 1 | 187 | | return false; |
| | | 188 | | |
| | 1 | 189 | | Done: |
| | 1 | 190 | | bytesConsumed = index; |
| | 1 | 191 | | value = (short)(answer * sign); |
| | 1 | 192 | | return true; |
| | 1 | 193 | | } |
| | | 194 | | |
| | | 195 | | private static bool TryParseInt32D(ReadOnlySpan<byte> text, out int value, out int bytesConsumed) |
| | 1 | 196 | | { |
| | 1 | 197 | | if (text.Length < 1) |
| | 1 | 198 | | goto FalseExit; |
| | | 199 | | |
| | 1 | 200 | | int sign = 1; |
| | 1 | 201 | | int index = 0; |
| | 1 | 202 | | int num = text[index]; |
| | 1 | 203 | | if (num == '-') |
| | 1 | 204 | | { |
| | 1 | 205 | | sign = -1; |
| | 1 | 206 | | index++; |
| | 1 | 207 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 208 | | goto FalseExit; |
| | 1 | 209 | | num = text[index]; |
| | 1 | 210 | | } |
| | 1 | 211 | | else if (num == '+') |
| | 1 | 212 | | { |
| | 1 | 213 | | index++; |
| | 1 | 214 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 215 | | goto FalseExit; |
| | 1 | 216 | | num = text[index]; |
| | 1 | 217 | | } |
| | | 218 | | |
| | 1 | 219 | | int answer = 0; |
| | | 220 | | |
| | 1 | 221 | | if (ParserHelpers.IsDigit(num)) |
| | 1 | 222 | | { |
| | 1 | 223 | | if (num == '0') |
| | 1 | 224 | | { |
| | | 225 | | do |
| | 1 | 226 | | { |
| | 1 | 227 | | index++; |
| | 1 | 228 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 229 | | goto Done; |
| | 1 | 230 | | num = text[index]; |
| | 2 | 231 | | } while (num == '0'); |
| | 1 | 232 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 233 | | goto Done; |
| | 1 | 234 | | } |
| | | 235 | | |
| | 1 | 236 | | answer = num - '0'; |
| | 1 | 237 | | index++; |
| | | 238 | | |
| | 1 | 239 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 240 | | goto Done; |
| | 1 | 241 | | num = text[index]; |
| | 1 | 242 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 243 | | goto Done; |
| | 1 | 244 | | index++; |
| | 1 | 245 | | answer = 10 * answer + num - '0'; |
| | | 246 | | |
| | 1 | 247 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 248 | | goto Done; |
| | 1 | 249 | | num = text[index]; |
| | 1 | 250 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 251 | | goto Done; |
| | 1 | 252 | | index++; |
| | 1 | 253 | | answer = 10 * answer + num - '0'; |
| | | 254 | | |
| | 1 | 255 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 256 | | goto Done; |
| | 1 | 257 | | num = text[index]; |
| | 1 | 258 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 259 | | goto Done; |
| | 1 | 260 | | index++; |
| | 1 | 261 | | answer = 10 * answer + num - '0'; |
| | | 262 | | |
| | 1 | 263 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 264 | | goto Done; |
| | 1 | 265 | | num = text[index]; |
| | 1 | 266 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 267 | | goto Done; |
| | 1 | 268 | | index++; |
| | 1 | 269 | | answer = 10 * answer + num - '0'; |
| | | 270 | | |
| | 1 | 271 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 272 | | goto Done; |
| | 1 | 273 | | num = text[index]; |
| | 1 | 274 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 275 | | goto Done; |
| | 1 | 276 | | index++; |
| | 1 | 277 | | answer = 10 * answer + num - '0'; |
| | | 278 | | |
| | 1 | 279 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 280 | | goto Done; |
| | 1 | 281 | | num = text[index]; |
| | 1 | 282 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 283 | | goto Done; |
| | 1 | 284 | | index++; |
| | 1 | 285 | | answer = 10 * answer + num - '0'; |
| | | 286 | | |
| | 1 | 287 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 288 | | goto Done; |
| | 1 | 289 | | num = text[index]; |
| | 1 | 290 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 291 | | goto Done; |
| | 1 | 292 | | index++; |
| | 1 | 293 | | answer = 10 * answer + num - '0'; |
| | | 294 | | |
| | 1 | 295 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 296 | | goto Done; |
| | 1 | 297 | | num = text[index]; |
| | 1 | 298 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 299 | | goto Done; |
| | 1 | 300 | | index++; |
| | 1 | 301 | | answer = 10 * answer + num - '0'; |
| | | 302 | | |
| | | 303 | | // Potential overflow |
| | 1 | 304 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 305 | | goto Done; |
| | 1 | 306 | | num = text[index]; |
| | 1 | 307 | | if (!ParserHelpers.IsDigit(num)) |
| | 1 | 308 | | goto Done; |
| | 1 | 309 | | index++; |
| | 1 | 310 | | if (answer > int.MaxValue / 10) |
| | 1 | 311 | | goto FalseExit; // Overflow |
| | 1 | 312 | | answer = answer * 10 + num - '0'; |
| | | 313 | | // if sign < 0, (-1 * sign + 1) / 2 = 1 |
| | | 314 | | // else, (-1 * sign + 1) / 2 = 0 |
| | 1 | 315 | | if ((uint)answer > (uint)int.MaxValue + (-1 * sign + 1) / 2) |
| | 1 | 316 | | goto FalseExit; // Overflow |
| | | 317 | | |
| | 1 | 318 | | if ((uint)index >= (uint)text.Length) |
| | 1 | 319 | | goto Done; |
| | 1 | 320 | | if (!ParserHelpers.IsDigit(text[index])) |
| | 1 | 321 | | goto Done; |
| | | 322 | | |
| | | 323 | | // Guaranteed overflow |
| | 1 | 324 | | goto FalseExit; |
| | | 325 | | } |
| | | 326 | | |
| | 1 | 327 | | FalseExit: |
| | 1 | 328 | | bytesConsumed = default; |
| | 1 | 329 | | value = default; |
| | 1 | 330 | | return false; |
| | | 331 | | |
| | 1 | 332 | | Done: |
| | 1 | 333 | | bytesConsumed = index; |
| | 1 | 334 | | value = answer * sign; |
| | 1 | 335 | | return true; |
| | 1 | 336 | | } |
| | | 337 | | |
| | | 338 | | private static bool TryParseInt64D(ReadOnlySpan<byte> text, out long value, out int bytesConsumed) |
| | 1 | 339 | | { |
| | 1 | 340 | | if (text.Length < 1) |
| | 1 | 341 | | { |
| | 1 | 342 | | bytesConsumed = 0; |
| | 1 | 343 | | value = default; |
| | 1 | 344 | | return false; |
| | | 345 | | } |
| | | 346 | | |
| | 1 | 347 | | int indexOfFirstDigit = 0; |
| | 1 | 348 | | int sign = 1; |
| | 1 | 349 | | if (text[0] == '-') |
| | 1 | 350 | | { |
| | 1 | 351 | | indexOfFirstDigit = 1; |
| | 1 | 352 | | sign = -1; |
| | | 353 | | |
| | 1 | 354 | | if (text.Length <= indexOfFirstDigit) |
| | 1 | 355 | | { |
| | 1 | 356 | | bytesConsumed = 0; |
| | 1 | 357 | | value = default; |
| | 1 | 358 | | return false; |
| | | 359 | | } |
| | 1 | 360 | | } |
| | 1 | 361 | | else if (text[0] == '+') |
| | 1 | 362 | | { |
| | 1 | 363 | | indexOfFirstDigit = 1; |
| | | 364 | | |
| | 1 | 365 | | if (text.Length <= indexOfFirstDigit) |
| | 1 | 366 | | { |
| | 1 | 367 | | bytesConsumed = 0; |
| | 1 | 368 | | value = default; |
| | 1 | 369 | | return false; |
| | | 370 | | } |
| | 1 | 371 | | } |
| | | 372 | | |
| | 1 | 373 | | int overflowLength = ParserHelpers.Int64OverflowLength + indexOfFirstDigit; |
| | | 374 | | |
| | | 375 | | // Parse the first digit separately. If invalid here, we need to return false. |
| | 1 | 376 | | long firstDigit = text[indexOfFirstDigit] - 48; // '0' |
| | 1 | 377 | | if (firstDigit < 0 || firstDigit > 9) |
| | 1 | 378 | | { |
| | 1 | 379 | | bytesConsumed = 0; |
| | 1 | 380 | | value = default; |
| | 1 | 381 | | return false; |
| | | 382 | | } |
| | 1 | 383 | | ulong parsedValue = (ulong)firstDigit; |
| | | 384 | | |
| | 1 | 385 | | if (text.Length < overflowLength) |
| | 1 | 386 | | { |
| | | 387 | | // Length is less than Parsers.Int64OverflowLength; overflow is not possible |
| | 3 | 388 | | for (int index = indexOfFirstDigit + 1; index < text.Length; index++) |
| | 1 | 389 | | { |
| | 1 | 390 | | long nextDigit = text[index] - 48; // '0' |
| | 1 | 391 | | if (nextDigit < 0 || nextDigit > 9) |
| | 1 | 392 | | { |
| | 1 | 393 | | bytesConsumed = index; |
| | 1 | 394 | | value = ((long)parsedValue) * sign; |
| | 1 | 395 | | return true; |
| | | 396 | | } |
| | 1 | 397 | | parsedValue = parsedValue * 10 + (ulong)nextDigit; |
| | 1 | 398 | | } |
| | 1 | 399 | | } |
| | | 400 | | else |
| | 1 | 401 | | { |
| | | 402 | | // Length is greater than Parsers.Int64OverflowLength; overflow is only possible after Parsers.Int64Over |
| | | 403 | | // digits. There may be no overflow after Parsers.Int64OverflowLength if there are leading zeroes. |
| | 3 | 404 | | for (int index = indexOfFirstDigit + 1; index < overflowLength - 1; index++) |
| | 1 | 405 | | { |
| | 1 | 406 | | long nextDigit = text[index] - 48; // '0' |
| | 1 | 407 | | if (nextDigit < 0 || nextDigit > 9) |
| | 1 | 408 | | { |
| | 1 | 409 | | bytesConsumed = index; |
| | 1 | 410 | | value = ((long)parsedValue) * sign; |
| | 1 | 411 | | return true; |
| | | 412 | | } |
| | 1 | 413 | | parsedValue = parsedValue * 10 + (ulong)nextDigit; |
| | 1 | 414 | | } |
| | 3 | 415 | | for (int index = overflowLength - 1; index < text.Length; index++) |
| | 1 | 416 | | { |
| | 1 | 417 | | long nextDigit = text[index] - 48; // '0' |
| | 1 | 418 | | if (nextDigit < 0 || nextDigit > 9) |
| | 1 | 419 | | { |
| | 1 | 420 | | bytesConsumed = index; |
| | 1 | 421 | | value = ((long)parsedValue) * sign; |
| | 1 | 422 | | return true; |
| | | 423 | | } |
| | | 424 | | // If parsedValue > (long.MaxValue / 10), any more appended digits will cause overflow. |
| | | 425 | | // if parsedValue == (long.MaxValue / 10), any nextDigit greater than 7 or 8 (depending on sign) imp |
| | 1 | 426 | | bool positive = sign > 0; |
| | 1 | 427 | | bool nextDigitTooLarge = nextDigit > 8 || (positive && nextDigit > 7); |
| | 1 | 428 | | if (parsedValue > long.MaxValue / 10 || parsedValue == long.MaxValue / 10 && nextDigitTooLarge) |
| | 1 | 429 | | { |
| | 1 | 430 | | bytesConsumed = 0; |
| | 1 | 431 | | value = default; |
| | 1 | 432 | | return false; |
| | | 433 | | } |
| | 1 | 434 | | parsedValue = parsedValue * 10 + (ulong)nextDigit; |
| | 1 | 435 | | } |
| | 1 | 436 | | } |
| | | 437 | | |
| | 1 | 438 | | bytesConsumed = text.Length; |
| | 1 | 439 | | value = ((long)parsedValue) * sign; |
| | 1 | 440 | | return true; |
| | 1 | 441 | | } |
| | | 442 | | } |
| | | 443 | | } |