StrToI64
Description
Convert string to signed 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:2965:
_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))
_MAKE_READ_TXT_INT(i16, i16, i64, StrToI64, _I_BOUND(i16, INT16_MIN, INT16_MAX))
_MAKE_READ_TXT_INT(i32, i32, i64, StrToI64, _I_BOUND(i32, INT32_MIN, INT32_MAX))- In
Io.c:2966:
_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))
_MAKE_READ_TXT_INT(i16, i16, i64, StrToI64, _I_BOUND(i16, INT16_MIN, INT16_MAX))
_MAKE_READ_TXT_INT(i32, i32, i64, StrToI64, _I_BOUND(i32, INT32_MIN, INT32_MAX))
_MAKE_READ_TXT_INT(i64, i64, i64, StrToI64, /* val is already i64; no bound */)- In
Io.c:2967:
_MAKE_READ_TXT_INT(i8, i8, i64, StrToI64, _I_BOUND(i8, INT8_MIN, INT8_MAX))
_MAKE_READ_TXT_INT(i16, i16, i64, StrToI64, _I_BOUND(i16, INT16_MIN, INT16_MAX))
_MAKE_READ_TXT_INT(i32, i32, i64, StrToI64, _I_BOUND(i32, INT32_MIN, INT32_MAX))
_MAKE_READ_TXT_INT(i64, i64, i64, StrToI64, /* val is already i64; no bound */)- In
Str.c:949:
}
bool StrToI64(const Str *str, i64 *value, const StrParseConfig *config) {
ValidateStr(str);- In
Convert.c:300:
// Test StrToI64 function
bool test_str_to_i64(void) {
WriteFmt("Testing StrToI64\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:307:
Str s = StrInitFromZstr("12345", &alloc);
i64 value = 0;
bool success = StrToI64(&s, &value, NULL);
bool result = (success && value == 12345);- In
Convert.c:313:
StrDeinit(&s);
s = StrInitFromZstr("-12345", &alloc);
success = StrToI64(&s, &value, NULL);
result = result && (success && value == -12345);- In
Convert.c:319:
StrDeinit(&s);
s = StrInitFromZstr("0xABCD", &alloc);
success = StrToI64(&s, &value, NULL);
result = result && (success && value == 0xABCD);- In
Convert.c:325:
StrDeinit(&s);
s = StrInitFromZstr("0b101010", &alloc);
success = StrToI64(&s, &value, NULL);
result = result && (success && value == 42);- In
Convert.c:331:
StrDeinit(&s);
s = StrInitFromZstr("0", &alloc);
success = StrToI64(&s, &value, NULL);
result = result && (success && value == 0);- In
Convert.c:337:
StrDeinit(&s);
s = StrInitFromZstr("not a number", &alloc);
success = StrToI64(&s, &value, NULL);
result = result && (!success);- In
Convert.c:462:
StrFromI64(&s, i64_values[i], &config);
i64 recovered_i64 = 0;
bool success = StrToI64(&s, &recovered_i64, NULL);
result = result && success && (recovered_i64 == i64_values[i]);- In
Convert.c:532:
StrFromI64(&s, INT64_MIN + 1, &config);
i64 recovered_min = 0;
success = StrToI64(&s, &recovered_min, NULL);
result = result && success && (recovered_min == INT64_MIN + 1);- In
Convert.c:1712:
// missed. " -5" must still parse to -5.
static bool test_str_to_i64_leading_space_negative(void) {
WriteFmt("Testing StrToI64 strips leading spaces before negative sign\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1717:
Str s = StrInitFromZstr(" -5", &alloc);
i64 value = 0;
bool success = StrToI64(&s, &value, NULL);
bool result = success && (value == -5);- In
Convert.c:1729:
// must still parse to 5.
static bool test_str_to_i64_leading_space_positive(void) {
WriteFmt("Testing StrToI64 advances past leading spaces\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1734:
Str s = StrInitFromZstr(" 5", &alloc);
i64 value = 0;
bool success = StrToI64(&s, &value, NULL);
bool result = success && (value == 5);- In
Convert.c:1745:
// the '-' branch, so a negative input is stored positive. "-5" must be -5.
static bool test_str_to_i64_negative_sign(void) {
WriteFmt("Testing StrToI64 honors the negative sign\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1750:
Str s = StrInitFromZstr("-5", &alloc);
i64 value = 0;
bool success = StrToI64(&s, &value, NULL);
bool result = success && (value == -5);- In
Convert.c:1761:
// wrapping pos and building a garbage substring view. "+5" must parse to 5.
static bool test_str_to_i64_plus_sign(void) {
WriteFmt("Testing StrToI64 skips a leading plus sign\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1766:
Str s = StrInitFromZstr("+5", &alloc);
i64 value = 0;
bool success = StrToI64(&s, &value, NULL);
bool result = success && (value == 5);- In
Convert.c:1782:
// the mutant the abort kills the process and this normal test never returns.
static bool test_str_to_i64_long_zero_view(void) {
WriteFmt("Testing StrToI64 sets the borrowed view capacity to its length\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1790:
i64 value = 7; // sentinel that must be overwritten to 0
bool success = StrToI64(&s, &value, NULL);
bool result = success && (value == 0) && (StrLen(&s) == 43);- In
Convert.c:1802:
// INT64_MIN ("-9223372036854775808") must still parse.
static bool test_str_to_i64_int64_min(void) {
WriteFmt("Testing StrToI64 accepts INT64_MIN\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1807:
Str s = StrInitFromZstr("-9223372036854775808", &alloc);
i64 value = 0;
bool success = StrToI64(&s, &value, NULL);
bool result = success && (value == INT64_MIN);- In
Convert.c:1838:
// StrToI64. A corrupted Str (length > capacity, dirty magic) must abort.
static bool test_str_to_i64_corrupt_str_aborts(void) {
WriteFmt("Testing StrToI64 validates its Str argument (should abort)\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1849:
i64 value = 0;
StrToI64(&s, &value, NULL); // should abort here
return false; // unreachable on real code
- In
Convert.c:2218:
test_from_f64_null_aborts, // StrFromF64 (Str.Mutants3)
test_from_f64_null_aborts_high_precision,
test_str_to_i64_corrupt_str_aborts // StrToI64 (Str.Mutants5)
};
Last updated on