| // After editing this file, run "go generate" in the parent directory. |
| |
| // Copyright 2020 The Wuffs Authors. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| // ---------------- String Conversions |
| |
| // wuffs_base__parse_number__foo_digits entries are 0x00 for invalid digits, |
| // and (0x80 | v) for valid digits, where v is the 4 bit value. |
| |
| static const uint8_t wuffs_base__parse_number__decimal_digits[256] = { |
| // 0 1 2 3 4 5 6 7 |
| // 8 9 A B C D E F |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F. |
| 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x30 ..= 0x37. '0'-'7'. |
| 0x88, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. '8'-'9'. |
| |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 ..= 0x47. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60 ..= 0x67. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F. |
| |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 ..= 0xBF. |
| |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF. |
| // 0 1 2 3 4 5 6 7 |
| // 8 9 A B C D E F |
| }; |
| |
| static const uint8_t wuffs_base__parse_number__hexadecimal_digits[256] = { |
| // 0 1 2 3 4 5 6 7 |
| // 8 9 A B C D E F |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F. |
| 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x30 ..= 0x37. '0'-'7'. |
| 0x88, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. '8'-'9'. |
| |
| 0x00, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x00, // 0x40 ..= 0x47. 'A'-'F'. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F. |
| 0x00, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x00, // 0x60 ..= 0x67. 'a'-'f'. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F. |
| |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 ..= 0xBF. |
| |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7. |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF. |
| // 0 1 2 3 4 5 6 7 |
| // 8 9 A B C D E F |
| }; |
| |
| // -------- |
| |
| wuffs_base__result_i64 // |
| wuffs_base__parse_number_i64(wuffs_base__slice_u8 s) { |
| uint8_t* p = s.ptr; |
| uint8_t* q = s.ptr + s.len; |
| |
| for (; (p < q) && (*p == '_'); p++) { |
| } |
| |
| bool negative = false; |
| if (p >= q) { |
| goto fail_bad_argument; |
| } else if (*p == '-') { |
| p++; |
| negative = true; |
| } else if (*p == '+') { |
| p++; |
| } |
| |
| wuffs_base__result_u64 r = |
| wuffs_base__parse_number_u64(wuffs_base__make_slice_u8(p, q - p)); |
| if (r.status.repr != NULL) { |
| wuffs_base__result_i64 ret; |
| ret.status.repr = r.status.repr; |
| ret.value = 0; |
| return ret; |
| } else if (negative) { |
| if (r.value > 0x8000000000000000) { |
| goto fail_out_of_bounds; |
| } |
| wuffs_base__result_i64 ret; |
| ret.status.repr = NULL; |
| ret.value = -(int64_t)(r.value); |
| return ret; |
| } else if (r.value > 0x7FFFFFFFFFFFFFFF) { |
| goto fail_out_of_bounds; |
| } else { |
| wuffs_base__result_i64 ret; |
| ret.status.repr = NULL; |
| ret.value = +(int64_t)(r.value); |
| return ret; |
| } |
| |
| fail_bad_argument: |
| do { |
| wuffs_base__result_i64 ret; |
| ret.status.repr = wuffs_base__error__bad_argument; |
| ret.value = 0; |
| return ret; |
| } while (0); |
| |
| fail_out_of_bounds: |
| do { |
| wuffs_base__result_i64 ret; |
| ret.status.repr = wuffs_base__error__out_of_bounds; |
| ret.value = 0; |
| return ret; |
| } while (0); |
| } |
| |
| wuffs_base__result_u64 // |
| wuffs_base__parse_number_u64(wuffs_base__slice_u8 s) { |
| uint8_t* p = s.ptr; |
| uint8_t* q = s.ptr + s.len; |
| |
| for (; (p < q) && (*p == '_'); p++) { |
| } |
| |
| if (p >= q) { |
| goto fail_bad_argument; |
| |
| } else if (*p == '0') { |
| p++; |
| if (p >= q) { |
| goto ok_zero; |
| } |
| if (*p == '_') { |
| p++; |
| for (; p < q; p++) { |
| if (*p != '_') { |
| goto fail_bad_argument; |
| } |
| } |
| goto ok_zero; |
| } |
| |
| if ((*p == 'x') || (*p == 'X')) { |
| p++; |
| for (; (p < q) && (*p == '_'); p++) { |
| } |
| if (p < q) { |
| goto hexadecimal; |
| } |
| |
| } else if ((*p == 'd') || (*p == 'D')) { |
| p++; |
| for (; (p < q) && (*p == '_'); p++) { |
| } |
| if (p < q) { |
| goto decimal; |
| } |
| } |
| |
| goto fail_bad_argument; |
| } |
| |
| decimal: |
| do { |
| uint64_t v = wuffs_base__parse_number__decimal_digits[*p++]; |
| if (v == 0) { |
| goto fail_bad_argument; |
| } |
| v &= 0x0F; |
| |
| // UINT64_MAX is 18446744073709551615, which is ((10 * max10) + max1). |
| const uint64_t max10 = 1844674407370955161; |
| const uint8_t max1 = 5; |
| |
| for (; p < q; p++) { |
| if (*p == '_') { |
| continue; |
| } |
| uint8_t digit = wuffs_base__parse_number__decimal_digits[*p]; |
| if (digit == 0) { |
| goto fail_bad_argument; |
| } |
| digit &= 0x0F; |
| if ((v > max10) || ((v == max10) && (digit > max1))) { |
| goto fail_out_of_bounds; |
| } |
| v = (10 * v) + ((uint64_t)(digit)); |
| } |
| |
| wuffs_base__result_u64 ret; |
| ret.status.repr = NULL; |
| ret.value = v; |
| return ret; |
| } while (0); |
| |
| hexadecimal: |
| do { |
| uint64_t v = wuffs_base__parse_number__hexadecimal_digits[*p++]; |
| if (v == 0) { |
| goto fail_bad_argument; |
| } |
| v &= 0x0F; |
| |
| for (; p < q; p++) { |
| if (*p == '_') { |
| continue; |
| } |
| uint8_t digit = wuffs_base__parse_number__hexadecimal_digits[*p]; |
| if (digit == 0) { |
| goto fail_bad_argument; |
| } |
| digit &= 0x0F; |
| if ((v >> 60) != 0) { |
| goto fail_out_of_bounds; |
| } |
| v = (v << 4) | ((uint64_t)(digit)); |
| } |
| |
| wuffs_base__result_u64 ret; |
| ret.status.repr = NULL; |
| ret.value = v; |
| return ret; |
| } while (0); |
| |
| ok_zero: |
| do { |
| wuffs_base__result_u64 ret; |
| ret.status.repr = NULL; |
| ret.value = 0; |
| return ret; |
| } while (0); |
| |
| fail_bad_argument: |
| do { |
| wuffs_base__result_u64 ret; |
| ret.status.repr = wuffs_base__error__bad_argument; |
| ret.value = 0; |
| return ret; |
| } while (0); |
| |
| fail_out_of_bounds: |
| do { |
| wuffs_base__result_u64 ret; |
| ret.status.repr = wuffs_base__error__out_of_bounds; |
| ret.value = 0; |
| return ret; |
| } while (0); |
| } |