StrToU64
Description
Convert string to unsigned 64-bit integer
Parameters
| Name | Direction | Description |
|---|---|---|
str |
in | String to convert |
value |
out | Where to store the result |
config |
in | Parse configuration (NULL for auto-detect base) |
Success
Returns true and writes the parsed value into *value
Failure
Returns false when the input cannot be parsed; *value is left unchanged
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Io.c:2825:
// rejected a bare prefix with no digits.
u64 val;
if (!StrToU64(&temp, &val, NULL)) {
LOG_ERROR("Failed to parse u8");
StrDeinit(&temp);- In
Io.c:2962:
}
_MAKE_READ_TXT_INT(u16, u16, u64, StrToU64, _U_BOUND(u16, UINT16_MAX))
_MAKE_READ_TXT_INT(u32, u32, u64, StrToU64, _U_BOUND(u32, UINT32_MAX))
_MAKE_READ_TXT_INT(u64, u64, u64, StrToU64, /* val is already u64; no bound */)- In
Io.c:2963:
_MAKE_READ_TXT_INT(u16, u16, u64, StrToU64, _U_BOUND(u16, UINT16_MAX))
_MAKE_READ_TXT_INT(u32, u32, u64, StrToU64, _U_BOUND(u32, UINT32_MAX))
_MAKE_READ_TXT_INT(u64, u64, u64, StrToU64, /* val is already u64; no bound */)
_MAKE_READ_TXT_INT(i8, i8, i64, StrToI64, _I_BOUND(i8, INT8_MIN, INT8_MAX))- In
Io.c:3227:
u64 value;
StrParseConfig config = {.base = 16};
if (!StrToU64(&hex_str, &value, &config)) {
LOG_ERROR("Failed to parse hex value");
StrDeinit(&hex_str);- In
Io.c:3268:
u64 value;
StrParseConfig config = {.base = 8};
if (!StrToU64(&oct_str, &value, &config)) {
LOG_ERROR("Failed to parse octal value");
StrDeinit(&oct_str);- In
Str.c:862:
}
bool StrToU64(const Str *str, u64 *value, const StrParseConfig *config) {
ValidateStr(str);- In
Str.c:985:
u64 unsigned_value;
if (!StrToU64(&temp_str, &unsigned_value, config)) {
return false;
}- In
Convert.c:240:
// Test StrToU64 function
bool test_str_to_u64(void) {
WriteFmt("Testing StrToU64\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:247:
Str s = StrInitFromZstr("12345", &alloc);
u64 value = 0;
bool success = StrToU64(&s, &value, NULL);
bool result = (success && value == 12345);- In
Convert.c:254:
s = StrInitFromZstr("ABCD", &alloc); // No 0x prefix when base is explicitly 16
StrParseConfig config = {.base = 16};
success = StrToU64(&s, &value, &config);
result = result && (success && value == 0xABCD);- In
Convert.c:260:
StrDeinit(&s);
s = StrInitFromZstr("0xABCD", &alloc);
success = StrToU64(&s, &value, NULL);
result = result && (success && value == 0xABCD);- In
Convert.c:266:
StrDeinit(&s);
s = StrInitFromZstr("0b101010", &alloc);
success = StrToU64(&s, &value, NULL);
result = result && (success && value == 42);- In
Convert.c:272:
StrDeinit(&s);
s = StrInitFromZstr("0o52", &alloc);
success = StrToU64(&s, &value, NULL);
result = result && (success && value == 42);- In
Convert.c:278:
StrDeinit(&s);
s = StrInitFromZstr("0", &alloc);
success = StrToU64(&s, &value, NULL);
result = result && (success && value == 0);- In
Convert.c:284:
StrDeinit(&s);
s = StrInitFromZstr("not a number", &alloc);
success = StrToU64(&s, &value, NULL);
result = result && (!success);- In
Convert.c:290:
StrDeinit(&s);
s = StrInitFromZstr("-123", &alloc);
success = StrToU64(&s, &value, NULL);
result = result && (!success);- In
Convert.c:441:
StrFromU64(&s, u64_values[i], &config);
u64 recovered_u64 = 0;
bool success = StrToU64(&s, &recovered_u64, NULL);
result = result && success && (recovered_u64 == u64_values[i]);- In
Convert.c:449:
StrFromU64(&s, u64_values[i], &config);
recovered_u64 = 0;
success = StrToU64(&s, &recovered_u64, NULL); // Can now use explicit base 16 with 0x prefix
result = result && success && (recovered_u64 == u64_values[i]);- In
Convert.c:511:
// For bases with prefixes, use auto-detect. For others, use explicit base parsing
StrParseConfig parse_config = {.base = base};
bool success = StrToU64(&s, &recovered, config.use_prefix ? NULL : &parse_config);
result = result && success && (recovered == base);- In
Convert.c:524:
StrFromU64(&s, UINT64_MAX, &config);
u64 recovered_max = 0;
bool success = StrToU64(&s, &recovered_max, NULL);
result = result && success && (recovered_max == UINT64_MAX);- In
Convert.c:571:
u64 value = 0;
StrParseConfig config = {.base = prefix_tests[i].base};
bool success = StrToU64(&test_str, &value, prefix_tests[i].base == 0 ? NULL : &config);
result = result && success && (value == prefix_tests[i].expected);
StrDeinit(&test_str);- In
Convert.c:640:
u64 recovered = 0;
StrParseConfig pconfig = {.base = base};
bool success = StrToU64(&s, &recovered, &pconfig); // Can now use explicit base with prefixes
result = result && success && (recovered == large_value);- In
Convert.c:672:
u64 recovered = 0;
StrParseConfig parse_config = {.base = base};
bool success = StrToU64(&s, &recovered, &parse_config);
result = result && success && (recovered == test_value);- In
Convert.c:688:
u64 recovered = 0;
StrParseConfig parse_config = {.base = base};
bool success = StrToU64(&s, &recovered, &parse_config);
result = result && success && (recovered == test_value);- In
Convert.c:707:
u64 recovered = 0;
StrParseConfig parse_config = {.base = base};
bool success = StrToU64(&s, &recovered, &parse_config);
result = result && success && (recovered == test_values[i]);- In
Convert.c:740:
u64 recovered = 0;
bool success = StrToU64(&s, &recovered, NULL);
result = result && success && (recovered == test_value);- In
Convert.c:783:
Str long_str = StrInitFromZstr(long_number, &alloc);
u64 long_value = 0;
bool success = StrToU64(&long_str, &long_value, NULL);
// This might overflow, but should handle gracefully
result = result && (success || !success); // Either succeeds or fails gracefully
- In
Convert.c:1095:
// (5 < 99). Real code rejects base 99; the mutant parses "5" as 5.
static bool test_str_to_u64_invalid_base_rejected(void) {
WriteFmt("Testing StrToU64 rejects an out-of-range base\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1101:
u64 value = 0;
StrParseConfig config = {.base = 99};
bool ok = StrToU64(&s, &value, &config);
bool result = (!ok);- In
Convert.c:1113:
// parse of "ff" must succeed with value 255.
static bool test_str_to_u64_valid_base_sixteen_accepted(void) {
WriteFmt("Testing StrToU64 accepts explicit base 16\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1119:
u64 value = 0;
StrParseConfig config = {.base = 16};
bool ok = StrToU64(&s, &value, &config);
bool result = (ok && value == 255);- In
Convert.c:1131:
// digit and is rejected. Real code parses 42.
static bool test_str_to_u64_leading_space_skipped(void) {
WriteFmt("Testing StrToU64 skips leading whitespace\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1136:
Str s = StrInitFromZstr(" 42", &alloc);
u64 value = 0;
bool ok = StrToU64(&s, &value, NULL);
bool result = (ok && value == 42);- In
Convert.c:1148:
// falls into the empty-string path. Real code parses 42.
static bool test_str_to_u64_leading_space_advance(void) {
WriteFmt("Testing StrToU64 advances over leading whitespace\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1153:
Str s = StrInitFromZstr(" 42", &alloc);
u64 value = 0;
bool ok = StrToU64(&s, &value, NULL);
bool result = (ok && value == 42);- In
Convert.c:1166:
// decimal 5. Mutant skips "0","5", finds no digits, returns false.
static bool test_str_to_u64_zero_five_is_decimal(void) {
WriteFmt("Testing StrToU64 parses 05 as decimal\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1171:
Str s = StrInitFromZstr("05", &alloc);
u64 value = 0;
bool ok = StrToU64(&s, &value, NULL);
bool result = (ok && value == 5);- In
Convert.c:1183:
// digits and must be rejected. Mutant returns true with value 0.
static bool test_str_to_u64_prefix_only_rejected(void) {
WriteFmt("Testing StrToU64 rejects bare 0x prefix\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1188:
Str s = StrInitFromZstr("0x", &alloc);
u64 value = 0;
bool ok = StrToU64(&s, &value, NULL);
bool result = (!ok);- In
Convert.c:1200:
// slip through. UINT64_MAX+1 == "18446744073709551616" must be rejected.
static bool test_str_to_u64_overflow_rejected(void) {
WriteFmt("Testing StrToU64 rejects u64 overflow\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1205:
Str s = StrInitFromZstr("18446744073709551616", &alloc);
u64 value = 0;
bool ok = StrToU64(&s, &value, NULL);
bool result = (!ok);- In
Convert.c:1217:
// must parse to 42. Mutant returns false.
static bool test_str_to_u64_have_digits_set(void) {
WriteFmt("Testing StrToU64 marks digits consumed\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1222:
Str s = StrInitFromZstr("42", &alloc);
u64 value = 0;
bool ok = StrToU64(&s, &value, NULL);
bool result = (ok && value == 42);- In
Convert.c:1234:
// wrongly reports extra characters for "42 ". Real code parses 42.
static bool test_str_to_u64_trailing_space_skipped_strict(void) {
WriteFmt("Testing StrToU64 skips trailing whitespace in strict mode\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1240:
u64 value = 0;
StrParseConfig config = {.strict = true};
bool ok = StrToU64(&s, &value, &config);
bool result = (ok && value == 42);- In
Convert.c:1252:
// strict mode wrongly rejects "42 ". Real code parses 42.
static bool test_str_to_u64_trailing_space_advance_strict(void) {
WriteFmt("Testing StrToU64 advances over trailing whitespace in strict mode\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1258:
u64 value = 0;
StrParseConfig config = {.strict = true};
bool ok = StrToU64(&s, &value, &config);
bool result = (ok && value == 42);- In
Convert.c:1270:
// trailing junk. "42x" in strict mode must be rejected. Mutant returns true.
static bool test_str_to_u64_strict_rejects_trailing_junk(void) {
WriteFmt("Testing StrToU64 strict mode rejects trailing junk\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1276:
u64 value = 0;
StrParseConfig config = {.strict = true};
bool ok = StrToU64(&s, &value, &config);
bool result = (!ok);- In
Convert.c:1288:
// including a clean "42". Real code parses 42.
static bool test_str_to_u64_strict_accepts_clean(void) {
WriteFmt("Testing StrToU64 strict mode accepts a clean number\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1294:
u64 value = 0;
StrParseConfig config = {.strict = true};
bool ok = StrToU64(&s, &value, &config);
bool result = (ok && value == 42);- In
Convert.c:1306:
// mutant the barrier is gone and the corruption is not caught at entry.
static bool test_str_to_u64_validate_barrier(void) {
WriteFmt("Testing StrToU64 validate barrier aborts on corrupt Str\n");
Str bad = {0}; // zeroed magic: ValidateStr must LOG_FATAL
- In
Convert.c:1311:
u64 value = 0;
StrParseConfig config = {.base = 10};
StrToU64(&bad, &value, &config);
return false; // unreachable on real code (aborts above)
- In
Convert.c:1564:
StrParseConfig cfg = {.base = 16};
u64 out = 0;
bool ok = StrToU64(&s, &out, &cfg);
bool pass = ok && out == 31;
StrDeinit(&s);- In
Convert.c:1579:
StrParseConfig cfg = {.base = 16};
u64 out = 0;
bool ok = StrToU64(&s, &out, &cfg);
bool pass = ok && out == 5;
StrDeinit(&s);- In
Convert.c:1594:
StrParseConfig cfg = {.base = 16};
u64 out = 0;
bool ok = StrToU64(&s, &out, &cfg);
bool pass = ok && out == 5;
StrDeinit(&s);- In
Convert.c:1607:
StrParseConfig cfg = {.base = 2};
u64 out = 0;
bool ok = StrToU64(&s, &out, &cfg);
bool pass = ok && out == 1;
StrDeinit(&s);- In
Convert.c:1621:
StrParseConfig cfg = {.base = 2};
u64 out = 0;
bool ok = StrToU64(&s, &out, &cfg);
bool pass = ok && out == 1;
StrDeinit(&s);- In
Convert.c:1634:
StrParseConfig cfg = {.base = 8};
u64 out = 0;
bool ok = StrToU64(&s, &out, &cfg);
bool pass = ok && out == 7;
StrDeinit(&s);- In
Convert.c:1647:
StrParseConfig cfg = {.base = 8};
u64 out = 0;
bool ok = StrToU64(&s, &out, &cfg);
bool pass = ok && out == 7;
StrDeinit(&s);- In
Convert.c:1660:
StrParseConfig cfg = {.base = 8};
u64 out = 0;
bool ok = StrToU64(&s, &out, &cfg);
bool pass = ok && out == 7;
StrDeinit(&s);- In
Convert.c:1673:
StrParseConfig cfg = {.base = 16};
u64 out = 0;
bool ok = StrToU64(&s, &out, &cfg);
bool pass = ok && out == 31;
StrDeinit(&s);- In
Convert.c:1686:
StrParseConfig cfg = {.base = 16};
u64 out = 0;
bool ok = StrToU64(&s, &out, &cfg);
bool pass = ok && out == 31;
StrDeinit(&s);- In
Convert.c:1699:
StrParseConfig cfg = {.base = 16};
u64 out = 0;
bool ok = StrToU64(&s, &out, &cfg);
bool pass = ok && out == 31;
StrDeinit(&s);- In
Convert.c:1825:
u64 value = 999; // sentinel
StrParseConfig config = {.base = 2};
bool success = StrToU64(&s, &value, &config);
// Real code: '2' is invalid in base 2 -> parse fails. The mutant accepts
// digit value 2 and returns success.
- In
Convert.c:2076:
// and fails.
static bool test_to_u64_base2_uppercase_prefix_skipped(void) {
WriteFmt("Testing StrToU64 skips 0B prefix on explicit base 2 (533:51)\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:2082:
StrParseConfig config = {.base = 2};
u64 value = 0;
bool ok = StrToU64(&s, &value, &config);
// Real: uppercase prefix skipped, "101" base 2 == 5.
- In
Convert.c:2215:
test_str_conversion_invalid_input_failures,
test_validate_str_guard, // StrToF64 (Str.Mutants1)
test_str_to_u64_validate_barrier, // StrToU64 (Str.Mutants2)
test_from_f64_null_aborts, // StrFromF64 (Str.Mutants3)
test_from_f64_null_aborts_high_precision,
Last updated on