diff --git a/internal/cgen/base/all-impl.c b/internal/cgen/base/all-impl.c index 1230d598..91fcf9d7 100644 --- a/internal/cgen/base/all-impl.c +++ b/internal/cgen/base/all-impl.c @@ -69,6 +69,8 @@ extern "C" { // !! INSERT base/all-private.h. +// ---------------- + #if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) const uint8_t wuffs_base__low_bits_mask__u8[9] = { @@ -120,6 +122,8 @@ const uint64_t wuffs_base__low_bits_mask__u64[65] = { // !! INSERT base/image-impl.c. +// !! INSERT base/strconv-impl.c. + #endif // !defined(WUFFS_CONFIG__MODULES) || // defined(WUFFS_CONFIG__MODULE__BASE) diff --git a/internal/cgen/base/core-public.h b/internal/cgen/base/core-public.h index 527db4bb..74401ac8 100644 --- a/internal/cgen/base/core-public.h +++ b/internal/cgen/base/core-public.h @@ -239,6 +239,20 @@ wuffs_base__status::message() const { // -------- +// WUFFS_BASE__RESULT is a result type: either a status (an error) or a value. +// +// A result with all fields NULL or zero is as valid as a zero-valued T. +#define WUFFS_BASE__RESULT(T) \ + struct { \ + wuffs_base__status status; \ + T value; \ + } + +typedef WUFFS_BASE__RESULT(int64_t) wuffs_base__result_i64; +typedef WUFFS_BASE__RESULT(uint64_t) wuffs_base__result_u64; + +// -------- + // FourCC constants. // !! INSERT FourCCs. diff --git a/internal/cgen/base/strconv-impl.c b/internal/cgen/base/strconv-impl.c new file mode 100644 index 00000000..136e6ba8 --- /dev/null +++ b/internal/cgen/base/strconv-impl.c @@ -0,0 +1,229 @@ +// After editing this file, run "go generate" in the parent directory. + +// Copyright 2020 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// ---------------- String Conversions + +// wuffs_base__parse_number__foo_digits entries are 0x00 for invalid digits, +// and (0x80 | v) for valid digits, where v is the 4 bit value. + +static const uint8_t wuffs_base__parse_number__decimal_digits[256] = { + 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. +}; + +static const uint8_t wuffs_base__parse_number__hexadecimal_digits[256] = { + 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, 0x01, 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. +}; + +wuffs_base__result_u64 // +wuffs_base__parse_number_u64(wuffs_base__slice_u8 s) { + uint8_t* p = s.ptr; + uint8_t* q = s.ptr + s.len; + + for (; (p < q) && (*p == '_'); p++) { + } + + if (p >= q) { + goto fail_bad_argument; + + } else if (*p == '0') { + p++; + if (p >= q) { + goto ok_zero; + } + if (*p == '_') { + p++; + for (; p < q; p++) { + if (*p != '_') { + goto fail_bad_argument; + } + } + goto ok_zero; + } + + if ((*p == 'x') || (*p == 'X')) { + p++; + for (; (p < q) && (*p == '_'); p++) { + } + if (p < q) { + goto hexadecimal; + } + + } else if ((*p == 'd') || (*p == 'D')) { + p++; + for (; (p < q) && (*p == '_'); p++) { + } + if (p < q) { + goto decimal; + } + } + + goto fail_bad_argument; + } + +decimal: + do { + uint64_t v = wuffs_base__parse_number__decimal_digits[*p++]; + if (v == 0) { + goto fail_bad_argument; + } + v &= 0x0F; + + // UINT64_MAX is 18446744073709551615, which is ((10 * max10) + max1). + const uint64_t max10 = 1844674407370955161; + const uint8_t max1 = 5; + + for (; p < q; p++) { + if (*p == '_') { + continue; + } + uint8_t digit = wuffs_base__parse_number__decimal_digits[*p]; + if (digit == 0) { + goto fail_bad_argument; + } + digit &= 0x0F; + if ((v > max10) || ((v == max10) && (digit > max1))) { + goto fail_out_of_bounds; + } + v = (10 * v) + ((uint64_t)(digit)); + } + + wuffs_base__result_u64 ret; + ret.status.repr = NULL; + ret.value = v; + return ret; + } while (0); + +hexadecimal: + do { + uint64_t v = wuffs_base__parse_number__hexadecimal_digits[*p++]; + if (v == 0) { + goto fail_bad_argument; + } + v &= 0x0F; + + for (; p < q; p++) { + if (*p == '_') { + continue; + } + uint8_t digit = wuffs_base__parse_number__hexadecimal_digits[*p]; + if (digit == 0) { + goto fail_bad_argument; + } + digit &= 0x0F; + if ((v >> 60) != 0) { + goto fail_out_of_bounds; + } + v = (v << 4) | ((uint64_t)(digit)); + } + + wuffs_base__result_u64 ret; + ret.status.repr = NULL; + ret.value = v; + return ret; + } while (0); + +ok_zero: + do { + wuffs_base__result_u64 ret; + ret.status.repr = NULL; + ret.value = 0; + return ret; + } while (0); + +fail_bad_argument: + do { + wuffs_base__result_u64 ret; + ret.status.repr = wuffs_base__error__bad_argument; + ret.value = 0; + return ret; + } while (0); + +fail_out_of_bounds: + do { + wuffs_base__result_u64 ret; + ret.status.repr = wuffs_base__error__out_of_bounds; + ret.value = 0; + return ret; + } while (0); +} diff --git a/internal/cgen/base/strconv-private.h b/internal/cgen/base/strconv-private.h new file mode 100644 index 00000000..e9e2557c --- /dev/null +++ b/internal/cgen/base/strconv-private.h @@ -0,0 +1,17 @@ +// After editing this file, run "go generate" in the parent directory. + +// Copyright 2020 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// ---------------- String Conversions diff --git a/internal/cgen/base/strconv-public.h b/internal/cgen/base/strconv-public.h new file mode 100644 index 00000000..101f35f2 --- /dev/null +++ b/internal/cgen/base/strconv-public.h @@ -0,0 +1,43 @@ +// After editing this file, run "go generate" in the parent directory. + +// Copyright 2020 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// ---------------- String Conversions + +// wuffs_base__parse_number_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 multiple leading zeroes: "0" is valid but "00" is not. +// - 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. +// - 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__result_u64 // +wuffs_base__parse_number_u64(wuffs_base__slice_u8 s); diff --git a/internal/cgen/cgen.go b/internal/cgen/cgen.go index 502f0a3d..b186f97c 100644 --- a/internal/cgen/cgen.go +++ b/internal/cgen/cgen.go @@ -122,6 +122,7 @@ func Do(args []string) error { "// !! INSERT base/all-public.h.\n": insertBaseAllPublicH, "// !! INSERT base/copyright\n": insertBaseCopyright, "// !! INSERT base/image-impl.c.\n": insertBaseImageImplC, + "// !! INSERT base/strconv-impl.c.\n": insertBaseStrConvImplC, "// !! INSERT wuffs_base__status strings.\n": func(b *buffer) error { for _, z := range builtin.Statuses { msg, _ := t.Unescape(z) @@ -262,6 +263,8 @@ func insertBaseAllPrivateH(buf *buffer) error { buf.writeb('\n') buf.writes(baseImagePrivateH) buf.writeb('\n') + buf.writes(baseStrConvPrivateH) + buf.writeb('\n') return nil } @@ -311,6 +314,8 @@ func insertBaseAllPublicH(buf *buffer) error { buf.writeb('\n') buf.writes(baseImagePublicH) buf.writeb('\n') + buf.writes(baseStrConvPublicH) + buf.writeb('\n') return nil } @@ -326,6 +331,12 @@ func insertBaseImageImplC(buf *buffer) error { return nil } +func insertBaseStrConvImplC(buf *buffer) error { + buf.writes(baseStrConvImplC) + buf.writeb('\n') + return nil +} + func insertInterfaceDeclarations(buf *buffer) error { if err := parseBuiltInInterfaceMethods(); err != nil { return err diff --git a/internal/cgen/data.go b/internal/cgen/data.go index a87e6739..f8aea32b 100644 --- a/internal/cgen/data.go +++ b/internal/cgen/data.go @@ -19,10 +19,11 @@ package cgen const baseAllImplC = "" + "#ifndef WUFFS_INCLUDE_GUARD__BASE\n#define WUFFS_INCLUDE_GUARD__BASE\n\n#if defined(WUFFS_IMPLEMENTATION) && !defined(WUFFS_CONFIG__MODULES)\n#define WUFFS_CONFIG__MODULES\n#define WUFFS_CONFIG__MODULE__BASE\n#endif\n\n// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING ABOVE.\n\n// !! INSERT base/copyright\n\n#include \n#include \n#include \n\n// GCC does not warn for unused *static inline* functions, but clang does.\n#ifdef __clang__\n#pragma clang diagnostic push\n#pragma clang diagnostic ignored \"-Wunused-function\"\n#endif\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// !! INSERT base/all-public.h.\n\n// !! INSERT InterfaceDeclarations.\n\n" + "" + - "// ----------------\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#ifdef __clang__\n#pragma clang diagnostic pop\n#endif\n\n// WUFFS C HEADER ENDS HERE.\n#ifdef WUFFS_IMPLEMENTATION\n\n// GCC does not warn for unused *static inline* functions, but clang does.\n#ifdef __clang__\n#pragma clang diagnostic push\n#pragma clang diagnostic ignored \"-Wunused-function\"\n#endif\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// !! INSERT base/all-private.h.\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE)\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, 0x000" + - "007FF,\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, 0x0000000000000001, 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, 0x00000" + - "000FFFFFFFF,\n 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF,\n 0x0000000FFFFFFFFF, 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF,\n 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF, 0x000001FFFFFFFFFF,\n 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,\n 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF,\n 0x0000FFFFFFFFFFFF, 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF,\n 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF, 0x001FFFFFFFFFFFFF,\n 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,\n 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF,\n 0x0FFFFFFFFFFFFFFF, 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF,\n 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,\n};\n\n// !! INSERT wuffs_base__status strings.\n\n// !! INSERT InterfaceDefinitions.\n\n// !! INSERT base/image-impl.c.\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE)\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#ifdef __clang__\n#pragma clang diagnostic pop\n#endif\n\n#endif // WUFFS_IMP" + - "LEMENTATION\n\n// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING BELOW.\n\n#endif // WUFFS_INCLUDE_GUARD__BASE\n" + + "// ----------------\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#ifdef __clang__\n#pragma clang diagnostic pop\n#endif\n\n// WUFFS C HEADER ENDS HERE.\n#ifdef WUFFS_IMPLEMENTATION\n\n// GCC does not warn for unused *static inline* functions, but clang does.\n#ifdef __clang__\n#pragma clang diagnostic push\n#pragma clang diagnostic ignored \"-Wunused-function\"\n#endif\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// !! INSERT base/all-private.h.\n\n" + + "" + + "// ----------------\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE)\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, 0x0000000000000001, 0x0000000000000003,\n 0x0000000000" + + "000007, 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, 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF,\n 0x0007FFFFFF" + + "FFFFFF, 0x000FFFFFFFFFFFFF, 0x001FFFFFFFFFFFFF,\n 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,\n 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF,\n 0x0FFFFFFFFFFFFFFF, 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF,\n 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,\n};\n\n// !! INSERT wuffs_base__status strings.\n\n// !! INSERT InterfaceDefinitions.\n\n// !! INSERT base/image-impl.c.\n\n// !! INSERT base/strconv-impl.c.\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE)\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#ifdef __clang__\n#pragma clang diagnostic pop\n#endif\n\n#endif // WUFFS_IMPLEMENTATION\n\n// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING BELOW.\n\n#endif // WUFFS_INCLUDE_GUARD__BASE\n" + "" const baseImageImplC = "" + @@ -63,6 +64,17 @@ const baseImageImplC = "" + "palette, src_palette, blend);\n break;\n\n case WUFFS_BASE__PIXEL_FORMAT__BGR:\n func = wuffs_base__pixel_swizzler__prepare__bgr(\n p, dst_format, dst_palette, src_palette, blend);\n break;\n\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:\n func = wuffs_base__pixel_swizzler__prepare__bgra_nonpremul(\n p, dst_format, dst_palette, src_palette, blend);\n break;\n }\n\n p->private_impl.func = func;\n return wuffs_base__make_status(\n func ? NULL : wuffs_base__error__unsupported_pixel_swizzler_option);\n}\n\nuint64_t //\nwuffs_base__pixel_swizzler__swizzle_interleaved(\n const wuffs_base__pixel_swizzler* p,\n wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 dst_palette,\n wuffs_base__slice_u8 src) {\n if (p && p->private_impl.func) {\n return (*p->private_impl.func)(dst, dst_palette, src);\n }\n return 0;\n}\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 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, 0x" + + "00, 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};\n\nstatic const uint8_t wuffs_base__parse_number__hexadecimal_digits[256] = {\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, 0x01, 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, 0x0" + + "0, 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};\n\nwuffs_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 = 1844674407370955161;\n const uint8_t max1 = 5;\n\n for (; p < q; p++) {\n if (*p == '_') {\n continue;\n }\n ui" + + "nt8_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" + + "" + const baseCorePrivateH = "" + "static inline wuffs_base__empty_struct //\nwuffs_base__ignore_status(wuffs_base__status z) {\n return wuffs_base__make_empty_struct();\n}\n\n// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.\n// It's not foolproof, given C doesn't automatically zero memory before use,\n// but it should catch 99.99% of cases.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"wuffs\").\n#define WUFFS_BASE__MAGIC ((uint32_t)0x3CCB6C71)\n\n// WUFFS_BASE__DISABLED is a magic number to indicate that a non-recoverable\n// error was previously encountered.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"disabled\").\n#define WUFFS_BASE__DISABLED ((uint32_t)0x075AE3D2)\n\n// Denote intentional fallthroughs for -Wimplicit-fallthrough.\n//\n// The order matters here. Clang also defines \"__GNUC__\".\n#if defined(__clang__) && defined(__cplusplus) && (__cplusplus >= 201103L)\n#define WUFFS_BASE__FALLTHROUGH [[clang::fallthrough]]\n#elif !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 7)\n#define WUFFS_BAS" + "E__FALLTHROUGH __attribute__((fallthrough))\n#else\n#define WUFFS_BASE__FALLTHROUGH\n#endif\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use trivial macros instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (!status.repr) { \\\n goto ok; \\\n } else if (*status.repr != '$') { \\\n goto exit; \\\n } \\\n coro_susp_point" + @@ -104,6 +116,8 @@ const baseCorePublicH = "" + "ffs_base__status__is_ok(const wuffs_base__status* z) {\n return z->repr == NULL;\n}\n\nstatic inline bool //\nwuffs_base__status__is_suspension(const wuffs_base__status* z) {\n return z->repr && (*z->repr == '$');\n}\n\n// wuffs_base__status__message strips the leading '$', '#' or '@'.\nstatic inline const char* //\nwuffs_base__status__message(const wuffs_base__status* z) {\n if (z->repr) {\n if ((*z->repr == '$') || (*z->repr == '#') || (*z->repr == '@')) {\n return z->repr + 1;\n }\n }\n return z->repr;\n}\n\n#ifdef __cplusplus\n\ninline bool //\nwuffs_base__status::is_complete() const {\n return wuffs_base__status__is_complete(this);\n}\n\ninline bool //\nwuffs_base__status::is_error() const {\n return wuffs_base__status__is_error(this);\n}\n\ninline bool //\nwuffs_base__status::is_note() const {\n return wuffs_base__status__is_note(this);\n}\n\ninline bool //\nwuffs_base__status::is_ok() const {\n return wuffs_base__status__is_ok(this);\n}\n\ninline bool //\nwuffs_base__status::is_suspension() const {\n return wuffs_base" + "__status__is_suspension(this);\n}\n\ninline const char* //\nwuffs_base__status::message() const {\n return wuffs_base__status__message(this);\n}\n\n#endif // __cplusplus\n\n" + "" + + "// --------\n\n// WUFFS_BASE__RESULT is a result type: either a status (an error) or a value.\n//\n// A result with all fields NULL or zero is as valid as a zero-valued T.\n#define WUFFS_BASE__RESULT(T) \\\n struct { \\\n wuffs_base__status status; \\\n T value; \\\n }\n\ntypedef WUFFS_BASE__RESULT(int64_t) wuffs_base__result_i64;\ntypedef WUFFS_BASE__RESULT(uint64_t) wuffs_base__result_u64;\n\n" + + "" + "// --------\n\n// FourCC constants.\n\n// !! INSERT FourCCs.\n\n" + "" + "// --------\n\n// Flicks are a unit of time. One flick (frame-tick) is 1 / 705_600_000 of a\n// second. See https://github.com/OculusVR/Flicks\ntypedef int64_t wuffs_base__flicks;\n\n#define WUFFS_BASE__FLICKS_PER_SECOND ((uint64_t)705600000)\n#define WUFFS_BASE__FLICKS_PER_MILLISECOND ((uint64_t)705600)\n\n" + @@ -251,6 +265,15 @@ const baseRangePublicH = "" + "e__rect_ie_u32 s) const {\n return wuffs_base__rect_ie_u32__equals(this, s);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__rect_ie_u32::intersect(wuffs_base__rect_ie_u32 s) const {\n return wuffs_base__rect_ie_u32__intersect(this, s);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__rect_ie_u32::unite(wuffs_base__rect_ie_u32 s) const {\n return wuffs_base__rect_ie_u32__unite(this, s);\n}\n\ninline bool //\nwuffs_base__rect_ie_u32::contains(uint32_t x, uint32_t y) const {\n return wuffs_base__rect_ie_u32__contains(this, x, y);\n}\n\ninline bool //\nwuffs_base__rect_ie_u32::contains_rect(wuffs_base__rect_ie_u32 s) const {\n return wuffs_base__rect_ie_u32__contains_rect(this, s);\n}\n\ninline uint32_t //\nwuffs_base__rect_ie_u32::width() const {\n return wuffs_base__rect_ie_u32__width(this);\n}\n\ninline uint32_t //\nwuffs_base__rect_ie_u32::height() const {\n return wuffs_base__rect_ie_u32__height(this);\n}\n\n#endif // __cplusplus\n" + "" +const baseStrConvPrivateH = "" + + "// ---------------- String Conversions\n" + + "" + +const baseStrConvPublicH = "" + + "// ---------------- String Conversions\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 multiple leading zeroes: \"0\" is valid but \"00\" is not.\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// \"0x9aB" + + "C\"). 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.\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__result_u64 //\nwuffs_base__parse_number_u64(wuffs_base__slice_u8 s);\n" + + "" + const baseTokenPrivateH = "" + "// ---------------- Tokens\n\n" + "" + diff --git a/internal/cgen/gen.go b/internal/cgen/gen.go index 51bca4c5..9c1d3aaa 100644 --- a/internal/cgen/gen.go +++ b/internal/cgen/gen.go @@ -75,6 +75,7 @@ func genBase(out *bytes.Buffer) error { }{ {"base/all-impl.c", "baseAllImplC"}, {"base/image-impl.c", "baseImageImplC"}, + {"base/strconv-impl.c", "baseStrConvImplC"}, {"base/core-private.h", "baseCorePrivateH"}, {"base/core-public.h", "baseCorePublicH"}, @@ -86,6 +87,8 @@ func genBase(out *bytes.Buffer) error { {"base/io-public.h", "baseIOPublicH"}, {"base/range-private.h", "baseRangePrivateH"}, {"base/range-public.h", "baseRangePublicH"}, + {"base/strconv-private.h", "baseStrConvPrivateH"}, + {"base/strconv-public.h", "baseStrConvPublicH"}, {"base/token-private.h", "baseTokenPrivateH"}, {"base/token-public.h", "baseTokenPublicH"}, } diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index ac51c33c..7d807cf8 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -56,6 +56,7 @@ var Statuses = [...]string{ `"#initialize not called"`, `"#interleaved coroutine calls"`, `"#not enough data"`, + `"#out of bounds"`, `"#unsupported method"`, `"#unsupported option"`, `"#unsupported pixel swizzler option"`, diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 73323227..f13cf44c 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -200,6 +200,7 @@ extern const char* wuffs_base__error__initialize_falsely_claimed_already_zeroed; extern const char* wuffs_base__error__initialize_not_called; extern const char* wuffs_base__error__interleaved_coroutine_calls; extern const char* wuffs_base__error__not_enough_data; +extern const char* wuffs_base__error__out_of_bounds; extern const char* wuffs_base__error__unsupported_method; extern const char* wuffs_base__error__unsupported_option; extern const char* wuffs_base__error__unsupported_pixel_swizzler_option; @@ -284,6 +285,20 @@ wuffs_base__status::message() const { // -------- +// WUFFS_BASE__RESULT is a result type: either a status (an error) or a value. +// +// A result with all fields NULL or zero is as valid as a zero-valued T. +#define WUFFS_BASE__RESULT(T) \ + struct { \ + wuffs_base__status status; \ + T value; \ + } + +typedef WUFFS_BASE__RESULT(int64_t) wuffs_base__result_i64; +typedef WUFFS_BASE__RESULT(uint64_t) wuffs_base__result_u64; + +// -------- + // FourCC constants. // International Color Consortium Profile. @@ -2976,6 +2991,34 @@ wuffs_base__pixel_swizzler::swizzle_interleaved( #endif // __cplusplus +// ---------------- String Conversions + +// 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 multiple leading zeroes: "0" is valid but "00" is not. +// - 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. +// - 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__result_u64 // +wuffs_base__parse_number_u64(wuffs_base__slice_u8 s); + // ---------------- Interface Declarations. extern const char* wuffs_base__hasher_u32__vtable_name; @@ -6358,6 +6401,10 @@ wuffs_base__io_writer__set(wuffs_base__io_buffer* b, #define wuffs_base__utility__make_pixel_format wuffs_base__make_pixel_format + // ---------------- String Conversions + + // ---------------- + #if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE) const uint8_t wuffs_base__low_bits_mask__u8[9] = { @@ -6430,6 +6477,7 @@ const char* wuffs_base__error__initialize_not_called = const char* wuffs_base__error__interleaved_coroutine_calls = "#base: interleaved coroutine calls"; const char* wuffs_base__error__not_enough_data = "#base: not enough data"; +const char* wuffs_base__error__out_of_bounds = "#base: out of bounds"; const char* wuffs_base__error__unsupported_method = "#base: unsupported method"; const char* wuffs_base__error__unsupported_option = "#base: unsupported option"; const char* wuffs_base__error__unsupported_pixel_swizzler_option = @@ -7829,6 +7877,220 @@ wuffs_base__pixel_swizzler__swizzle_interleaved( return 0; } +// ---------------- 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] = { + 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. +}; + +static const uint8_t wuffs_base__parse_number__hexadecimal_digits[256] = { + 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, 0x01, 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. +}; + +wuffs_base__result_u64 // +wuffs_base__parse_number_u64(wuffs_base__slice_u8 s) { + uint8_t* p = s.ptr; + uint8_t* q = s.ptr + s.len; + + for (; (p < q) && (*p == '_'); p++) { + } + + if (p >= q) { + goto fail_bad_argument; + + } else if (*p == '0') { + p++; + if (p >= q) { + goto ok_zero; + } + if (*p == '_') { + p++; + for (; p < q; p++) { + if (*p != '_') { + goto fail_bad_argument; + } + } + goto ok_zero; + } + + if ((*p == 'x') || (*p == 'X')) { + p++; + for (; (p < q) && (*p == '_'); p++) { + } + if (p < q) { + goto hexadecimal; + } + + } else if ((*p == 'd') || (*p == 'D')) { + p++; + for (; (p < q) && (*p == '_'); p++) { + } + if (p < q) { + goto decimal; + } + } + + goto fail_bad_argument; + } + +decimal: + do { + uint64_t v = wuffs_base__parse_number__decimal_digits[*p++]; + if (v == 0) { + goto fail_bad_argument; + } + v &= 0x0F; + + // UINT64_MAX is 18446744073709551615, which is ((10 * max10) + max1). + const uint64_t max10 = 1844674407370955161; + const uint8_t max1 = 5; + + for (; p < q; p++) { + if (*p == '_') { + continue; + } + uint8_t digit = wuffs_base__parse_number__decimal_digits[*p]; + if (digit == 0) { + goto fail_bad_argument; + } + digit &= 0x0F; + if ((v > max10) || ((v == max10) && (digit > max1))) { + goto fail_out_of_bounds; + } + v = (10 * v) + ((uint64_t)(digit)); + } + + wuffs_base__result_u64 ret; + ret.status.repr = NULL; + ret.value = v; + return ret; + } while (0); + +hexadecimal: + do { + uint64_t v = wuffs_base__parse_number__hexadecimal_digits[*p++]; + if (v == 0) { + goto fail_bad_argument; + } + v &= 0x0F; + + for (; p < q; p++) { + if (*p == '_') { + continue; + } + uint8_t digit = wuffs_base__parse_number__hexadecimal_digits[*p]; + if (digit == 0) { + goto fail_bad_argument; + } + digit &= 0x0F; + if ((v >> 60) != 0) { + goto fail_out_of_bounds; + } + v = (v << 4) | ((uint64_t)(digit)); + } + + wuffs_base__result_u64 ret; + ret.status.repr = NULL; + ret.value = v; + return ret; + } while (0); + +ok_zero: + do { + wuffs_base__result_u64 ret; + ret.status.repr = NULL; + ret.value = 0; + return ret; + } while (0); + +fail_bad_argument: + do { + wuffs_base__result_u64 ret; + ret.status.repr = wuffs_base__error__bad_argument; + ret.value = 0; + return ret; + } while (0); + +fail_out_of_bounds: + do { + wuffs_base__result_u64 ret; + ret.status.repr = wuffs_base__error__out_of_bounds; + ret.value = 0; + return ret; + } while (0); +} + #endif // !defined(WUFFS_CONFIG__MODULES) || // defined(WUFFS_CONFIG__MODULE__BASE) diff --git a/test/c/std/json.c b/test/c/std/json.c index 5bea9af1..219e082a 100644 --- a/test/c/std/json.c +++ b/test/c/std/json.c @@ -67,6 +67,87 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the // No mimic library. #endif +// ---------------- String Conversions Tests + +const char* test_strconv_parse_number_u64() { + CHECK_FOCUS(__func__); + + const uint64_t bad = 0xDEADBEEF; + + struct { + uint64_t want; + const char* str; + } test_cases[] = { + {.want = 0x0000000000000000, .str = "0"}, + {.want = 0x0000000000000000, .str = "0_"}, + {.want = 0x0000000000000000, .str = "0d0"}, + {.want = 0x0000000000000000, .str = "0x000"}, + {.want = 0x0000000000000000, .str = "_0"}, + {.want = 0x0000000000000000, .str = "__0__"}, + {.want = 0x000000000000004A, .str = "0x4A"}, + {.want = 0x000000000000004B, .str = "0x__4_B_"}, + {.want = 0x000000000000007B, .str = "123"}, + {.want = 0x000000000000007C, .str = "12_4"}, + {.want = 0x000000000000007D, .str = "_1__2________5_"}, + {.want = 0x00000000000001F4, .str = "0d500"}, + {.want = 0x00000000000001F5, .str = "0D___5_01__"}, + {.want = 0x00000000FFFFFFFF, .str = "4294967295"}, + {.want = 0x0000000100000000, .str = "4294967296"}, + {.want = 0x0123456789ABCDEF, .str = "0x0123456789ABCDEF"}, + {.want = 0x0123456789ABCDEF, .str = "0x0123456789abcdef"}, + {.want = 0xFFFFFFFFFFFFFFF9, .str = "18446744073709551609"}, + {.want = 0xFFFFFFFFFFFFFFFA, .str = "18446744073709551610"}, + {.want = 0xFFFFFFFFFFFFFFFE, .str = "0xFFFFffffFFFFfffe"}, + {.want = 0xFFFFFFFFFFFFFFFE, .str = "18446744073709551614"}, + {.want = 0xFFFFFFFFFFFFFFFF, .str = "0xFFFF_FFFF_FFFF_FFFF"}, + {.want = 0xFFFFFFFFFFFFFFFF, .str = "18446744073709551615"}, + + {.want = bad, .str = " "}, + {.want = bad, .str = " 0"}, + {.want = bad, .str = " 12 "}, + {.want = bad, .str = ""}, + {.want = bad, .str = "+0"}, + {.want = bad, .str = "+1"}, + {.want = bad, .str = "-0"}, + {.want = bad, .str = "-1"}, + {.want = bad, .str = "0 "}, + {.want = bad, .str = "0_x1"}, + {.want = bad, .str = "0d___"}, + {.want = bad, .str = "0x"}, + {.want = bad, .str = "0x10000000000000000"}, // 1 << 64. + {.want = bad, .str = "0x1_0000_0000_0000_0000"}, // 1 << 64. + {.want = bad, .str = "1 23"}, + {.want = bad, .str = "1,23"}, + {.want = bad, .str = "1.23"}, + {.want = bad, .str = "123 "}, + {.want = bad, .str = "123456789012345678901234"}, + {.want = bad, .str = "12a3"}, + {.want = bad, .str = "18446744073709551616"}, // UINT64_MAX. + {.want = bad, .str = "18446744073709551617"}, + {.want = bad, .str = "18446744073709551618"}, + {.want = bad, .str = "18446744073709551619"}, + {.want = bad, .str = "18446744073709551620"}, + {.want = bad, .str = "18446744073709551621"}, + {.want = bad, .str = "_"}, + {.want = bad, .str = "d"}, + {.want = bad, .str = "x"}, + }; + + int i; + for (i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); i++) { + wuffs_base__result_u64 r = + wuffs_base__parse_number_u64(wuffs_base__make_slice_u8( + (void*)test_cases[i].str, strlen(test_cases[i].str))); + uint64_t got = (r.status.repr == NULL) ? r.value : bad; + if (got != test_cases[i].want) { + RETURN_FAIL("\"%s\": got 0x%" PRIX64 ", want 0x%" PRIX64, + test_cases[i].str, got, test_cases[i].want); + } + } + + return NULL; +} + // ---------------- JSON Tests const char* test_wuffs_json_decode_tokens() { @@ -131,6 +212,11 @@ const char* test_wuffs_json_decode_tokens() { // The empty comments forces clang-format to place one element per line. proc tests[] = { + // These strconv tests are really testing the Wuffs base library. They + // aren't specific to the std/json code, but putting them here is as good + // as any other place. + test_strconv_parse_number_u64, // + test_wuffs_json_decode_tokens, // #ifdef WUFFS_MIMIC