mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Add wuffs_base__render_number_{i,u}64
This commit is contained in:
1 parent
00cc8a50aa
commit
d40a386211
5 files changed
+454
-2
No files matched your search
@@ -299,6 +299,103 @@ fail_out_of_bounds:
|
||||
} 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 //
|
||||
|
||||
@@ -95,6 +95,52 @@ wuffs_base__parse_number_u64(wuffs_base__slice_u8 s);
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //
|
||||
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).
|
||||
|
||||
@@ -41,6 +41,11 @@ const BaseStrConvImplC = "" +
|
||||
"} while (0);\n\nhexadecimal:\n do {\n uint64_t v = wuffs_base__parse_number__hexadecimal_digits[*p++];\n if (v == 0) {\n goto fail_bad_argument;\n }\n v &= 0x0F;\n\n for (; p < q; p++) {\n if (*p == '_') {\n continue;\n }\n uint8_t digit = wuffs_base__parse_number__hexadecimal_digits[*p];\n if (digit == 0) {\n goto fail_bad_argument;\n }\n digit &= 0x0F;\n if ((v >> 60) != 0) {\n goto fail_out_of_bounds;\n }\n v = (v << 4) | ((uint64_t)(digit));\n }\n\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = v;\n return ret;\n } while (0);\n\nok_zero:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_bad_argument:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_out_of_bounds:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = wuffs_base__error__" +
|
||||
"out_of_bounds;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\n" +
|
||||
"" +
|
||||
"// --------\n\n// wuffs_base__render_number__first_hundred contains the decimal encodings of\n// the first one hundred numbers [0 ..= 99].\nstatic const uint8_t wuffs_base__render_number__first_hundred[200] = {\n '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', //\n '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', //\n '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', //\n '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', //\n '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', //\n '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', //\n '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', //\n '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', //\n '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', //\n '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', //\n '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', //\n '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', //\n '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', //\n '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', //\n '" +
|
||||
"7', '0', '7', '1', '7', '2', '7', '3', '7', '4', //\n '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', //\n '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', //\n '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', //\n '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', //\n '9', '5', '9', '6', '9', '7', '9', '8', '9', '9', //\n};\n\nstatic size_t //\nwuffs_base__private_implementation__render_number_u64(wuffs_base__slice_u8 dst,\n uint64_t x,\n uint32_t options,\n bool neg) {\n uint8_t buf[WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL];\n uint8_t* ptr = &buf[0] + sizeof(buf);\n\n while (x >= 100) {\n size_t index = (x % 100) * 2;\n x /= 100;\n uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0];\n uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1];\n ptr -= 2;\n ptr[0] = s0;\n ptr[1] = s1;\n }\n\n if (x < 10) {\n " +
|
||||
" ptr -= 1;\n ptr[0] = '0' + x;\n } else {\n size_t index = x * 2;\n uint8_t s0 = wuffs_base__render_number__first_hundred[index + 0];\n uint8_t s1 = wuffs_base__render_number__first_hundred[index + 1];\n ptr -= 2;\n ptr[0] = s0;\n ptr[1] = s1;\n }\n\n if (neg) {\n ptr -= 1;\n ptr[0] = '-';\n } else if (options & WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN) {\n ptr -= 1;\n ptr[0] = '+';\n }\n\n size_t n = sizeof(buf) - (ptr - &buf[0]);\n if (n > dst.len) {\n return 0;\n }\n memcpy(dst.ptr + ((options & WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT)\n ? (dst.len - n)\n : 0),\n ptr, n);\n return n;\n}\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_i64(wuffs_base__slice_u8 dst,\n int64_t x,\n uint32_t options) {\n uint64_t u = x;\n bool neg = x < 0;\n if (neg) {\n u = 1 + ~u;\n }\n return wuffs_base__private_implementation__render_number_u64(dst, u, options,\n " +
|
||||
" neg);\n}\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_u64(wuffs_base__slice_u8 dst,\n uint64_t x,\n uint32_t options) {\n return wuffs_base__private_implementation__render_number_u64(dst, x, options,\n false);\n}\n\n" +
|
||||
"" +
|
||||
"// ---------------- Hexadecimal\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__hexadecimal__decode2(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t src_len2 = src.len / 2;\n size_t len = dst.len < src_len2 ? dst.len : src_len2;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n while (n--) {\n *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[0]] << 4) |\n (wuffs_base__parse_number__hexadecimal_digits[s[1]] & 0x0F));\n d += 1;\n s += 2;\n }\n\n return len;\n}\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t src_len4 = src.len / 4;\n size_t len = dst.len < src_len4 ? dst.len : src_len4;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n while (n--) {\n *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[2]] << 4) |\n (wuffs_base__parse_number__hexa" +
|
||||
"decimal_digits[s[3]] & 0x0F));\n d += 1;\n s += 4;\n }\n\n return len;\n}\n\n" +
|
||||
"" +
|
||||
@@ -414,8 +419,14 @@ const BaseStrConvPublicH = "" +
|
||||
"" +
|
||||
"// ---------------- IEEE 754 Floating Point\n\n// wuffs_base__parse_number_f64 parses the floating point number in s. For\n// example, if s contains the bytes \"1.5\" then it will return the double 1.5.\n//\n// It returns an error if s does not contain a floating point number.\n//\n// It does not necessarily return an error if the conversion is lossy, e.g. if\n// s is \"0.3\", which double-precision floating point cannot represent exactly.\n//\n// Similarly, the returned value may be infinite (and no error returned) even\n// if s was not \"inf\", when the input is nominally finite but sufficiently\n// larger than DBL_MAX, about 1.8e+308.\n//\n// It is similar to the C standard library's strtod function, but:\n// - Errors are returned in-band (in a result type), not out-of-band (errno).\n// - It takes a slice (a pointer and length), not a NUL-terminated C string.\n// - It does not take an optional endptr argument. It does not allow a partial\n// parse: it returns an error unless all of s is consumed.\n// - It does not allow whi" +
|
||||
"tespace, leading or otherwise.\n// - It does not allow unnecessary leading zeroes (\"0\" is valid and its sole\n// zero is necessary). All of \"00\", \"0644\" and \"00.7\" are invalid.\n// - It is not affected by i18n / l10n settings such as environment variables.\n// - Conversely, it always accepts either ',' or '.' as a decimal separator.\n// In particular, \"3,141,592\" is always invalid but \"3,141\" is always valid\n// (and approximately π). The caller is responsible for e.g. previously\n// rejecting or filtering s if it contains a comma, if that is unacceptable\n// to the caller. For example, JSON numbers always use a dot '.' and never a\n// comma ',', regardless of the LOCALE environment variable.\n// - It does allow arbitrary underscores. For example, \"_3.141_592\" would\n// successfully parse, again approximately π.\n// - It does allow \"inf\", \"+Infinity\" and \"-NAN\", case insensitive, but it\n// does not permit \"nan\" to be followed by an integer mantissa.\n// - It does not allow hexadecimal float" +
|
||||
"ing point numbers.\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__F64CONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__parse_number_f64(wuffs_base__slice_u8 s);\n\n// wuffs_base__ieee_754_bit_representation__etc converts between a double\n// precision numerical value and its IEEE 754 64-bit representation (1 sign\n// bit, 11 exponent bits, 52 explicit significand bits).\n//\n// For example, it converts between:\n// - +1.0 and 0x3FF0_0000_0000_0000.\n// - +5.5 and 0x4016_0000_0000_0000.\n// - -inf and 0xFFF0_0000_0000_0000.\n//\n// See https://en.wikipedia.org/wiki/Double-precision_floating-point_format\n\nstatic inline uint64_t //\nwuffs_base__ieee_754_bit_representation__from_f64(double f) {\n uint64_t u = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&u, &f, sizeof(uint64_t));\n }\n return u;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bi" +
|
||||
"t_representation__to_f64(uint64_t u) {\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &u, sizeof(uint64_t));\n }\n return f;\n}\n\n" +
|
||||
"ing point numbers.\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__F64CONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__parse_number_f64(wuffs_base__slice_u8 s);\n\n" +
|
||||
"" +
|
||||
"// --------\n\n// Options (bitwise or'ed together) for wuffs_base__render_number_etc\n// functions.\n\n#define WUFFS_BASE__RENDER_NUMBER__DEFAULT_OPTIONS ((uint32_t)0x00000000)\n\n// WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT means to render to the right side\n// (higher indexes) of the destination slice, leaving any untouched bytes on\n// the left side (lower indexes). The default is vice versa: rendering on the\n// left with slack on the right.\n#define WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT ((uint32_t)0x00000001)\n\n// WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN means to render the unnecessary\n// leading \"+\" for non-negative numbers: \"+0\" and \"+123\", not \"0\" and \"123\".\n#define WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN ((uint32_t)0x00000002)\n\n" +
|
||||
"" +
|
||||
"// --------\n\n#define WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL 20\n#define WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL 21\n\n// wuffs_base__render_number_i64 writes the decimal encoding of x to dst and\n// returns the number of bytes written. If dst is shorter than the entire\n// encoding, it returns 0 (and no bytes are written).\n//\n// dst will never be too short if its length is at least 20, also known as\n// WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL.\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_i64(wuffs_base__slice_u8 dst,\n int64_t x,\n uint32_t options);\n\n// wuffs_base__render_number_u64 writes the decimal encoding of x to dst and\n// returns the number of bytes written. If dst is shorter than the entire\n// encoding, it returns 0 (and no bytes are written).\n//\n// dst will never be too short if its length is at least 21, also known as\n// WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL.\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_u64(wuffs_bas" +
|
||||
"e__slice_u8 dst,\n uint64_t x,\n uint32_t options);\n\n" +
|
||||
"" +
|
||||
"// --------\n\n// wuffs_base__ieee_754_bit_representation__etc converts between a double\n// precision numerical value and its IEEE 754 64-bit representation (1 sign\n// bit, 11 exponent bits, 52 explicit significand bits).\n//\n// For example, it converts between:\n// - +1.0 and 0x3FF0_0000_0000_0000.\n// - +5.5 and 0x4016_0000_0000_0000.\n// - -inf and 0xFFF0_0000_0000_0000.\n//\n// See https://en.wikipedia.org/wiki/Double-precision_floating-point_format\n\nstatic inline uint64_t //\nwuffs_base__ieee_754_bit_representation__from_f64(double f) {\n uint64_t u = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&u, &f, sizeof(uint64_t));\n }\n return u;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representation__to_f64(uint64_t u) {\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &u, sizeof(uint64_t));\n }\n return f;\n}\n\n" +
|
||||
"" +
|
||||
"// ---------------- Hexadecimal\n\n// wuffs_base__hexadecimal__decode2 converts \"6A6b\" to \"jk\", where e.g. 'j' is\n// U+006A. There are 2 source bytes for every destination byte.\n//\n// It returns the number of dst bytes written: the minimum of dst.len and\n// (src.len / 2). Excess source bytes are ignored.\n//\n// It assumes that the src bytes are two hexadecimal digits (0-9, A-F, a-f),\n// repeated. It may write nonsense bytes if not, although it will not read or\n// write out of bounds.\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__hexadecimal__decode2(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src);\n\n// wuffs_base__hexadecimal__decode4 converts \"\\\\x6A\\\\x6b\" to \"jk\", where e.g.\n// 'j' is U+006A. There are 4 source bytes for every destination byte.\n//\n// It returns the number of dst bytes written: the minimum of dst.len and\n// (src.len / 4). Excess source bytes are ignored.\n//\n// It assumes that the src bytes are two ignored bytes and then two hexadecimal\n// digits (0-9, A-F, a" +
|
||||
"-f), repeated. It may write nonsense bytes if not,\n// although it will not read or write out of bounds.\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src);\n\n" +
|
||||
|
||||
@@ -3943,6 +3943,52 @@ wuffs_base__parse_number_u64(wuffs_base__slice_u8 s);
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //
|
||||
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).
|
||||
@@ -8474,6 +8520,103 @@ fail_out_of_bounds:
|
||||
} 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 //
|
||||
|
||||
@@ -858,6 +858,159 @@ test_wuffs_strconv_parse_number_u64() {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_strconv_render_number_i64() {
|
||||
CHECK_FOCUS(__func__);
|
||||
|
||||
struct {
|
||||
int64_t x;
|
||||
const char* want;
|
||||
} test_cases[] = {
|
||||
{.x = +0x0000000000000000l, .want = "0"},
|
||||
{.x = +0x0000000000000009l, .want = "9"},
|
||||
{.x = +0x000000000000000Al, .want = "10"},
|
||||
{.x = +0x000000000000004Al, .want = "74"},
|
||||
{.x = +0x0000000000000063l, .want = "99"},
|
||||
{.x = +0x0000000000000064l, .want = "100"},
|
||||
{.x = +0x000000000000007Cl, .want = "124"},
|
||||
{.x = +0x00000000000001F4l, .want = "500"},
|
||||
{.x = +0x000000000000036Cl, .want = "876"},
|
||||
{.x = +0x000000000000036Fl, .want = "879"},
|
||||
{.x = +0x0000000000000929l, .want = "2345"},
|
||||
{.x = +0x0000000000010932l, .want = "67890"},
|
||||
{.x = +0x00000000FFFFFFFFl, .want = "4294967295"},
|
||||
{.x = +0x0000000100000000l, .want = "4294967296"},
|
||||
{.x = +0x0123456789ABCDEFl, .want = "81985529216486895"},
|
||||
{.x = +0x7FFFFFFFFFFFFFFFl, .want = "9223372036854775807"},
|
||||
|
||||
{.x = -0x0000000000000009l, .want = "-9"},
|
||||
{.x = -0x000000000000000Al, .want = "-10"},
|
||||
{.x = -0x000000000000004Al, .want = "-74"},
|
||||
{.x = -0x0000000000000063l, .want = "-99"},
|
||||
{.x = -0x0000000000000064l, .want = "-100"},
|
||||
{.x = -0x000000000000007Cl, .want = "-124"},
|
||||
{.x = -0x00000000000001F4l, .want = "-500"},
|
||||
{.x = -0x000000000000036Cl, .want = "-876"},
|
||||
{.x = -0x000000000000036Fl, .want = "-879"},
|
||||
{.x = -0x0000000000000929l, .want = "-2345"},
|
||||
{.x = -0x0000000000010932l, .want = "-67890"},
|
||||
{.x = -0x00000000FFFFFFFFl, .want = "-4294967295"},
|
||||
{.x = -0x0000000100000000l, .want = "-4294967296"},
|
||||
{.x = -0x0123456789ABCDEFl, .want = "-81985529216486895"},
|
||||
{.x = -0x7FFFFFFFFFFFFFFFl, .want = "-9223372036854775807"},
|
||||
{.x = -0x8000000000000000l, .want = "-9223372036854775808"},
|
||||
};
|
||||
|
||||
if (g_have_slice_u8.len < WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL) {
|
||||
return "g_have_slice_u8.len is too short";
|
||||
}
|
||||
|
||||
int tc;
|
||||
for (tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) {
|
||||
size_t n = wuffs_base__render_number_i64(
|
||||
g_have_slice_u8, test_cases[tc].x,
|
||||
WUFFS_BASE__RENDER_NUMBER__DEFAULT_OPTIONS);
|
||||
if (n == 0) {
|
||||
RETURN_FAIL("%" PRId64 ": could not render", test_cases[tc].x);
|
||||
} else if (n >= g_have_slice_u8.len) {
|
||||
RETURN_FAIL("%" PRId64 ": n is too large", test_cases[tc].x);
|
||||
}
|
||||
g_have_slice_u8.ptr[n] = 0x00;
|
||||
if (strcmp((const char*)(g_have_slice_u8.ptr), test_cases[tc].want) != 0) {
|
||||
RETURN_FAIL("%" PRId64 ": have \"%s\", want \"%s\"", test_cases[tc].x,
|
||||
g_have_slice_u8.ptr, test_cases[tc].want);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_strconv_render_number_u64() {
|
||||
CHECK_FOCUS(__func__);
|
||||
|
||||
struct {
|
||||
uint64_t x;
|
||||
const char* want;
|
||||
} test_cases[] = {
|
||||
{.x = 0x0000000000000000l, .want = "0"},
|
||||
{.x = 0x0000000000000009l, .want = "9"},
|
||||
{.x = 0x000000000000000Al, .want = "10"},
|
||||
{.x = 0x000000000000004Al, .want = "74"},
|
||||
{.x = 0x0000000000000063l, .want = "99"},
|
||||
{.x = 0x0000000000000064l, .want = "100"},
|
||||
{.x = 0x000000000000007Cl, .want = "124"},
|
||||
{.x = 0x00000000000001F4l, .want = "500"},
|
||||
{.x = 0x000000000000036Cl, .want = "876"},
|
||||
{.x = 0x000000000000036Fl, .want = "879"},
|
||||
{.x = 0x0000000000000929l, .want = "2345"},
|
||||
{.x = 0x0000000000010932l, .want = "67890"},
|
||||
{.x = 0x00000000FFFFFFFFl, .want = "4294967295"},
|
||||
{.x = 0x0000000100000000l, .want = "4294967296"},
|
||||
{.x = 0x0123456789ABCDEFl, .want = "81985529216486895"},
|
||||
{.x = 0x7FFFFFFFFFFFFFFFl, .want = "9223372036854775807"},
|
||||
{.x = 0x8000000000000000l, .want = "9223372036854775808"},
|
||||
{.x = 0xFFFFFFFFFFFFFFF9l, .want = "18446744073709551609"},
|
||||
{.x = 0xFFFFFFFFFFFFFFFAl, .want = "18446744073709551610"},
|
||||
{.x = 0xFFFFFFFFFFFFFFFEl, .want = "18446744073709551614"},
|
||||
{.x = 0xFFFFFFFFFFFFFFFFl, .want = "18446744073709551615"},
|
||||
};
|
||||
|
||||
if (g_have_slice_u8.len < WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL) {
|
||||
return "g_have_slice_u8.len is too short";
|
||||
}
|
||||
|
||||
int tc;
|
||||
for (tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) {
|
||||
size_t n = wuffs_base__render_number_u64(
|
||||
g_have_slice_u8, test_cases[tc].x,
|
||||
WUFFS_BASE__RENDER_NUMBER__DEFAULT_OPTIONS);
|
||||
if (n == 0) {
|
||||
RETURN_FAIL("%" PRIu64 ": could not render", test_cases[tc].x);
|
||||
} else if (n >= g_have_slice_u8.len) {
|
||||
RETURN_FAIL("%" PRIu64 ": n is too large", test_cases[tc].x);
|
||||
}
|
||||
g_have_slice_u8.ptr[n] = 0x00;
|
||||
if (strcmp((const char*)(g_have_slice_u8.ptr), test_cases[tc].want) != 0) {
|
||||
RETURN_FAIL("%" PRIu64 ": have \"%s\", want \"%s\"", test_cases[tc].x,
|
||||
g_have_slice_u8.ptr, test_cases[tc].want);
|
||||
}
|
||||
}
|
||||
|
||||
// Test dst slice is too short.
|
||||
{
|
||||
uint8_t dst[5];
|
||||
size_t n = wuffs_base__render_number_u64(
|
||||
wuffs_base__make_slice_u8(&dst[0], sizeof(dst)), 123456,
|
||||
WUFFS_BASE__RENDER_NUMBER__DEFAULT_OPTIONS);
|
||||
if (n != 0) {
|
||||
RETURN_FAIL("dst too short: have %zu, want 0", n);
|
||||
}
|
||||
}
|
||||
|
||||
// Test options.
|
||||
{
|
||||
uint8_t dst[8];
|
||||
memcpy(&dst[0], "ABCDEFG\x00", 8);
|
||||
size_t n = wuffs_base__render_number_u64(
|
||||
wuffs_base__make_slice_u8(&dst[0], sizeof(dst) - 1), 1234,
|
||||
WUFFS_BASE__RENDER_NUMBER__ALIGN_RIGHT |
|
||||
WUFFS_BASE__RENDER_NUMBER__LEADING_PLUS_SIGN);
|
||||
if (n != 5) {
|
||||
RETURN_FAIL("ALIGN_RIGHT | LEADING_PLUS_SIGN: have %zu, want 5", n);
|
||||
}
|
||||
uint64_t have = wuffs_base__load_u64be__no_bounds_check(&dst[0]);
|
||||
uint64_t want = 0x41422B3132333400; // "AB+1234\x00".
|
||||
if (have != want) {
|
||||
RETURN_FAIL("ALIGN_RIGHT | LEADING_PLUS_SIGN: have 0x%" PRIX64
|
||||
", want 0x%" PRIX64,
|
||||
have, want);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_strconv_utf_8_next() {
|
||||
CHECK_FOCUS(__func__);
|
||||
@@ -2481,6 +2634,8 @@ proc g_tests[] = {
|
||||
test_wuffs_strconv_parse_number_f64,
|
||||
test_wuffs_strconv_parse_number_i64,
|
||||
test_wuffs_strconv_parse_number_u64,
|
||||
test_wuffs_strconv_render_number_i64,
|
||||
test_wuffs_strconv_render_number_u64,
|
||||
test_wuffs_strconv_utf_8_next,
|
||||
|
||||
test_wuffs_json_decode_end_of_data,
|
||||
|
||||
Reference in new issue
Block a user