IntTryFromStr
Description
Parse a decimal string into an integer. An optional leading + is accepted; underscore separators between digits are silently skipped.
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. |
decimal |
in | Null-terminated decimal string, optionally prefixed with +. |
Success
Returns true. *out is deinitialised and replaced with the normalised parsed value.
Failure
Returns false on a non-decimal character, an empty digit run, or an allocation failure during accumulation. *out retains its pre-call value.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Float.c:556:
}
if (!IntTryFromStr(&result.significand, &digits)) {
goto fail;
} Int parsed = IntFromStr("12x3", ALLOCATOR_OF(&alloc));
Int value = IntInit(ALLOCATOR_OF(&alloc));
bool result = !IntTryFromStr(&value, "12x3");
result = result && IntIsZero(&parsed);
bool test_int_try_from_decimal_null(void) {
WriteFmt("Testing IntTryFromStr NULL handling\n");
DefaultAllocator alloc = DefaultAllocatorInit();
Int value = IntInit(ALLOCATOR_OF(&alloc));
IntTryFromStr(&value, (Zstr)NULL);
IntDeinit(&value);
DefaultAllocatorDeinit(&alloc); Str text = StrInitFromZstr("123", &alloc);
Int out = IntInit(&alloc.base);
bool parsed = IntTryFromStr(&out, &text);
bool result = parsed && (IntToU64(&out) == 123); Str text = StrInitFromZstr("+5", &alloc);
Int out = IntInit(&alloc.base);
bool parsed = IntTryFromStr(&out, &text);
bool result = parsed && (IntToU64(&out) == 5); const char *text = "+5";
bool ok = IntTryFromStr(&out, text);
bool result = ok;
result = result && (IntToU64(&out) == 5u); const char *text = "42";
bool ok = IntTryFromStr(&out, text);
bool result = ok;
result = result && (IntToU64(&out) == 42u);- In
Int.Leak.c:1104:
Int out = IntFrom(987654321u, a); // pre-populated: holds a live buffer
bool ok = IntTryFromStr(&out, "123456789");
ok = ok && IntToU64(&out) == 123456789u;
Last updated on