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:584:
}
if (!IntTryFromStr(&result.significand, &digits)) {
goto fail;
}- In
Convert.c:359:
Int parsed = IntFromStr("12x3", ALLOCATOR_OF(&alloc));
Int value = IntInit(ALLOCATOR_OF(&alloc));
bool result = !IntTryFromStr(&value, "12x3");
result = result && IntIsZero(&parsed);- In
Convert.c:494:
bool test_int_try_from_decimal_null(void) {
WriteFmt("Testing IntTryFromStr NULL handling\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:499:
Int value = IntInit(ALLOCATOR_OF(&alloc));
IntTryFromStr(&value, (Zstr)NULL);
IntDeinit(&value);
DefaultAllocatorDeinit(&alloc);- In
Convert.c:1006:
Str text = StrInitFromZstr("123", &alloc);
Int out = IntInit(&alloc.base);
bool parsed = IntTryFromStr(&out, &text);
bool result = parsed && (IntToU64(&out) == 123);- In
Convert.c:1037:
Str text = StrInitFromZstr("+5", &alloc);
Int out = IntInit(&alloc.base);
bool parsed = IntTryFromStr(&out, &text);
bool result = parsed && (IntToU64(&out) == 5);- In
Convert.c:1213:
const char *text = "+5";
bool ok = IntTryFromStr(&out, text);
bool result = ok;
result = result && (IntToU64(&out) == 5u);- In
Convert.c:1233:
const char *text = "42";
bool ok = IntTryFromStr(&out, text);
bool result = ok;
result = result && (IntToU64(&out) == 42u);- In
Convert.c:1611:
Int out = IntFrom(987654321u, a); // pre-populated: holds a live buffer
bool ok = IntTryFromStr(&out, "123456789");
ok = ok && IntToU64(&out) == 123456789u;
Last updated on