Skip to content
IntTryFromStrRadix

IntTryFromStrRadix

Description

Parse digits in the given radix into an integer. Supports radices from 2 through 36 and ignores underscore separators. An optional leading + sign is consumed by callers; this entry point itself does not accept a sign.

Parameters

Name Direction Description
out in,out Int to overwrite with the parsed value. Must already be a valid initialised Int; its allocator is reused for the parsed result.
digits in Null-terminated digit string (no base prefix).
radix in Output radix in the range 2..36.

Success

Returns true. *out is deinitialised and replaced with the normalised parsed value.

Failure

Returns false on an invalid radix, an invalid digit for the radix, an empty digit run, or an allocation failure during accumulation. *out retains its pre-call value.

Usage example (Cross-references)

Usage examples (Cross-references)
        Str  temp   = StrInitFromCstr(start, StrIterIndex(&si) - StrIterIndex(&saved), IntAllocator(value));
        Int  parsed = IntInit(IntAllocator(value));
        bool ok     = IntTryFromStrRadix(&parsed, StrBegin(&temp), radix);
    
        if (!ok) {
        Int  parsed = IntFromStrRadix("102", 2, ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool result = !IntTryFromStrRadix(&value, "102", 2);
    
        result = result && IntIsZero(&parsed);
        Int  parsed = IntFromStrRadix("10", 1, ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool result = !IntTryFromStrRadix(&value, "10", 1);
    
        result = result && IntIsZero(&parsed);
    
    bool test_int_try_from_radix_null(void) {
        WriteFmt("Testing IntTryFromStrRadix NULL handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntInit(ALLOCATOR_OF(&alloc));
        IntTryFromStrRadix(&value, (Zstr)NULL, 10);
        IntDeinit(&value);
        DefaultAllocatorDeinit(&alloc);
    //
    bool test_m12_radix_underscore_only_rejected(void) {
        WriteFmt("Testing IntTryFromStrRadix rejects underscore-only input\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool result = !IntTryFromStrRadix(&value, "_", 10);
    
        result = result && IntIsZero(&value);
    //
    bool test_m12_radix_valid_digit_parses(void) {
        WriteFmt("Testing IntTryFromStrRadix accepts a valid digit run\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool ok     = IntTryFromStrRadix(&value, "7", 10);
        bool result = ok && (IntToU64(&value) == 7);
    //
    bool test_m12_radix_invalid_radix_rejected(void) {
        WriteFmt("Testing IntTryFromStrRadix radix-range gate\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int invalid = IntInit(ALLOCATOR_OF(&alloc));
    
        bool ok_valid       = IntTryFromStrRadix(&valid, "5", 10);
        bool rejected_radix = !IntTryFromStrRadix(&invalid, "5", 37);
    
        bool ok_valid       = IntTryFromStrRadix(&valid, "5", 10);
        bool rejected_radix = !IntTryFromStrRadix(&invalid, "5", 37);
    
        bool result = ok_valid && (IntToU64(&valid) == 5);
    
        Int  out    = IntInit(&alloc.base);
        bool parsed = IntTryFromStrRadix(&out, "AZ", 36);
    
        bool result = parsed && (IntToU64(&out) == 395);
    
        Int  out    = IntInit(&alloc.base);
        bool parsed = IntTryFromStrRadix(&out, "FF", 16);
    
        bool result = parsed && (IntToU64(&out) == 255);
        Str  digits = StrInitFromZstr("123", ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool ok     = IntTryFromStrRadix(&value, &digits, 10);
    
        bool result = ok;
        Str  digits = StrInitFromZstr("+5", ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool ok     = IntTryFromStrRadix(&value, &digits, 10);
    
        bool result = ok;
    
        Int  out = IntInit(&alloc.base);
        bool ok  = IntTryFromStrRadix(&out, "+5", (u8)10);
    
        bool result = ok && (IntToU64(&out) == 5);
Last updated on