mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +01:00
Add wuffs_base__parse_number_u64
This commit is contained in:
1 parent
b194c7fbb9
commit
70db96ac6d
11 files changed
+697
-4
No files matched your search
@@ -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)
|
||||
|
||||
|
||||
Reference in new issue
Block a user