diff --git a/internal/cgen/base/all-impl.c b/internal/cgen/base/all-impl.c index cd54fe67..62dae8ab 100644 --- a/internal/cgen/base/all-impl.c +++ b/internal/cgen/base/all-impl.c @@ -144,6 +144,15 @@ const uint32_t wuffs_base__pixel_format__bits_per_channel[16] = { // defined(WUFFS_CONFIG__MODULE__BASE) || // defined(WUFFS_CONFIG__MODULE__BASE__F64CONV) +#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) || \ + defined(WUFFS_CONFIG__MODULE__BASE__I64CONV) + +// !! INSERT base/i64conv-submodule.c. + +#endif // !defined(WUFFS_CONFIG__MODULES) || + // defined(WUFFS_CONFIG__MODULE__BASE) || + // defined(WUFFS_CONFIG__MODULE__BASE__I64CONV) + #if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) || \ defined(WUFFS_CONFIG__MODULE__BASE__PIXCONV) diff --git a/internal/cgen/base/i64conv-submodule.c b/internal/cgen/base/i64conv-submodule.c new file mode 100644 index 00000000..e3a3f5db --- /dev/null +++ b/internal/cgen/base/i64conv-submodule.c @@ -0,0 +1,437 @@ +// 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. + +// ---------------- Integer + +// 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__MAYBE_STATIC 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++; + } + + do { + wuffs_base__result_u64 r = wuffs_base__parse_number_u64( + wuffs_base__make_slice_u8(p, (size_t)(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; + } + } while (0); + +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__MAYBE_STATIC 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 = 1844674407370955161u; + 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); +} + +// -------- + +// wuffs_base__render_number__first_hundred contains the decimal encodings of +// the first one hundred numbers [0 ..= 99]. +static const uint8_t wuffs_base__render_number__first_hundred[200] = { + '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', // + '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', // + '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', // + '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', // + '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', // + '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', // + '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', // + '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', // + '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', // + '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', // + '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', // + '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', // + '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', // + '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', // + '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', // + '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', // + '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', // + '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', // + '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', // + '9', '5', '9', '6', '9', '7', '9', '8', '9', '9', // +}; + +static size_t // +wuffs_base__private_implementation__render_number_u64(wuffs_base__slice_u8 dst, + uint64_t x, + uint32_t options, + bool neg) { + uint8_t buf[WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL]; + uint8_t* ptr = &buf[0] + sizeof(buf); + + while (x >= 100) { + size_t index = (x % 100) * 2; + x /= 100; + uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0]; + uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1]; + ptr -= 2; + ptr[0] = s0; + ptr[1] = s1; + } + + if (x < 10) { + ptr -= 1; + ptr[0] = '0' + x; + } else { + size_t index = x * 2; + uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0]; + uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1]; + ptr -= 2; + ptr[0] = s0; + ptr[1] = s1; + } + + if (neg) { + ptr -= 1; + ptr[0] = '-'; + } else if (options & WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN) { + ptr -= 1; + ptr[0] = '+'; + } + + size_t n = sizeof(buf) - (ptr - &buf[0]); + if (n > dst.len) { + return 0; + } + memcpy(dst.ptr + ((options & WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT) + ? (dst.len - n) + : 0), + ptr, n); + return n; +} + +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__render_number_i64(wuffs_base__slice_u8 dst, + int64_t x, + uint32_t options) { + uint64_t u = x; + bool neg = x < 0; + if (neg) { + u = 1 + ~u; + } + return wuffs_base__private_implementation__render_number_u64(dst, u, options, + neg); +} + +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__render_number_u64(wuffs_base__slice_u8 dst, + uint64_t x, + uint32_t options) { + return wuffs_base__private_implementation__render_number_u64(dst, x, options, + false); +} + +// ---------------- Hexadecimal + +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__hexadecimal__decode2(wuffs_base__slice_u8 dst, + wuffs_base__slice_u8 src) { + size_t src_len2 = src.len / 2; + size_t len = dst.len < src_len2 ? dst.len : src_len2; + uint8_t* d = dst.ptr; + uint8_t* s = src.ptr; + size_t n = len; + + while (n--) { + *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[0]] << 4) | + (wuffs_base__parse_number__hexadecimal_digits[s[1]] & 0x0F)); + d += 1; + s += 2; + } + + return len; +} + +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst, + wuffs_base__slice_u8 src) { + size_t src_len4 = src.len / 4; + size_t len = dst.len < src_len4 ? dst.len : src_len4; + uint8_t* d = dst.ptr; + uint8_t* s = src.ptr; + size_t n = len; + + while (n--) { + *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[2]] << 4) | + (wuffs_base__parse_number__hexadecimal_digits[s[3]] & 0x0F)); + d += 1; + s += 4; + } + + return len; +} diff --git a/internal/cgen/base/strconv-impl.c b/internal/cgen/base/strconv-impl.c index afc5a913..8fa31310 100644 --- a/internal/cgen/base/strconv-impl.c +++ b/internal/cgen/base/strconv-impl.c @@ -14,428 +14,6 @@ // 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__MAYBE_STATIC 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++; - } - - do { - wuffs_base__result_u64 r = wuffs_base__parse_number_u64( - wuffs_base__make_slice_u8(p, (size_t)(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; - } - } while (0); - -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__MAYBE_STATIC 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 = 1844674407370955161u; - 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); -} - -// -------- - -// wuffs_base__render_number__first_hundred contains the decimal encodings of -// the first one hundred numbers [0 ..= 99]. -static const uint8_t wuffs_base__render_number__first_hundred[200] = { - '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', // - '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', // - '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', // - '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', // - '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', // - '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', // - '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', // - '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', // - '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', // - '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', // - '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', // - '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', // - '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', // - '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', // - '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', // - '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', // - '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', // - '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', // - '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', // - '9', '5', '9', '6', '9', '7', '9', '8', '9', '9', // -}; - -static size_t // -wuffs_base__private_implementation__render_number_u64(wuffs_base__slice_u8 dst, - uint64_t x, - uint32_t options, - bool neg) { - uint8_t buf[WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL]; - uint8_t* ptr = &buf[0] + sizeof(buf); - - while (x >= 100) { - size_t index = (x % 100) * 2; - x /= 100; - uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0]; - uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1]; - ptr -= 2; - ptr[0] = s0; - ptr[1] = s1; - } - - if (x < 10) { - ptr -= 1; - ptr[0] = '0' + x; - } else { - size_t index = x * 2; - uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0]; - uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1]; - ptr -= 2; - ptr[0] = s0; - ptr[1] = s1; - } - - if (neg) { - ptr -= 1; - ptr[0] = '-'; - } else if (options & WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN) { - ptr -= 1; - ptr[0] = '+'; - } - - size_t n = sizeof(buf) - (ptr - &buf[0]); - if (n > dst.len) { - return 0; - } - memcpy(dst.ptr + ((options & WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT) - ? (dst.len - n) - : 0), - ptr, n); - return n; -} - -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__render_number_i64(wuffs_base__slice_u8 dst, - int64_t x, - uint32_t options) { - uint64_t u = x; - bool neg = x < 0; - if (neg) { - u = 1 + ~u; - } - return wuffs_base__private_implementation__render_number_u64(dst, u, options, - neg); -} - -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__render_number_u64(wuffs_base__slice_u8 dst, - uint64_t x, - uint32_t options) { - return wuffs_base__private_implementation__render_number_u64(dst, x, options, - false); -} - -// ---------------- Hexadecimal - -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__hexadecimal__decode2(wuffs_base__slice_u8 dst, - wuffs_base__slice_u8 src) { - size_t src_len2 = src.len / 2; - size_t len = dst.len < src_len2 ? dst.len : src_len2; - uint8_t* d = dst.ptr; - uint8_t* s = src.ptr; - size_t n = len; - - while (n--) { - *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[0]] << 4) | - (wuffs_base__parse_number__hexadecimal_digits[s[1]] & 0x0F)); - d += 1; - s += 2; - } - - return len; -} - -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst, - wuffs_base__slice_u8 src) { - size_t src_len4 = src.len / 4; - size_t len = dst.len < src_len4 ? dst.len : src_len4; - uint8_t* d = dst.ptr; - uint8_t* s = src.ptr; - size_t n = len; - - while (n--) { - *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[2]] << 4) | - (wuffs_base__parse_number__hexadecimal_digits[s[3]] & 0x0F)); - d += 1; - s += 4; - } - - return len; -} - // ---------------- Unicode and UTF-8 WUFFS_BASE__MAYBE_STATIC size_t // diff --git a/internal/cgen/base/strconv-public.h b/internal/cgen/base/strconv-public.h index 098dd05b..4f80c2a9 100644 --- a/internal/cgen/base/strconv-public.h +++ b/internal/cgen/base/strconv-public.h @@ -16,43 +16,20 @@ // ---------------- String Conversions -// wuffs_base__parse_number_i64 parses the ASCII integer in s. For example, if -// s contains the bytes "-123" then it will return the int64_t -123. -// -// It returns an error if s does not contain an integer or if the integer -// within would overflow an int64_t. -// -// It is similar to wuffs_base__parse_number_u64 but it returns a signed -// integer, not an unsigned integer. It also allows a leading '+' or '-'. -WUFFS_BASE__MAYBE_STATIC wuffs_base__result_i64 // -wuffs_base__parse_number_i64(wuffs_base__slice_u8 s); +// Options (bitwise or'ed together) for wuffs_base__render_number_etc +// functions. -// wuffs_base__parse_number_u64 parses the ASCII integer in s. For example, if -// s contains the bytes "123" then it will return the uint64_t 123. -// -// It returns an error if s does not contain an integer or if the integer -// within would overflow a uint64_t. -// -// It is similar to the C standard library's strtoull function, but: -// - Errors are returned in-band (in a result type), not out-of-band (errno). -// - It takes a slice (a pointer and length), not a NUL-terminated C string. -// - It does not take an optional endptr argument. It does not allow a partial -// parse: it returns an error unless all of s is consumed. -// - It does not allow whitespace, leading or otherwise. -// - It does not allow a leading '+' or '-'. -// - It does not allow unnecessary leading zeroes ("0" is valid and its sole -// zero is necessary). All of "00", "0644" and "007" are invalid. -// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it -// always accepts both decimal (e.g "1234", "0d5678") and hexadecimal (e.g. -// "0x9aBC"). The caller is responsible for prior filtering of e.g. hex -// numbers if they are unwanted. For example, Wuffs' JSON decoder will only -// produce a wuffs_base__token for decimal numbers, not hexadecimal. -// - It is not affected by i18n / l10n settings such as environment variables. -// - It does allow arbitrary underscores, except inside the optional 2-byte -// opening "0d" or "0X" that denotes base-10 or base-16. For example, -// "__0D_1_002" would successfully parse as "one thousand and two". -WUFFS_BASE__MAYBE_STATIC wuffs_base__result_u64 // -wuffs_base__parse_number_u64(wuffs_base__slice_u8 s); +#define WUFFS_BASE__RENDER_NUMBER__DEFAULT_OPTIONS ((uint32_t)0x00000000) + +// WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT means to render to the right side +// (higher indexes) of the destination slice, leaving any untouched bytes on +// the left side (lower indexes). The default is vice versa: rendering on the +// left with slack on the right. +#define WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT ((uint32_t)0x00000001) + +// WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN means to render the unnecessary +// leading "+" for non-negative numbers: "+0" and "+123", not "0" and "123". +#define WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN ((uint32_t)0x00000002) // ---------------- IEEE 754 Floating Point @@ -97,50 +74,6 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s); // -------- -// Options (bitwise or'ed together) for wuffs_base__render_number_etc -// functions. - -#define WUFFS_BASE__RENDER_NUMBER__DEFAULT_OPTIONS ((uint32_t)0x00000000) - -// WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT means to render to the right side -// (higher indexes) of the destination slice, leaving any untouched bytes on -// the left side (lower indexes). The default is vice versa: rendering on the -// left with slack on the right. -#define WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT ((uint32_t)0x00000001) - -// WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN means to render the unnecessary -// leading "+" for non-negative numbers: "+0" and "+123", not "0" and "123". -#define WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN ((uint32_t)0x00000002) - -// -------- - -#define WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL 20 -#define WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL 21 - -// wuffs_base__render_number_i64 writes the decimal encoding of x to dst and -// returns the number of bytes written. If dst is shorter than the entire -// encoding, it returns 0 (and no bytes are written). -// -// dst will never be too short if its length is at least 20, also known as -// WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL. -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__render_number_i64(wuffs_base__slice_u8 dst, - int64_t x, - uint32_t options); - -// wuffs_base__render_number_u64 writes the decimal encoding of x to dst and -// returns the number of bytes written. If dst is shorter than the entire -// encoding, it returns 0 (and no bytes are written). -// -// dst will never be too short if its length is at least 21, also known as -// WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL. -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__render_number_u64(wuffs_base__slice_u8 dst, - uint64_t x, - uint32_t options); - -// -------- - // wuffs_base__ieee_754_bit_representation__etc converts between a double // precision numerical value and its IEEE 754 64-bit representation (1 sign // bit, 11 exponent bits, 52 explicit significand bits). @@ -170,6 +103,73 @@ wuffs_base__ieee_754_bit_representation__to_f64(uint64_t u) { return f; } +// ---------------- Integer + +// wuffs_base__parse_number_i64 parses the ASCII integer in s. For example, if +// s contains the bytes "-123" then it will return the int64_t -123. +// +// It returns an error if s does not contain an integer or if the integer +// within would overflow an int64_t. +// +// It is similar to wuffs_base__parse_number_u64 but it returns a signed +// integer, not an unsigned integer. It also allows a leading '+' or '-'. +WUFFS_BASE__MAYBE_STATIC wuffs_base__result_i64 // +wuffs_base__parse_number_i64(wuffs_base__slice_u8 s); + +// wuffs_base__parse_number_u64 parses the ASCII integer in s. For example, if +// s contains the bytes "123" then it will return the uint64_t 123. +// +// It returns an error if s does not contain an integer or if the integer +// within would overflow a uint64_t. +// +// It is similar to the C standard library's strtoull function, but: +// - Errors are returned in-band (in a result type), not out-of-band (errno). +// - It takes a slice (a pointer and length), not a NUL-terminated C string. +// - It does not take an optional endptr argument. It does not allow a partial +// parse: it returns an error unless all of s is consumed. +// - It does not allow whitespace, leading or otherwise. +// - It does not allow a leading '+' or '-'. +// - It does not allow unnecessary leading zeroes ("0" is valid and its sole +// zero is necessary). All of "00", "0644" and "007" are invalid. +// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it +// always accepts both decimal (e.g "1234", "0d5678") and hexadecimal (e.g. +// "0x9aBC"). The caller is responsible for prior filtering of e.g. hex +// numbers if they are unwanted. For example, Wuffs' JSON decoder will only +// produce a wuffs_base__token for decimal numbers, not hexadecimal. +// - It is not affected by i18n / l10n settings such as environment variables. +// - It does allow arbitrary underscores, except inside the optional 2-byte +// opening "0d" or "0X" that denotes base-10 or base-16. For example, +// "__0D_1_002" would successfully parse as "one thousand and two". +WUFFS_BASE__MAYBE_STATIC wuffs_base__result_u64 // +wuffs_base__parse_number_u64(wuffs_base__slice_u8 s); + +// -------- + +#define WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL 20 +#define WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL 21 + +// wuffs_base__render_number_i64 writes the decimal encoding of x to dst and +// returns the number of bytes written. If dst is shorter than the entire +// encoding, it returns 0 (and no bytes are written). +// +// dst will never be too short if its length is at least 20, also known as +// WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL. +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__render_number_i64(wuffs_base__slice_u8 dst, + int64_t x, + uint32_t options); + +// wuffs_base__render_number_u64 writes the decimal encoding of x to dst and +// returns the number of bytes written. If dst is shorter than the entire +// encoding, it returns 0 (and no bytes are written). +// +// dst will never be too short if its length is at least 21, also known as +// WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL. +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__render_number_u64(wuffs_base__slice_u8 dst, + uint64_t x, + uint32_t options); + // ---------------- Hexadecimal // wuffs_base__hexadecimal__decode2 converts "6A6b" to "jk", where e.g. 'j' is diff --git a/internal/cgen/cgen.go b/internal/cgen/cgen.go index 9b4ce960..f5e7edcb 100644 --- a/internal/cgen/cgen.go +++ b/internal/cgen/cgen.go @@ -97,6 +97,7 @@ const ( var BaseSubModules = []string{ "core", "f64conv", + "i64conv", "interfaces", "pixconv", } @@ -125,6 +126,7 @@ func Do(args []string) error { "// !! INSERT base/all-public.h.\n": insertBaseAllPublicH, "// !! INSERT base/copyright\n": insertBaseCopyright, "// !! INSERT base/f64conv-submodule.c.\n": insertBaseF64ConvSubmoduleC, + "// !! INSERT base/i64conv-submodule.c.\n": insertBaseI64ConvSubmoduleC, "// !! INSERT base/pixconv-submodule.c.\n": insertBasePixConvSubmoduleC, "// !! INSERT base/strconv-impl.c.\n": insertBaseStrConvImplC, "// !! INSERT vtable names.\n": func(b *buffer) error { @@ -343,6 +345,11 @@ func insertBaseF64ConvSubmoduleC(buf *buffer) error { return nil } +func insertBaseI64ConvSubmoduleC(buf *buffer) error { + buf.writes(data.BaseI64ConvSubmoduleC) + return nil +} + func insertBasePixConvSubmoduleC(buf *buffer) error { buf.writes(data.BasePixConvSubmoduleC) return nil diff --git a/internal/cgen/data/data.go b/internal/cgen/data/data.go index 469c2f72..130903bb 100644 --- a/internal/cgen/data/data.go +++ b/internal/cgen/data/data.go @@ -24,31 +24,11 @@ const BaseAllImplC = "" + "// ----------------\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) || \\\n defined(WUFFS_CONFIG__MODULE__BASE__CORE)\n\nconst uint8_t wuffs_base__low_bits_mask__u8[9] = {\n 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF,\n};\n\nconst uint16_t wuffs_base__low_bits_mask__u16[17] = {\n 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF,\n 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF,\n};\n\nconst uint32_t wuffs_base__low_bits_mask__u32[33] = {\n 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x0000001F,\n 0x0000003F, 0x0000007F, 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,\n 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF, 0x0001FFFF,\n 0x0003FFFF, 0x0007FFFF, 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,\n 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, 0x1FFFFFFF,\n 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF,\n};\n\nconst uint64_t wuffs_base__low_bits_mask__u64[65] = {\n 0x0000000000000000, 0x000" + "0000000000001, 0x0000000000000003,\n 0x0000000000000007, 0x000000000000000F, 0x000000000000001F,\n 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,\n 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF,\n 0x0000000000000FFF, 0x0000000000001FFF, 0x0000000000003FFF,\n 0x0000000000007FFF, 0x000000000000FFFF, 0x000000000001FFFF,\n 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,\n 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF,\n 0x0000000000FFFFFF, 0x0000000001FFFFFF, 0x0000000003FFFFFF,\n 0x0000000007FFFFFF, 0x000000000FFFFFFF, 0x000000001FFFFFFF,\n 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,\n 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF,\n 0x0000000FFFFFFFFF, 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF,\n 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF, 0x000001FFFFFFFFFF,\n 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,\n 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF,\n 0x0000FFFFFFFFFFFF, 0x000" + "1FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF,\n 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF, 0x001FFFFFFFFFFFFF,\n 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,\n 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF,\n 0x0FFFFFFFFFFFFFFF, 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF,\n 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,\n};\n\nconst uint32_t wuffs_base__pixel_format__bits_per_channel[16] = {\n 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,\n 0x08, 0x0A, 0x0C, 0x10, 0x18, 0x20, 0x30, 0x40,\n};\n\n// !! INSERT wuffs_base__status strings.\n\n// !! INSERT vtable names.\n\n// !! INSERT base/strconv-impl.c.\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE) ||\n // defined(WUFFS_CONFIG__MODULE__BASE__CORE)\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) || \\\n defined(WUFFS_CONFIG__MODULE__BASE__INTERFACES)\n\n// !! INSERT InterfaceDefinitions.\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE_" + - "_BASE) ||\n // defined(WUFFS_CONFIG__MODULE__BASE__INTERFACES)\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) || \\\n defined(WUFFS_CONFIG__MODULE__BASE__F64CONV)\n\n// !! INSERT base/f64conv-submodule.c.\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE) ||\n // defined(WUFFS_CONFIG__MODULE__BASE__F64CONV)\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) || \\\n defined(WUFFS_CONFIG__MODULE__BASE__PIXCONV)\n\n// !! INSERT base/pixconv-submodule.c.\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE) ||\n // defined(WUFFS_CONFIG__MODULE__BASE__PIXCONV)\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#endif // WUFFS_IMPLEMENTATION\n\n// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING BELOW.\n\n#endif // WUFFS_INCLUDE_GUARD__BASE\n" + + "_BASE) ||\n // defined(WUFFS_CONFIG__MODULE__BASE__INTERFACES)\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) || \\\n defined(WUFFS_CONFIG__MODULE__BASE__F64CONV)\n\n// !! INSERT base/f64conv-submodule.c.\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE) ||\n // defined(WUFFS_CONFIG__MODULE__BASE__F64CONV)\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) || \\\n defined(WUFFS_CONFIG__MODULE__BASE__I64CONV)\n\n// !! INSERT base/i64conv-submodule.c.\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE) ||\n // defined(WUFFS_CONFIG__MODULE__BASE__I64CONV)\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) || \\\n defined(WUFFS_CONFIG__MODULE__BASE__PIXCONV)\n\n// !! INSERT base/pixconv-submodule.c.\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE) ||\n // defined(WUFFS_CONFIG__MODULE__B" + + "ASE__PIXCONV)\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#endif // WUFFS_IMPLEMENTATION\n\n// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING BELOW.\n\n#endif // WUFFS_INCLUDE_GUARD__BASE\n" + "" const BaseStrConvImplC = "" + - "// ---------------- String Conversions\n\n// wuffs_base__parse_number__foo_digits entries are 0x00 for invalid digits,\n// and (0x80 | v) for valid digits, where v is the 4 bit value.\n\nstatic const uint8_t wuffs_base__parse_number__decimal_digits[256] = {\n // 0 1 2 3 4 5 6 7\n // 8 9 A B C D E F\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F.\n 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x30 ..= 0x37. '0'-'7'.\n 0x88, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. '8'-'9'.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 ..= 0x47.\n 0x00, 0" + - "x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60 ..= 0x67.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, " + - "0x00, 0x00, // 0xB8 ..= 0xBF.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.\n // 0 1 2 3 4 5 6 7\n // 8 9 A B C D E F\n};\n\nstatic const uint8_t wuffs_base__parse_number__hexadecimal_digits[256] = {\n // 0 1 2 3 4 5 6 7\n // 8 9 A B C D E F\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F." + - "\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F.\n 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x30 ..= 0x37. '0'-'7'.\n 0x88, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. '8'-'9'.\n\n 0x00, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x00, // 0x40 ..= 0x47. 'A'-'F'.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F.\n 0x00, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x00, // 0x60 ..= 0x67. 'a'-'f'.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 .." + - "= 0x7F.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 ..= 0xBF.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.\n 0x00, 0x00, 0x00" + - ", 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.\n // 0 1 2 3 4 5 6 7\n // 8 9 A B C D E F\n};\n\n" + - "" + - "// --------\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_i64 //\nwuffs_base__parse_number_i64(wuffs_base__slice_u8 s) {\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n for (; (p < q) && (*p == '_'); p++) {\n }\n\n bool negative = false;\n if (p >= q) {\n goto fail_bad_argument;\n } else if (*p == '-') {\n p++;\n negative = true;\n } else if (*p == '+') {\n p++;\n }\n\n do {\n wuffs_base__result_u64 r = wuffs_base__parse_number_u64(\n wuffs_base__make_slice_u8(p, (size_t)(q - p)));\n if (r.status.repr != NULL) {\n wuffs_base__result_i64 ret;\n ret.status.repr = r.status.repr;\n ret.value = 0;\n return ret;\n } else if (negative) {\n if (r.value > 0x8000000000000000) {\n goto fail_out_of_bounds;\n }\n wuffs_base__result_i64 ret;\n ret.status.repr = NULL;\n ret.value = -(int64_t)(r.value);\n return ret;\n } else if (r.value > 0x7FFFFFFFFFFFFFFF) {\n goto fail_out_of_bounds;\n } else {\n wuffs_base__result_i64 ret;\n ret.status" + - ".repr = NULL;\n ret.value = +(int64_t)(r.value);\n return ret;\n }\n } while (0);\n\nfail_bad_argument:\n do {\n wuffs_base__result_i64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_out_of_bounds:\n do {\n wuffs_base__result_i64 ret;\n ret.status.repr = wuffs_base__error__out_of_bounds;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_u64 //\nwuffs_base__parse_number_u64(wuffs_base__slice_u8 s) {\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n for (; (p < q) && (*p == '_'); p++) {\n }\n\n if (p >= q) {\n goto fail_bad_argument;\n\n } else if (*p == '0') {\n p++;\n if (p >= q) {\n goto ok_zero;\n }\n if (*p == '_') {\n p++;\n for (; p < q; p++) {\n if (*p != '_') {\n goto fail_bad_argument;\n }\n }\n goto ok_zero;\n }\n\n if ((*p == 'x') || (*p == 'X')) {\n p++;\n for (; (p < q) && (*p == '_'); p++) {\n }\n " + - "if (p < q) {\n goto hexadecimal;\n }\n\n } else if ((*p == 'd') || (*p == 'D')) {\n p++;\n for (; (p < q) && (*p == '_'); p++) {\n }\n if (p < q) {\n goto decimal;\n }\n }\n\n goto fail_bad_argument;\n }\n\ndecimal:\n do {\n uint64_t v = wuffs_base__parse_number__decimal_digits[*p++];\n if (v == 0) {\n goto fail_bad_argument;\n }\n v &= 0x0F;\n\n // UINT64_MAX is 18446744073709551615, which is ((10 * max10) + max1).\n const uint64_t max10 = 1844674407370955161u;\n const uint8_t max1 = 5;\n\n for (; p < q; p++) {\n if (*p == '_') {\n continue;\n }\n uint8_t digit = wuffs_base__parse_number__decimal_digits[*p];\n if (digit == 0) {\n goto fail_bad_argument;\n }\n digit &= 0x0F;\n if ((v > max10) || ((v == max10) && (digit > max1))) {\n goto fail_out_of_bounds;\n }\n v = (10 * v) + ((uint64_t)(digit));\n }\n\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = v;\n return ret;\n " + - "} while (0);\n\nhexadecimal:\n do {\n uint64_t v = wuffs_base__parse_number__hexadecimal_digits[*p++];\n if (v == 0) {\n goto fail_bad_argument;\n }\n v &= 0x0F;\n\n for (; p < q; p++) {\n if (*p == '_') {\n continue;\n }\n uint8_t digit = wuffs_base__parse_number__hexadecimal_digits[*p];\n if (digit == 0) {\n goto fail_bad_argument;\n }\n digit &= 0x0F;\n if ((v >> 60) != 0) {\n goto fail_out_of_bounds;\n }\n v = (v << 4) | ((uint64_t)(digit));\n }\n\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = v;\n return ret;\n } while (0);\n\nok_zero:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_bad_argument:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_out_of_bounds:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = wuffs_base__error__" + - "out_of_bounds;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\n" + - "" + - "// --------\n\n// wuffs_base__render_number__first_hundred contains the decimal encodings of\n// the first one hundred numbers [0 ..= 99].\nstatic const uint8_t wuffs_base__render_number__first_hundred[200] = {\n '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', //\n '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', //\n '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', //\n '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', //\n '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', //\n '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', //\n '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', //\n '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', //\n '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', //\n '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', //\n '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', //\n '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', //\n '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', //\n '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', //\n '" + - "7', '0', '7', '1', '7', '2', '7', '3', '7', '4', //\n '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', //\n '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', //\n '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', //\n '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', //\n '9', '5', '9', '6', '9', '7', '9', '8', '9', '9', //\n};\n\nstatic size_t //\nwuffs_base__private_implementation__render_number_u64(wuffs_base__slice_u8 dst,\n uint64_t x,\n uint32_t options,\n bool neg) {\n uint8_t buf[WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL];\n uint8_t* ptr = &buf[0] + sizeof(buf);\n\n while (x >= 100) {\n size_t index = (x % 100) * 2;\n x /= 100;\n uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0];\n uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1];\n ptr -= 2;\n ptr[0] = s0;\n ptr[1] = s1;\n }\n\n if (x < 10) {\n " + - " ptr -= 1;\n ptr[0] = '0' + x;\n } else {\n size_t index = x * 2;\n uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0];\n uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1];\n ptr -= 2;\n ptr[0] = s0;\n ptr[1] = s1;\n }\n\n if (neg) {\n ptr -= 1;\n ptr[0] = '-';\n } else if (options & WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN) {\n ptr -= 1;\n ptr[0] = '+';\n }\n\n size_t n = sizeof(buf) - (ptr - &buf[0]);\n if (n > dst.len) {\n return 0;\n }\n memcpy(dst.ptr + ((options & WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT)\n ? (dst.len - n)\n : 0),\n ptr, n);\n return n;\n}\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_i64(wuffs_base__slice_u8 dst,\n int64_t x,\n uint32_t options) {\n uint64_t u = x;\n bool neg = x < 0;\n if (neg) {\n u = 1 + ~u;\n }\n return wuffs_base__private_implementation__render_number_u64(dst, u, options,\n " + - " neg);\n}\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_u64(wuffs_base__slice_u8 dst,\n uint64_t x,\n uint32_t options) {\n return wuffs_base__private_implementation__render_number_u64(dst, x, options,\n false);\n}\n\n" + - "" + - "// ---------------- Hexadecimal\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__hexadecimal__decode2(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t src_len2 = src.len / 2;\n size_t len = dst.len < src_len2 ? dst.len : src_len2;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n while (n--) {\n *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[0]] << 4) |\n (wuffs_base__parse_number__hexadecimal_digits[s[1]] & 0x0F));\n d += 1;\n s += 2;\n }\n\n return len;\n}\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t src_len4 = src.len / 4;\n size_t len = dst.len < src_len4 ? dst.len : src_len4;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n while (n--) {\n *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[2]] << 4) |\n (wuffs_base__parse_number__hexa" + - "decimal_digits[s[3]] & 0x0F));\n d += 1;\n s += 4;\n }\n\n return len;\n}\n\n" + - "" + "// ---------------- Unicode and UTF-8\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__utf_8__encode(wuffs_base__slice_u8 dst, uint32_t code_point) {\n if (code_point <= 0x7F) {\n if (dst.len >= 1) {\n dst.ptr[0] = (uint8_t)(code_point);\n return 1;\n }\n\n } else if (code_point <= 0x07FF) {\n if (dst.len >= 2) {\n dst.ptr[0] = (uint8_t)(0xC0 | ((code_point >> 6)));\n dst.ptr[1] = (uint8_t)(0x80 | ((code_point >> 0) & 0x3F));\n return 2;\n }\n\n } else if (code_point <= 0xFFFF) {\n if ((dst.len >= 3) && ((code_point < 0xD800) || (0xDFFF < code_point))) {\n dst.ptr[0] = (uint8_t)(0xE0 | ((code_point >> 12)));\n dst.ptr[1] = (uint8_t)(0x80 | ((code_point >> 6) & 0x3F));\n dst.ptr[2] = (uint8_t)(0x80 | ((code_point >> 0) & 0x3F));\n return 3;\n }\n\n } else if (code_point <= 0x10FFFF) {\n if (dst.len >= 4) {\n dst.ptr[0] = (uint8_t)(0xF0 | ((code_point >> 18)));\n dst.ptr[1] = (uint8_t)(0x80 | ((code_point >> 12) & 0x3F));\n dst.ptr[2] = (uint8_t)(0x80 |" + " ((code_point >> 6) & 0x3F));\n dst.ptr[3] = (uint8_t)(0x80 | ((code_point >> 0) & 0x3F));\n return 4;\n }\n }\n\n return 0;\n}\n\n// wuffs_base__utf_8__byte_length_minus_1 is the byte length (minus 1) of a\n// UTF-8 encoded code point, based on the encoding's initial byte.\n// - 0x00 is 1-byte UTF-8 (ASCII).\n// - 0x01 is the start of 2-byte UTF-8.\n// - 0x02 is the start of 3-byte UTF-8.\n// - 0x03 is the start of 4-byte UTF-8.\n// - 0x40 is a UTF-8 tail byte.\n// - 0x80 is invalid UTF-8.\n//\n// RFC 3629 (UTF-8) gives this grammar for valid UTF-8:\n// UTF8-1 = %x00-7F\n// UTF8-2 = %xC2-DF UTF8-tail\n// UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /\n// %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )\n// UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /\n// %xF4 %x80-8F 2( UTF8-tail )\n// UTF8-tail = %x80-BF\nstatic const uint8_t wuffs_base__utf_8__byte_length_minus_1[256] = {\n // 0 1 2 3 4 5 " + " 6 7\n // 8 9 A B C D E F\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30 ..= 0x37.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 ..= 0x47.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60 ..= 0x67.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00" + @@ -117,6 +97,29 @@ const BaseF64ConvSubmoduleC = "" + "NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits);\n return ret;\n } while (0);\n\ninfinity:\n do {\n uint64_t bits = h.negative ? 0xFFF0000000000000 : 0x7FF0000000000000;\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits);\n return ret;\n } while (0);\n}\n" + "" +const BaseI64ConvSubmoduleC = "" + + "// ---------------- Integer\n\n// wuffs_base__parse_number__foo_digits entries are 0x00 for invalid digits,\n// and (0x80 | v) for valid digits, where v is the 4 bit value.\n\nstatic const uint8_t wuffs_base__parse_number__decimal_digits[256] = {\n // 0 1 2 3 4 5 6 7\n // 8 9 A B C D E F\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F.\n 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x30 ..= 0x37. '0'-'7'.\n 0x88, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. '8'-'9'.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 ..= 0x47.\n 0x00, 0x00, 0x00, " + + "0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60 ..= 0x67.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00," + + " // 0xB8 ..= 0xBF.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.\n // 0 1 2 3 4 5 6 7\n // 8 9 A B C D E F\n};\n\nstatic const uint8_t wuffs_base__parse_number__hexadecimal_digits[256] = {\n // 0 1 2 3 4 5 6 7\n // 8 9 A B C D E F\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F.\n 0x00, " + + "0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F.\n 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x30 ..= 0x37. '0'-'7'.\n 0x88, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. '8'-'9'.\n\n 0x00, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x00, // 0x40 ..= 0x47. 'A'-'F'.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F.\n 0x00, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x00, // 0x60 ..= 0x67. 'a'-'f'.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F.\n\n " + + " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 ..= 0xBF.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.\n 0x00, 0x00, 0x00, 0x00, 0x0" + + "0, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.\n // 0 1 2 3 4 5 6 7\n // 8 9 A B C D E F\n};\n\n" + + "" + + "// --------\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_i64 //\nwuffs_base__parse_number_i64(wuffs_base__slice_u8 s) {\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n for (; (p < q) && (*p == '_'); p++) {\n }\n\n bool negative = false;\n if (p >= q) {\n goto fail_bad_argument;\n } else if (*p == '-') {\n p++;\n negative = true;\n } else if (*p == '+') {\n p++;\n }\n\n do {\n wuffs_base__result_u64 r = wuffs_base__parse_number_u64(\n wuffs_base__make_slice_u8(p, (size_t)(q - p)));\n if (r.status.repr != NULL) {\n wuffs_base__result_i64 ret;\n ret.status.repr = r.status.repr;\n ret.value = 0;\n return ret;\n } else if (negative) {\n if (r.value > 0x8000000000000000) {\n goto fail_out_of_bounds;\n }\n wuffs_base__result_i64 ret;\n ret.status.repr = NULL;\n ret.value = -(int64_t)(r.value);\n return ret;\n } else if (r.value > 0x7FFFFFFFFFFFFFFF) {\n goto fail_out_of_bounds;\n } else {\n wuffs_base__result_i64 ret;\n ret.status" + + ".repr = NULL;\n ret.value = +(int64_t)(r.value);\n return ret;\n }\n } while (0);\n\nfail_bad_argument:\n do {\n wuffs_base__result_i64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_out_of_bounds:\n do {\n wuffs_base__result_i64 ret;\n ret.status.repr = wuffs_base__error__out_of_bounds;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_u64 //\nwuffs_base__parse_number_u64(wuffs_base__slice_u8 s) {\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n for (; (p < q) && (*p == '_'); p++) {\n }\n\n if (p >= q) {\n goto fail_bad_argument;\n\n } else if (*p == '0') {\n p++;\n if (p >= q) {\n goto ok_zero;\n }\n if (*p == '_') {\n p++;\n for (; p < q; p++) {\n if (*p != '_') {\n goto fail_bad_argument;\n }\n }\n goto ok_zero;\n }\n\n if ((*p == 'x') || (*p == 'X')) {\n p++;\n for (; (p < q) && (*p == '_'); p++) {\n }\n " + + "if (p < q) {\n goto hexadecimal;\n }\n\n } else if ((*p == 'd') || (*p == 'D')) {\n p++;\n for (; (p < q) && (*p == '_'); p++) {\n }\n if (p < q) {\n goto decimal;\n }\n }\n\n goto fail_bad_argument;\n }\n\ndecimal:\n do {\n uint64_t v = wuffs_base__parse_number__decimal_digits[*p++];\n if (v == 0) {\n goto fail_bad_argument;\n }\n v &= 0x0F;\n\n // UINT64_MAX is 18446744073709551615, which is ((10 * max10) + max1).\n const uint64_t max10 = 1844674407370955161u;\n const uint8_t max1 = 5;\n\n for (; p < q; p++) {\n if (*p == '_') {\n continue;\n }\n uint8_t digit = wuffs_base__parse_number__decimal_digits[*p];\n if (digit == 0) {\n goto fail_bad_argument;\n }\n digit &= 0x0F;\n if ((v > max10) || ((v == max10) && (digit > max1))) {\n goto fail_out_of_bounds;\n }\n v = (10 * v) + ((uint64_t)(digit));\n }\n\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = v;\n return ret;\n " + + "} while (0);\n\nhexadecimal:\n do {\n uint64_t v = wuffs_base__parse_number__hexadecimal_digits[*p++];\n if (v == 0) {\n goto fail_bad_argument;\n }\n v &= 0x0F;\n\n for (; p < q; p++) {\n if (*p == '_') {\n continue;\n }\n uint8_t digit = wuffs_base__parse_number__hexadecimal_digits[*p];\n if (digit == 0) {\n goto fail_bad_argument;\n }\n digit &= 0x0F;\n if ((v >> 60) != 0) {\n goto fail_out_of_bounds;\n }\n v = (v << 4) | ((uint64_t)(digit));\n }\n\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = v;\n return ret;\n } while (0);\n\nok_zero:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_bad_argument:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_out_of_bounds:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = wuffs_base__error__" + + "out_of_bounds;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\n" + + "" + + "// --------\n\n// wuffs_base__render_number__first_hundred contains the decimal encodings of\n// the first one hundred numbers [0 ..= 99].\nstatic const uint8_t wuffs_base__render_number__first_hundred[200] = {\n '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', //\n '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', //\n '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', //\n '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', //\n '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', //\n '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', //\n '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', //\n '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', //\n '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', //\n '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', //\n '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', //\n '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', //\n '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', //\n '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', //\n '" + + "7', '0', '7', '1', '7', '2', '7', '3', '7', '4', //\n '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', //\n '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', //\n '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', //\n '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', //\n '9', '5', '9', '6', '9', '7', '9', '8', '9', '9', //\n};\n\nstatic size_t //\nwuffs_base__private_implementation__render_number_u64(wuffs_base__slice_u8 dst,\n uint64_t x,\n uint32_t options,\n bool neg) {\n uint8_t buf[WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL];\n uint8_t* ptr = &buf[0] + sizeof(buf);\n\n while (x >= 100) {\n size_t index = (x % 100) * 2;\n x /= 100;\n uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0];\n uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1];\n ptr -= 2;\n ptr[0] = s0;\n ptr[1] = s1;\n }\n\n if (x < 10) {\n " + + " ptr -= 1;\n ptr[0] = '0' + x;\n } else {\n size_t index = x * 2;\n uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0];\n uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1];\n ptr -= 2;\n ptr[0] = s0;\n ptr[1] = s1;\n }\n\n if (neg) {\n ptr -= 1;\n ptr[0] = '-';\n } else if (options & WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN) {\n ptr -= 1;\n ptr[0] = '+';\n }\n\n size_t n = sizeof(buf) - (ptr - &buf[0]);\n if (n > dst.len) {\n return 0;\n }\n memcpy(dst.ptr + ((options & WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT)\n ? (dst.len - n)\n : 0),\n ptr, n);\n return n;\n}\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_i64(wuffs_base__slice_u8 dst,\n int64_t x,\n uint32_t options) {\n uint64_t u = x;\n bool neg = x < 0;\n if (neg) {\n u = 1 + ~u;\n }\n return wuffs_base__private_implementation__render_number_u64(dst, u, options,\n " + + " neg);\n}\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_u64(wuffs_base__slice_u8 dst,\n uint64_t x,\n uint32_t options) {\n return wuffs_base__private_implementation__render_number_u64(dst, x, options,\n false);\n}\n\n" + + "" + + "// ---------------- Hexadecimal\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__hexadecimal__decode2(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t src_len2 = src.len / 2;\n size_t len = dst.len < src_len2 ? dst.len : src_len2;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n while (n--) {\n *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[0]] << 4) |\n (wuffs_base__parse_number__hexadecimal_digits[s[1]] & 0x0F));\n d += 1;\n s += 2;\n }\n\n return len;\n}\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t src_len4 = src.len / 4;\n size_t len = dst.len < src_len4 ? dst.len : src_len4;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n while (n--) {\n *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[2]] << 4) |\n (wuffs_base__parse_number__hexa" + + "decimal_digits[s[3]] & 0x0F));\n d += 1;\n s += 4;\n }\n\n return len;\n}\n" + + "" + const BasePixConvSubmoduleC = "" + "// ---------------- Pixel Swizzler\n\nstatic inline uint32_t //\nwuffs_base__swap_u32_argb_abgr(uint32_t u) {\n uint32_t o = u & 0xFF00FF00;\n uint32_t r = u & 0x00FF0000;\n uint32_t b = u & 0x000000FF;\n return o | (r >> 16) | (b << 16);\n}\n\n" + "" + @@ -414,21 +417,21 @@ const BaseStrConvPrivateH = "" + "" const BaseStrConvPublicH = "" + - "// ---------------- String Conversions\n\n// wuffs_base__parse_number_i64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"-123\" then it will return the int64_t -123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow an int64_t.\n//\n// It is similar to wuffs_base__parse_number_u64 but it returns a signed\n// integer, not an unsigned integer. It also allows a leading '+' or '-'.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_i64 //\nwuffs_base__parse_number_i64(wuffs_base__slice_u8 s);\n\n// wuffs_base__parse_number_u64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"123\" then it will return the uint64_t 123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow a uint64_t.\n//\n// It is similar to the C standard library's strtoull function, but:\n// - Errors are returned in-band (in a result type), not out-of-band (errno).\n// - It takes a slice (a pointer and length), not a " + - "NUL-terminated C string.\n// - It does not take an optional endptr argument. It does not allow a partial\n// parse: it returns an error unless all of s is consumed.\n// - It does not allow whitespace, leading or otherwise.\n// - It does not allow a leading '+' or '-'.\n// - It does not allow unnecessary leading zeroes (\"0\" is valid and its sole\n// zero is necessary). All of \"00\", \"0644\" and \"007\" are invalid.\n// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it\n// always accepts both decimal (e.g \"1234\", \"0d5678\") and hexadecimal (e.g.\n// \"0x9aBC\"). The caller is responsible for prior filtering of e.g. hex\n// numbers if they are unwanted. For example, Wuffs' JSON decoder will only\n// produce a wuffs_base__token for decimal numbers, not hexadecimal.\n// - It is not affected by i18n / l10n settings such as environment variables.\n// - It does allow arbitrary underscores, except inside the optional 2-byte\n// opening \"0d\" or \"0X\" that denotes base-10 or base-16. For ex" + - "ample,\n// \"__0D_1_002\" would successfully parse as \"one thousand and two\".\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_u64 //\nwuffs_base__parse_number_u64(wuffs_base__slice_u8 s);\n\n" + + "// ---------------- String Conversions\n\n// Options (bitwise or'ed together) for wuffs_base__render_number_etc\n// functions.\n\n#define WUFFS_BASE__RENDER_NUMBER__DEFAULT_OPTIONS ((uint32_t)0x00000000)\n\n// WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT means to render to the right side\n// (higher indexes) of the destination slice, leaving any untouched bytes on\n// the left side (lower indexes). The default is vice versa: rendering on the\n// left with slack on the right.\n#define WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT ((uint32_t)0x00000001)\n\n// WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN means to render the unnecessary\n// leading \"+\" for non-negative numbers: \"+0\" and \"+123\", not \"0\" and \"123\".\n#define WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN ((uint32_t)0x00000002)\n\n" + "" + "// ---------------- IEEE 754 Floating Point\n\n// wuffs_base__parse_number_f64 parses the floating point number in s. For\n// example, if s contains the bytes \"1.5\" then it will return the double 1.5.\n//\n// It returns an error if s does not contain a floating point number.\n//\n// It does not necessarily return an error if the conversion is lossy, e.g. if\n// s is \"0.3\", which double-precision floating point cannot represent exactly.\n//\n// Similarly, the returned value may be infinite (and no error returned) even\n// if s was not \"inf\", when the input is nominally finite but sufficiently\n// larger than DBL_MAX, about 1.8e+308.\n//\n// It is similar to the C standard library's strtod function, but:\n// - Errors are returned in-band (in a result type), not out-of-band (errno).\n// - It takes a slice (a pointer and length), not a NUL-terminated C string.\n// - It does not take an optional endptr argument. It does not allow a partial\n// parse: it returns an error unless all of s is consumed.\n// - It does not allow whi" + "tespace, leading or otherwise.\n// - It does not allow unnecessary leading zeroes (\"0\" is valid and its sole\n// zero is necessary). All of \"00\", \"0644\" and \"00.7\" are invalid.\n// - It is not affected by i18n / l10n settings such as environment variables.\n// - Conversely, it always accepts either ',' or '.' as a decimal separator.\n// In particular, \"3,141,592\" is always invalid but \"3,141\" is always valid\n// (and approximately π). The caller is responsible for e.g. previously\n// rejecting or filtering s if it contains a comma, if that is unacceptable\n// to the caller. For example, JSON numbers always use a dot '.' and never a\n// comma ',', regardless of the LOCALE environment variable.\n// - It does allow arbitrary underscores. For example, \"_3.141_592\" would\n// successfully parse, again approximately π.\n// - It does allow \"inf\", \"+Infinity\" and \"-NAN\", case insensitive, but it\n// does not permit \"nan\" to be followed by an integer mantissa.\n// - It does not allow hexadecimal float" + "ing point numbers.\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__F64CONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__parse_number_f64(wuffs_base__slice_u8 s);\n\n" + "" + - "// --------\n\n// Options (bitwise or'ed together) for wuffs_base__render_number_etc\n// functions.\n\n#define WUFFS_BASE__RENDER_NUMBER__DEFAULT_OPTIONS ((uint32_t)0x00000000)\n\n// WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT means to render to the right side\n// (higher indexes) of the destination slice, leaving any untouched bytes on\n// the left side (lower indexes). The default is vice versa: rendering on the\n// left with slack on the right.\n#define WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT ((uint32_t)0x00000001)\n\n// WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN means to render the unnecessary\n// leading \"+\" for non-negative numbers: \"+0\" and \"+123\", not \"0\" and \"123\".\n#define WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN ((uint32_t)0x00000002)\n\n" + + "// --------\n\n// wuffs_base__ieee_754_bit_representation__etc converts between a double\n// precision numerical value and its IEEE 754 64-bit representation (1 sign\n// bit, 11 exponent bits, 52 explicit significand bits).\n//\n// For example, it converts between:\n// - +1.0 and 0x3FF0_0000_0000_0000.\n// - +5.5 and 0x4016_0000_0000_0000.\n// - -inf and 0xFFF0_0000_0000_0000.\n//\n// See https://en.wikipedia.org/wiki/Double-precision_floating-point_format\n\nstatic inline uint64_t //\nwuffs_base__ieee_754_bit_representation__from_f64(double f) {\n uint64_t u = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&u, &f, sizeof(uint64_t));\n }\n return u;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representation__to_f64(uint64_t u) {\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &u, sizeof(uint64_t));\n }\n return f;\n}\n\n" + + "" + + "// ---------------- Integer\n\n// wuffs_base__parse_number_i64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"-123\" then it will return the int64_t -123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow an int64_t.\n//\n// It is similar to wuffs_base__parse_number_u64 but it returns a signed\n// integer, not an unsigned integer. It also allows a leading '+' or '-'.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_i64 //\nwuffs_base__parse_number_i64(wuffs_base__slice_u8 s);\n\n// wuffs_base__parse_number_u64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"123\" then it will return the uint64_t 123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow a uint64_t.\n//\n// It is similar to the C standard library's strtoull function, but:\n// - Errors are returned in-band (in a result type), not out-of-band (errno).\n// - It takes a slice (a pointer and length), not a NUL-termina" + + "ted C string.\n// - It does not take an optional endptr argument. It does not allow a partial\n// parse: it returns an error unless all of s is consumed.\n// - It does not allow whitespace, leading or otherwise.\n// - It does not allow a leading '+' or '-'.\n// - It does not allow unnecessary leading zeroes (\"0\" is valid and its sole\n// zero is necessary). All of \"00\", \"0644\" and \"007\" are invalid.\n// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it\n// always accepts both decimal (e.g \"1234\", \"0d5678\") and hexadecimal (e.g.\n// \"0x9aBC\"). The caller is responsible for prior filtering of e.g. hex\n// numbers if they are unwanted. For example, Wuffs' JSON decoder will only\n// produce a wuffs_base__token for decimal numbers, not hexadecimal.\n// - It is not affected by i18n / l10n settings such as environment variables.\n// - It does allow arbitrary underscores, except inside the optional 2-byte\n// opening \"0d\" or \"0X\" that denotes base-10 or base-16. For example,\n// " + + " \"__0D_1_002\" would successfully parse as \"one thousand and two\".\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_u64 //\nwuffs_base__parse_number_u64(wuffs_base__slice_u8 s);\n\n" + "" + "// --------\n\n#define WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL 20\n#define WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL 21\n\n// wuffs_base__render_number_i64 writes the decimal encoding of x to dst and\n// returns the number of bytes written. If dst is shorter than the entire\n// encoding, it returns 0 (and no bytes are written).\n//\n// dst will never be too short if its length is at least 20, also known as\n// WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL.\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_i64(wuffs_base__slice_u8 dst,\n int64_t x,\n uint32_t options);\n\n// wuffs_base__render_number_u64 writes the decimal encoding of x to dst and\n// returns the number of bytes written. If dst is shorter than the entire\n// encoding, it returns 0 (and no bytes are written).\n//\n// dst will never be too short if its length is at least 21, also known as\n// WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL.\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_u64(wuffs_bas" + "e__slice_u8 dst,\n uint64_t x,\n uint32_t options);\n\n" + "" + - "// --------\n\n// wuffs_base__ieee_754_bit_representation__etc converts between a double\n// precision numerical value and its IEEE 754 64-bit representation (1 sign\n// bit, 11 exponent bits, 52 explicit significand bits).\n//\n// For example, it converts between:\n// - +1.0 and 0x3FF0_0000_0000_0000.\n// - +5.5 and 0x4016_0000_0000_0000.\n// - -inf and 0xFFF0_0000_0000_0000.\n//\n// See https://en.wikipedia.org/wiki/Double-precision_floating-point_format\n\nstatic inline uint64_t //\nwuffs_base__ieee_754_bit_representation__from_f64(double f) {\n uint64_t u = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&u, &f, sizeof(uint64_t));\n }\n return u;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representation__to_f64(uint64_t u) {\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &u, sizeof(uint64_t));\n }\n return f;\n}\n\n" + - "" + "// ---------------- Hexadecimal\n\n// wuffs_base__hexadecimal__decode2 converts \"6A6b\" to \"jk\", where e.g. 'j' is\n// U+006A. There are 2 source bytes for every destination byte.\n//\n// It returns the number of dst bytes written: the minimum of dst.len and\n// (src.len / 2). Excess source bytes are ignored.\n//\n// It assumes that the src bytes are two hexadecimal digits (0-9, A-F, a-f),\n// repeated. It may write nonsense bytes if not, although it will not read or\n// write out of bounds.\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__hexadecimal__decode2(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src);\n\n// wuffs_base__hexadecimal__decode4 converts \"\\\\x6A\\\\x6b\" to \"jk\", where e.g.\n// 'j' is U+006A. There are 4 source bytes for every destination byte.\n//\n// It returns the number of dst bytes written: the minimum of dst.len and\n// (src.len / 4). Excess source bytes are ignored.\n//\n// It assumes that the src bytes are two ignored bytes and then two hexadecimal\n// digits (0-9, A-F, a" + "-f), repeated. It may write nonsense bytes if not,\n// although it will not read or write out of bounds.\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src);\n\n" + "" + diff --git a/internal/cgen/data/gen.go b/internal/cgen/data/gen.go index b60f1c90..488dc3dc 100644 --- a/internal/cgen/data/gen.go +++ b/internal/cgen/data/gen.go @@ -77,6 +77,7 @@ func genBase(out *bytes.Buffer) error { {"../base/strconv-impl.c", "BaseStrConvImplC"}, {"../base/f64conv-submodule.c", "BaseF64ConvSubmoduleC"}, + {"../base/i64conv-submodule.c", "BaseI64ConvSubmoduleC"}, {"../base/pixconv-submodule.c", "BasePixConvSubmoduleC"}, {"../base/fundamental-private.h", "BaseFundamentalPrivateH"}, diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 4a17194a..e8a48951 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -3866,43 +3866,20 @@ wuffs_base__pixel_swizzler::swizzle_interleaved_from_slice( // ---------------- String Conversions -// wuffs_base__parse_number_i64 parses the ASCII integer in s. For example, if -// s contains the bytes "-123" then it will return the int64_t -123. -// -// It returns an error if s does not contain an integer or if the integer -// within would overflow an int64_t. -// -// It is similar to wuffs_base__parse_number_u64 but it returns a signed -// integer, not an unsigned integer. It also allows a leading '+' or '-'. -WUFFS_BASE__MAYBE_STATIC wuffs_base__result_i64 // -wuffs_base__parse_number_i64(wuffs_base__slice_u8 s); +// Options (bitwise or'ed together) for wuffs_base__render_number_etc +// functions. -// wuffs_base__parse_number_u64 parses the ASCII integer in s. For example, if -// s contains the bytes "123" then it will return the uint64_t 123. -// -// It returns an error if s does not contain an integer or if the integer -// within would overflow a uint64_t. -// -// It is similar to the C standard library's strtoull function, but: -// - Errors are returned in-band (in a result type), not out-of-band (errno). -// - It takes a slice (a pointer and length), not a NUL-terminated C string. -// - It does not take an optional endptr argument. It does not allow a partial -// parse: it returns an error unless all of s is consumed. -// - It does not allow whitespace, leading or otherwise. -// - It does not allow a leading '+' or '-'. -// - It does not allow unnecessary leading zeroes ("0" is valid and its sole -// zero is necessary). All of "00", "0644" and "007" are invalid. -// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it -// always accepts both decimal (e.g "1234", "0d5678") and hexadecimal (e.g. -// "0x9aBC"). The caller is responsible for prior filtering of e.g. hex -// numbers if they are unwanted. For example, Wuffs' JSON decoder will only -// produce a wuffs_base__token for decimal numbers, not hexadecimal. -// - It is not affected by i18n / l10n settings such as environment variables. -// - It does allow arbitrary underscores, except inside the optional 2-byte -// opening "0d" or "0X" that denotes base-10 or base-16. For example, -// "__0D_1_002" would successfully parse as "one thousand and two". -WUFFS_BASE__MAYBE_STATIC wuffs_base__result_u64 // -wuffs_base__parse_number_u64(wuffs_base__slice_u8 s); +#define WUFFS_BASE__RENDER_NUMBER__DEFAULT_OPTIONS ((uint32_t)0x00000000) + +// WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT means to render to the right side +// (higher indexes) of the destination slice, leaving any untouched bytes on +// the left side (lower indexes). The default is vice versa: rendering on the +// left with slack on the right. +#define WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT ((uint32_t)0x00000001) + +// WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN means to render the unnecessary +// leading "+" for non-negative numbers: "+0" and "+123", not "0" and "123". +#define WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN ((uint32_t)0x00000002) // ---------------- IEEE 754 Floating Point @@ -3947,50 +3924,6 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s); // -------- -// Options (bitwise or'ed together) for wuffs_base__render_number_etc -// functions. - -#define WUFFS_BASE__RENDER_NUMBER__DEFAULT_OPTIONS ((uint32_t)0x00000000) - -// WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT means to render to the right side -// (higher indexes) of the destination slice, leaving any untouched bytes on -// the left side (lower indexes). The default is vice versa: rendering on the -// left with slack on the right. -#define WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT ((uint32_t)0x00000001) - -// WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN means to render the unnecessary -// leading "+" for non-negative numbers: "+0" and "+123", not "0" and "123". -#define WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN ((uint32_t)0x00000002) - -// -------- - -#define WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL 20 -#define WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL 21 - -// wuffs_base__render_number_i64 writes the decimal encoding of x to dst and -// returns the number of bytes written. If dst is shorter than the entire -// encoding, it returns 0 (and no bytes are written). -// -// dst will never be too short if its length is at least 20, also known as -// WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL. -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__render_number_i64(wuffs_base__slice_u8 dst, - int64_t x, - uint32_t options); - -// wuffs_base__render_number_u64 writes the decimal encoding of x to dst and -// returns the number of bytes written. If dst is shorter than the entire -// encoding, it returns 0 (and no bytes are written). -// -// dst will never be too short if its length is at least 21, also known as -// WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL. -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__render_number_u64(wuffs_base__slice_u8 dst, - uint64_t x, - uint32_t options); - -// -------- - // wuffs_base__ieee_754_bit_representation__etc converts between a double // precision numerical value and its IEEE 754 64-bit representation (1 sign // bit, 11 exponent bits, 52 explicit significand bits). @@ -4020,6 +3953,73 @@ wuffs_base__ieee_754_bit_representation__to_f64(uint64_t u) { return f; } +// ---------------- Integer + +// wuffs_base__parse_number_i64 parses the ASCII integer in s. For example, if +// s contains the bytes "-123" then it will return the int64_t -123. +// +// It returns an error if s does not contain an integer or if the integer +// within would overflow an int64_t. +// +// It is similar to wuffs_base__parse_number_u64 but it returns a signed +// integer, not an unsigned integer. It also allows a leading '+' or '-'. +WUFFS_BASE__MAYBE_STATIC wuffs_base__result_i64 // +wuffs_base__parse_number_i64(wuffs_base__slice_u8 s); + +// wuffs_base__parse_number_u64 parses the ASCII integer in s. For example, if +// s contains the bytes "123" then it will return the uint64_t 123. +// +// It returns an error if s does not contain an integer or if the integer +// within would overflow a uint64_t. +// +// It is similar to the C standard library's strtoull function, but: +// - Errors are returned in-band (in a result type), not out-of-band (errno). +// - It takes a slice (a pointer and length), not a NUL-terminated C string. +// - It does not take an optional endptr argument. It does not allow a partial +// parse: it returns an error unless all of s is consumed. +// - It does not allow whitespace, leading or otherwise. +// - It does not allow a leading '+' or '-'. +// - It does not allow unnecessary leading zeroes ("0" is valid and its sole +// zero is necessary). All of "00", "0644" and "007" are invalid. +// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it +// always accepts both decimal (e.g "1234", "0d5678") and hexadecimal (e.g. +// "0x9aBC"). The caller is responsible for prior filtering of e.g. hex +// numbers if they are unwanted. For example, Wuffs' JSON decoder will only +// produce a wuffs_base__token for decimal numbers, not hexadecimal. +// - It is not affected by i18n / l10n settings such as environment variables. +// - It does allow arbitrary underscores, except inside the optional 2-byte +// opening "0d" or "0X" that denotes base-10 or base-16. For example, +// "__0D_1_002" would successfully parse as "one thousand and two". +WUFFS_BASE__MAYBE_STATIC wuffs_base__result_u64 // +wuffs_base__parse_number_u64(wuffs_base__slice_u8 s); + +// -------- + +#define WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL 20 +#define WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL 21 + +// wuffs_base__render_number_i64 writes the decimal encoding of x to dst and +// returns the number of bytes written. If dst is shorter than the entire +// encoding, it returns 0 (and no bytes are written). +// +// dst will never be too short if its length is at least 20, also known as +// WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL. +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__render_number_i64(wuffs_base__slice_u8 dst, + int64_t x, + uint32_t options); + +// wuffs_base__render_number_u64 writes the decimal encoding of x to dst and +// returns the number of bytes written. If dst is shorter than the entire +// encoding, it returns 0 (and no bytes are written). +// +// dst will never be too short if its length is at least 21, also known as +// WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL. +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__render_number_u64(wuffs_base__slice_u8 dst, + uint64_t x, + uint32_t options); + // ---------------- Hexadecimal // wuffs_base__hexadecimal__decode2 converts "6A6b" to "jk", where e.g. 'j' is @@ -8237,428 +8237,6 @@ const char* wuffs_base__image_decoder__vtable_name = "{vtable}wuffs_base__image_ const char* wuffs_base__io_transformer__vtable_name = "{vtable}wuffs_base__io_transformer"; const char* wuffs_base__token_decoder__vtable_name = "{vtable}wuffs_base__token_decoder"; -// ---------------- 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__MAYBE_STATIC 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++; - } - - do { - wuffs_base__result_u64 r = wuffs_base__parse_number_u64( - wuffs_base__make_slice_u8(p, (size_t)(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; - } - } while (0); - -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__MAYBE_STATIC 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 = 1844674407370955161u; - 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); -} - -// -------- - -// wuffs_base__render_number__first_hundred contains the decimal encodings of -// the first one hundred numbers [0 ..= 99]. -static const uint8_t wuffs_base__render_number__first_hundred[200] = { - '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', // - '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', // - '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', // - '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', // - '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', // - '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', // - '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', // - '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', // - '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', // - '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', // - '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', // - '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', // - '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', // - '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', // - '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', // - '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', // - '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', // - '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', // - '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', // - '9', '5', '9', '6', '9', '7', '9', '8', '9', '9', // -}; - -static size_t // -wuffs_base__private_implementation__render_number_u64(wuffs_base__slice_u8 dst, - uint64_t x, - uint32_t options, - bool neg) { - uint8_t buf[WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL]; - uint8_t* ptr = &buf[0] + sizeof(buf); - - while (x >= 100) { - size_t index = (x % 100) * 2; - x /= 100; - uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0]; - uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1]; - ptr -= 2; - ptr[0] = s0; - ptr[1] = s1; - } - - if (x < 10) { - ptr -= 1; - ptr[0] = '0' + x; - } else { - size_t index = x * 2; - uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0]; - uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1]; - ptr -= 2; - ptr[0] = s0; - ptr[1] = s1; - } - - if (neg) { - ptr -= 1; - ptr[0] = '-'; - } else if (options & WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN) { - ptr -= 1; - ptr[0] = '+'; - } - - size_t n = sizeof(buf) - (ptr - &buf[0]); - if (n > dst.len) { - return 0; - } - memcpy(dst.ptr + ((options & WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT) - ? (dst.len - n) - : 0), - ptr, n); - return n; -} - -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__render_number_i64(wuffs_base__slice_u8 dst, - int64_t x, - uint32_t options) { - uint64_t u = x; - bool neg = x < 0; - if (neg) { - u = 1 + ~u; - } - return wuffs_base__private_implementation__render_number_u64(dst, u, options, - neg); -} - -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__render_number_u64(wuffs_base__slice_u8 dst, - uint64_t x, - uint32_t options) { - return wuffs_base__private_implementation__render_number_u64(dst, x, options, - false); -} - -// ---------------- Hexadecimal - -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__hexadecimal__decode2(wuffs_base__slice_u8 dst, - wuffs_base__slice_u8 src) { - size_t src_len2 = src.len / 2; - size_t len = dst.len < src_len2 ? dst.len : src_len2; - uint8_t* d = dst.ptr; - uint8_t* s = src.ptr; - size_t n = len; - - while (n--) { - *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[0]] << 4) | - (wuffs_base__parse_number__hexadecimal_digits[s[1]] & 0x0F)); - d += 1; - s += 2; - } - - return len; -} - -WUFFS_BASE__MAYBE_STATIC size_t // -wuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst, - wuffs_base__slice_u8 src) { - size_t src_len4 = src.len / 4; - size_t len = dst.len < src_len4 ? dst.len : src_len4; - uint8_t* d = dst.ptr; - uint8_t* s = src.ptr; - size_t n = len; - - while (n--) { - *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[2]] << 4) | - (wuffs_base__parse_number__hexadecimal_digits[s[3]] & 0x0F)); - d += 1; - s += 4; - } - - return len; -} - // ---------------- Unicode and UTF-8 WUFFS_BASE__MAYBE_STATIC size_t // @@ -10695,6 +10273,435 @@ infinity: // defined(WUFFS_CONFIG__MODULE__BASE) || // defined(WUFFS_CONFIG__MODULE__BASE__F64CONV) +#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) || \ + defined(WUFFS_CONFIG__MODULE__BASE__I64CONV) + +// ---------------- Integer + +// 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__MAYBE_STATIC 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++; + } + + do { + wuffs_base__result_u64 r = wuffs_base__parse_number_u64( + wuffs_base__make_slice_u8(p, (size_t)(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; + } + } while (0); + +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__MAYBE_STATIC 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 = 1844674407370955161u; + 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); +} + +// -------- + +// wuffs_base__render_number__first_hundred contains the decimal encodings of +// the first one hundred numbers [0 ..= 99]. +static const uint8_t wuffs_base__render_number__first_hundred[200] = { + '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', // + '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', // + '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', // + '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', // + '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', // + '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', // + '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', // + '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', // + '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', // + '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', // + '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', // + '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', // + '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', // + '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', // + '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', // + '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', // + '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', // + '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', // + '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', // + '9', '5', '9', '6', '9', '7', '9', '8', '9', '9', // +}; + +static size_t // +wuffs_base__private_implementation__render_number_u64(wuffs_base__slice_u8 dst, + uint64_t x, + uint32_t options, + bool neg) { + uint8_t buf[WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL]; + uint8_t* ptr = &buf[0] + sizeof(buf); + + while (x >= 100) { + size_t index = (x % 100) * 2; + x /= 100; + uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0]; + uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1]; + ptr -= 2; + ptr[0] = s0; + ptr[1] = s1; + } + + if (x < 10) { + ptr -= 1; + ptr[0] = '0' + x; + } else { + size_t index = x * 2; + uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0]; + uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1]; + ptr -= 2; + ptr[0] = s0; + ptr[1] = s1; + } + + if (neg) { + ptr -= 1; + ptr[0] = '-'; + } else if (options & WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN) { + ptr -= 1; + ptr[0] = '+'; + } + + size_t n = sizeof(buf) - (ptr - &buf[0]); + if (n > dst.len) { + return 0; + } + memcpy(dst.ptr + ((options & WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT) + ? (dst.len - n) + : 0), + ptr, n); + return n; +} + +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__render_number_i64(wuffs_base__slice_u8 dst, + int64_t x, + uint32_t options) { + uint64_t u = x; + bool neg = x < 0; + if (neg) { + u = 1 + ~u; + } + return wuffs_base__private_implementation__render_number_u64(dst, u, options, + neg); +} + +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__render_number_u64(wuffs_base__slice_u8 dst, + uint64_t x, + uint32_t options) { + return wuffs_base__private_implementation__render_number_u64(dst, x, options, + false); +} + +// ---------------- Hexadecimal + +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__hexadecimal__decode2(wuffs_base__slice_u8 dst, + wuffs_base__slice_u8 src) { + size_t src_len2 = src.len / 2; + size_t len = dst.len < src_len2 ? dst.len : src_len2; + uint8_t* d = dst.ptr; + uint8_t* s = src.ptr; + size_t n = len; + + while (n--) { + *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[0]] << 4) | + (wuffs_base__parse_number__hexadecimal_digits[s[1]] & 0x0F)); + d += 1; + s += 2; + } + + return len; +} + +WUFFS_BASE__MAYBE_STATIC size_t // +wuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst, + wuffs_base__slice_u8 src) { + size_t src_len4 = src.len / 4; + size_t len = dst.len < src_len4 ? dst.len : src_len4; + uint8_t* d = dst.ptr; + uint8_t* s = src.ptr; + size_t n = len; + + while (n--) { + *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[2]] << 4) | + (wuffs_base__parse_number__hexadecimal_digits[s[3]] & 0x0F)); + d += 1; + s += 4; + } + + return len; +} + +#endif // !defined(WUFFS_CONFIG__MODULES) || + // defined(WUFFS_CONFIG__MODULE__BASE) || + // defined(WUFFS_CONFIG__MODULE__BASE__I64CONV) + #if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) || \ defined(WUFFS_CONFIG__MODULE__BASE__PIXCONV)