Skip to content

IntFromStr

Description

Compatibility wrapper for IntTryFromStr(...).

Success

Returns Parsed integer value, or zero on failure.

Failure

Returns a zero-initialised Int on a non-decimal character, an empty digit run, or an allocation failure. Use IntTryFromStr(...) when explicit failure propagation is required.

Usage example (Cross-references)

Usage examples (Cross-references)
        Float value = {
            .negative    = false,
            .significand = IntFromStr("50", &alloc.base),
            .exponent    = -1,
        };
    
        Float value  = FloatFromStr("0", &alloc.base);
        Int   result = IntFromStr("999", &alloc.base);
    
        bool ok    = FloatToInt(&result, &value);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int   integer = IntFromStr("12345678901234567890", ALLOCATOR_OF(&alloc));
        Float value   = float_from_int(&integer, ALLOCATOR_OF(&alloc));
        Str   text    = FloatToStr(&value);
    
        Float value = FloatFrom(4503599627370496.0, &alloc.base); // 2^52, binexp == 0
        Int   whole = IntFromStr("4503599627370496", &alloc.base);
    
        bool result = (FloatExponent(&value) == 0);
        Int large = IntFrom(1u, &alloc.base);
        IntShiftLeft(&large, 80);
        Int decimal = IntFromStr("12345678901234567890", &alloc.base);
    
        u64 h_zero    = int_hash(&zero, 0);
        Int rhs          = IntFrom(2, &alloc.base);
        Int result_value = IntInit(&alloc.base);
        Int huge         = IntFromStr("123456789012345678901234567890", &alloc.base);
        Str text         = StrInit(&alloc.base);
        Int result_value = IntInit(&alloc.base);
        Int preserved    = IntFrom(99, &alloc.base);
        Int huge         = IntFromStr("12345678901234567890", &alloc.base);
        Str text         = StrInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value        = IntFromStr("12345678901234567890", &alloc.base);
        Int result_value = IntInit(&alloc.base);
        Str text         = StrInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend  = IntFromStr("12345678901234567890", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
        Int remainder = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend     = IntFromStr("12345678901234567890", &alloc.base);
        Int result_value = IntInit(&alloc.base);
        Str text         = StrInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend  = IntFromStr("12345678901234567890", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
        Int remainder = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value     = IntFromStr("12345678901234567890", &alloc.base);
        Int remainder = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int prime     = IntFromStr("1000000007", &alloc.base);
        Int composite = IntFrom(561, &alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromStr("1000000000", &alloc.base);
        Int next  = IntInit(&alloc.base);
        Str text  = StrInit(&alloc.base);
        Int base = IntFrom(2, &alloc.base);
        // 2**64 == 18446744073709551616 -> bit length 65, does not fit u64.
        Int exp      = IntFromStr("18446744073709551616", &alloc.base);
        Int power    = IntFrom(123, &alloc.base);
        Int sentinel = IntFrom(123, &alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend  = IntFromStr("12345678901234567890", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
        Int remainder = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend  = IntFromStr("12345678901234567890", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
        Int remainder = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend     = IntFromStr("12345678901234567890", &alloc.base);
        Int result_value = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromStr("1373653", &alloc.base);
    
        bool result = !IntIsProbablePrime(&value);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromStr("1000000007", &alloc.base);
    
        bool result = IntIsProbablePrime(&value);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend  = IntFromStr("12345678901234567890123456789", &alloc.base);
        Int divisor   = IntFromStr("987654321987654321", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
    
        Int dividend  = IntFromStr("12345678901234567890123456789", &alloc.base);
        Int divisor   = IntFromStr("987654321987654321", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
        Int remainder = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend  = IntFromStr("1000000007", &alloc.base);
        Int divisor   = IntFromStr("97", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
    
        Int dividend  = IntFromStr("1000000007", &alloc.base);
        Int divisor   = IntFromStr("97", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
        Int remainder = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend  = IntFromStr("11975308533", &alloc.base);
        Int divisor   = IntFromStr("97", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
    
        Int dividend  = IntFromStr("11975308533", &alloc.base);
        Int divisor   = IntFromStr("97", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
        Int remainder = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend  = IntFromStr("42", &alloc.base);
        Int divisor   = IntFromStr("1000", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
    
        Int dividend  = IntFromStr("42", &alloc.base);
        Int divisor   = IntFromStr("1000", &alloc.base);
        Int quotient  = IntInit(&alloc.base);
        Int remainder = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend  = IntFromStr("12345", &alloc.base);
        Int divisor   = IntInit(&alloc.base); /* zero */
        Int quotient  = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend  = IntFromStr("100", &alloc.base);
        Int divisor   = IntFromStr("7", &alloc.base);
        Int remainder = IntInit(&alloc.base);
    
        Int dividend  = IntFromStr("100", &alloc.base);
        Int divisor   = IntFromStr("7", &alloc.base);
        Int remainder = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int dividend = IntFromStr("100", &alloc.base);
        Int divisor  = IntFromStr("7", &alloc.base);
        Int quotient = IntInit(&alloc.base);
    
        Int dividend = IntFromStr("100", &alloc.base);
        Int divisor  = IntFromStr("7", &alloc.base);
        Int quotient = IntInit(&alloc.base);
    
        Zstr digits = "123456789012345678901234567890";
        Int  value  = IntFromStr(digits, ALLOCATOR_OF(&alloc));
        Str  text   = IntToStr(&value);
    
    bool test_int_from_decimal_invalid_digit(void) {
        WriteFmt("Testing IntFromStr invalid digit handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  parsed = IntFromStr("12x3", ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool result = !IntTryFromStr(&value, "12x3");
    
    bool test_int_from_decimal_null(void) {
        WriteFmt("Testing IntFromStr NULL handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        IntFromStr((Zstr)NULL, ALLOCATOR_OF(&alloc));
        DefaultAllocatorDeinit(&alloc);
        return false;
        Int small = IntFrom(7u, &alloc.base);
        Int mid   = IntFrom(255u, &alloc.base);
        Int big   = IntFromStr("123456789012345678901234567890", &alloc.base);
    
        Str dec_small = IntToStrRadix(&small, 10, false, &alloc.base);
    
        Str digits = StrInitFromZstr("12345", &alloc.base);
        Int value  = IntFromStr(&digits, &alloc.base);
    
        bool result = (IntToU64(&value) == (u64)12345u);
        Allocator     *alloc = ALLOCATOR_OF(&dbg);
    
        Int value  = IntFromStr("123456789012345678901234567890", alloc);
        Str hex    = IntToStrRadix(&value, 16, false, alloc);
        Int parsed = IntFromStrRadix(&hex, 16, alloc);
        bool success = true;
    
        Int big_dec = IntFromStr("123456789012345678901234567890", alloc_base);
        Int hex_val = IntFromHexStr("deadbeefcafebabe1234", alloc_base);
        Int bin_val = IntFromBinary("10100011", alloc_base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  v = IntFromStr("123", &alloc);
        Zstr z = "9_";
        StrReadFmt(z, "{}", v);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  v = IntFromStr("200", &alloc);
        Zstr z = "0xFF";
        StrReadFmt(z, "{x}", v);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  v = IntFromStr("200", &alloc);
        Zstr z = "0XFF";
        StrReadFmt(z, "{x}", v);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  v = IntFromStr("5", &alloc);
        Zstr z = "0b1";
        StrReadFmt(z, "{b}", v);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  v = IntFromStr("5", &alloc);
        Zstr z = "0B1";
        StrReadFmt(z, "{b}", v);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  v = IntFromStr("9", &alloc);
        Zstr z = "0o7";
        StrReadFmt(z, "{o}", v);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  v = IntFromStr("9", &alloc);
        Zstr z = "0O7";
        StrReadFmt(z, "{o}", v);
    static bool test_int_write_no_leak(void) {
        DBG_BEGIN(dbg, out);
        Int v = IntFromStr("123456789012345678901234567890", &dbg.base);
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "123456789012345678901234567890") == 0);
    bool test_leak_write_int_freed(void) {
        HeapAllocator va = HeapAllocatorInit();
        Int           v  = IntFromStr("123456789012345678901234567890", ALLOCATOR_OF(&va));
        LEAK_WRITE_PRELUDE();
        ok = ok && StrAppendFmt(&out, "{}", v) && (StrLen(&out) > 0);
        // Seed the destination with a heap-backed Int (multi-limb) so the old
        // storage is visible in dbg's live count before the reassignment.
        Int value = IntFromStr("123456789012345678901234567890", adbg);
    
        Zstr input = "42";
Last updated on