mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 16:52:18 +01:00
Spin out base-i64conv sub-module
Before: 12624 gen/lib/c/clang-5.0-dynamic/wuffs-base-core.lo 13032 gen/lib/c/clang-5.0-static/wuffs-base-core.o 12184 gen/lib/c/gcc-dynamic/wuffs-base-core.lo 12408 gen/lib/c/gcc-static/wuffs-base-core.o After: 8224 gen/lib/c/clang-5.0-dynamic/wuffs-base-core.lo 5608 gen/lib/c/clang-5.0-dynamic/wuffs-base-i64conv.lo 8200 gen/lib/c/clang-5.0-static/wuffs-base-core.o 6032 gen/lib/c/clang-5.0-static/wuffs-base-i64conv.o 8704 gen/lib/c/gcc-dynamic/wuffs-base-core.lo 4952 gen/lib/c/gcc-dynamic/wuffs-base-i64conv.lo 8920 gen/lib/c/gcc-static/wuffs-base-core.o 4912 gen/lib/c/gcc-static/wuffs-base-i64conv.o
This commit is contained in:
1 parent
f5fbc5d8e7
commit
22a09b4133
8 files changed
+1074
-1032
No files matched your search
@@ -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)
|
||||
|
||||
|
||||
Reference in new issue
Block a user