Add wuffs_base__parse_number_i64

This commit is contained in:
Nigel Tao committed 2020-02-25 13:07:22 +11:00
1 parent a9d1488674
commit 6d698a3a47
5 files changed
+196 -5

No files matched your search

+59
View File
@@ -105,6 +105,65 @@ static const uint8_t wuffs_base__parse_number__hexadecimal_digits[256] = {
// --------
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++;
}
wuffs_base__result_u64 r =
wuffs_base__parse_number_u64(wuffs_base__make_slice_u8(p, 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;
}
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__result_u64 //
wuffs_base__parse_number_u64(wuffs_base__slice_u8 s) {
uint8_t* p = s.ptr;
+11
View File
@@ -16,6 +16,17 @@
// ---------------- 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__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.
//
+7 -5
View File
@@ -72,9 +72,10 @@ const baseStrConvImplC = "" +
"= 0x7F.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 ..= 0xBF.\n\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.\n 0x00, 0x00, 0x00" +
", 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.\n // 0 1 2 3 4 5 6 7\n // 8 9 A B C D E F\n};\n\n" +
"" +
"// --------\n\nwuffs_base__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 uint8_t digit = wuffs_base__parse_number__decimal_digits[*p];\n if (digit == 0) {\n goto fail_bad_argument;\n }\n digit &= 0x0F;\n if ((v > max10) || ((v == max10) && (digit > max1))) {\n goto fail_out_of_bounds;\n }\n v = (10 * v) + ((uint64_t)(digit));\n }\n\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = v;\n return ret;\n } while (0);\n\nhexadecimal:\n do {\n uint64_t v = wuffs_base__parse_number__hexadecimal_digits[*p++];\n if (v == 0) {\n goto fail_bad_argument;\n }\n v &= 0x0F;\n\n for (; p < q; p++) {\n if (*p == '_') {\n continue;\n }\n uint8_t digit = wuffs_base__parse_number__hexadecimal_digits[*p];\n if (digit == 0) {\n goto fail_bad_argument;\n }\n digit &= 0x0F;\n if ((v >> 60) != 0) {\n " +
" goto fail_out_of_bounds;\n }\n v = (v << 4) | ((uint64_t)(digit));\n }\n\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = v;\n return ret;\n } while (0);\n\nok_zero:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_bad_argument:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_out_of_bounds:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = wuffs_base__error__out_of_bounds;\n ret.value = 0;\n return ret;\n } while (0);\n}\n" +
"// --------\n\nwuffs_base__result_i64 //\nwuffs_base__parse_number_i64(wuffs_base__slice_u8 s) {\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n for (; (p < q) && (*p == '_'); p++) {\n }\n\n bool negative = false;\n if (p >= q) {\n goto fail_bad_argument;\n } else if (*p == '-') {\n p++;\n negative = true;\n } else if (*p == '+') {\n p++;\n }\n\n wuffs_base__result_u64 r =\n wuffs_base__parse_number_u64(wuffs_base__make_slice_u8(p, q - p));\n if (r.status.repr != NULL) {\n wuffs_base__result_i64 ret;\n ret.status.repr = r.status.repr;\n ret.value = 0;\n return ret;\n } else if (negative) {\n if (r.value > 0x8000000000000000) {\n goto fail_out_of_bounds;\n }\n wuffs_base__result_i64 ret;\n ret.status.repr = NULL;\n ret.value = -(int64_t)(r.value);\n return ret;\n } else if (r.value > 0x7FFFFFFFFFFFFFFF) {\n goto fail_out_of_bounds;\n } else {\n wuffs_base__result_i64 ret;\n ret.status.repr = NULL;\n ret.value = +(int64_t)(r.value);\n return ret;\n }\n\nfail_bad_ar" +
"gument:\n do {\n wuffs_base__result_i64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_out_of_bounds:\n do {\n wuffs_base__result_i64 ret;\n ret.status.repr = wuffs_base__error__out_of_bounds;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\nwuffs_base__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 uint8_t digit = wuffs_base__parse_number__decimal_digits[*p];\n if (digit == 0) {\n goto fail_bad_argument;\n }\n digit &= 0x0F;\n if ((v > max10) || ((v == max10) && (digit > max1))) {\n goto fail_out_of_bounds;\n }\n v = (10 * v) + ((uint64_t)(digit));\n }\n\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = v;\n return ret;\n } while (0);\n\nhexadecimal:\n do {\n uint64_t v = wuffs_base__parse_number__hexadecimal_digits[*p++];\n if (v == 0) {\n got" +
"o 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 = "" +
@@ -273,8 +274,9 @@ const baseStrConvPrivateH = "" +
""
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" +
"// ---------------- String Conversions\n\n// wuffs_base__parse_number_i64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"-123\" then it will return the int64_t -123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow an int64_t.\n//\n// It is similar to wuffs_base__parse_number_u64 but it returns a signed\n// integer, not an unsigned integer. It also allows a leading '+' or '-'.\nwuffs_base__result_i64 //\nwuffs_base__parse_number_i64(wuffs_base__slice_u8 s);\n\n// wuffs_base__parse_number_u64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"123\" then it will return the uint64_t 123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow a uint64_t.\n//\n// It is similar to the C standard library's strtoull function, but:\n// - Errors are returned in-band (in a result type), not out-of-band (errno).\n// - It takes a slice (a pointer and length), not a NUL-terminated C string.\n" +
"// - It does not take an optional endptr argument. It does not allow a partial\n// parse: it returns an error unless all of s is consumed.\n// - It does not allow whitespace, leading or otherwise.\n// - It does not allow a leading '+' or '-'.\n// - It does not allow 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// \"0x9aBC\"). The caller is responsible for prior filtering of e.g. hex\n// numbers if they are unwanted. For example, Wuffs' JSON decoder will only\n// produce a wuffs_base__token for decimal numbers, not hexadecimal.\n// - It is not affected by i18n / l10n settings.\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 = "" +
+70
View File
@@ -3069,6 +3069,17 @@ wuffs_base__pixel_swizzler::swizzle_interleaved(
// ---------------- 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__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.
//
@@ -8082,6 +8093,65 @@ static const uint8_t wuffs_base__parse_number__hexadecimal_digits[256] = {
// --------
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++;
}
wuffs_base__result_u64 r =
wuffs_base__parse_number_u64(wuffs_base__make_slice_u8(p, 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;
}
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__result_u64 //
wuffs_base__parse_number_u64(wuffs_base__slice_u8 s) {
uint8_t* p = s.ptr;
+49
View File
@@ -69,6 +69,54 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the
// ---------------- String Conversions Tests
const char* test_strconv_parse_number_i64() {
CHECK_FOCUS(__func__);
const int64_t fail = 0xDEADBEEF;
struct {
int64_t want;
const char* str;
} test_cases[] = {
{.want = +0x0000000000000000, .str = "+0"},
{.want = +0x0000000000000000, .str = "-0"},
{.want = +0x0000000000000000, .str = "0"},
{.want = +0x000000000000012C, .str = "+300"},
{.want = +0x7FFFFFFFFFFFFFFF, .str = "+9223372036854775807"},
{.want = +0x7FFFFFFFFFFFFFFF, .str = "9223372036854775807"},
{.want = -0x0000000000000002, .str = "-2"},
{.want = -0x00000000000000AB, .str = "_-_0x_AB"},
{.want = -0x7FFFFFFFFFFFFFFF, .str = "-9223372036854775807"},
{.want = -0x8000000000000000, .str = "-9223372036854775808"},
{.want = fail, .str = "+ 1"},
{.want = fail, .str = "++1"},
{.want = fail, .str = "+-1"},
{.want = fail, .str = "+9223372036854775808"}, // 1 << 63.
{.want = fail, .str = "-"},
{.want = fail, .str = "-+1"},
{.want = fail, .str = "-0x8000000000000001"}, // -((1 << 63) + 1).
{.want = fail, .str = "-9223372036854775809"}, // -((1 << 63) + 1).
{.want = fail, .str = "0x8000000000000000"}, // 1 << 63.
{.want = fail, .str = "1-"},
{.want = fail, .str = "9223372036854775808"}, // 1 << 63.
};
int i;
for (i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); i++) {
wuffs_base__result_i64 r =
wuffs_base__parse_number_i64(wuffs_base__make_slice_u8(
(void*)test_cases[i].str, strlen(test_cases[i].str)));
int64_t have = (r.status.repr == NULL) ? r.value : fail;
if (have != test_cases[i].want) {
RETURN_FAIL("\"%s\": have 0x%" PRIX64 ", want 0x%" PRIX64,
test_cases[i].str, have, test_cases[i].want);
}
}
return NULL;
}
const char* test_strconv_parse_number_u64() {
CHECK_FOCUS(__func__);
@@ -324,6 +372,7 @@ 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_i64, //
test_strconv_parse_number_u64, //
test_wuffs_json_decode_tokens, //