Skip to content

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)
    _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))
    _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 */)
    _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 */)
    }
    
    bool StrToI64(const Str *str, i64 *value, const StrParseConfig *config) {
        ValidateStr(str);
    // Test StrToI64 function
    bool test_str_to_i64(void) {
        WriteFmt("Testing StrToI64\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str  s       = StrInitFromZstr("12345", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        bool result  = (success && value == 12345);
        StrDeinit(&s);
        s       = StrInitFromZstr("-12345", &alloc);
        success = StrToI64(&s, &value, NULL);
        result  = result && (success && value == -12345);
        StrDeinit(&s);
        s       = StrInitFromZstr("0xABCD", &alloc);
        success = StrToI64(&s, &value, NULL);
        result  = result && (success && value == 0xABCD);
        StrDeinit(&s);
        s       = StrInitFromZstr("0b101010", &alloc);
        success = StrToI64(&s, &value, NULL);
        result  = result && (success && value == 42);
        StrDeinit(&s);
        s       = StrInitFromZstr("0", &alloc);
        success = StrToI64(&s, &value, NULL);
        result  = result && (success && value == 0);
        StrDeinit(&s);
        s       = StrInitFromZstr("not a number", &alloc);
        success = StrToI64(&s, &value, NULL);
        result  = result && (!success);
            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]);
        StrFromI64(&s, INT64_MIN + 1, &config);
        i64 recovered_min = 0;
        success           = StrToI64(&s, &recovered_min, NULL);
        result            = result && success && (recovered_min == INT64_MIN + 1);
    // 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();
        Str  s       = StrInitFromZstr("  -5", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        bool result  = success && (value == -5);
    // 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();
        Str  s       = StrInitFromZstr("  5", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        bool result  = success && (value == 5);
    // 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();
        Str  s       = StrInitFromZstr("-5", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        bool result  = success && (value == -5);
    // 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();
        Str  s       = StrInitFromZstr("+5", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        bool result  = success && (value == 5);
    // 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();
    
        i64  value   = 7; // sentinel that must be overwritten to 0
        bool success = StrToI64(&s, &value, NULL);
        bool result  = success && (value == 0) && (StrLen(&s) == 43);
    // 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();
        Str  s       = StrInitFromZstr("-9223372036854775808", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        bool result  = success && (value == INT64_MIN);
    // 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();
    
        i64 value = 0;
        StrToI64(&s, &value, NULL); // should abort here
    
        return false;               // unreachable on real code
            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