StrFromI64
Description
Convert a signed 64-bit integer to string
Parameters
| Name | Direction | Description |
|---|---|---|
str |
out | String to store the result in |
value |
in | Value to convert |
config |
in | Formatting configuration. May be NULL (uses the default: base 10, no prefix). |
Success
Returns str
Failure
Returns NULL on allocator OOM while appending to str, or when a non-NULL config has an invalid base. The destination string is untouched.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Io.c:2073:
// allocator needed -- `o` may itself be a stack Str.
StrInitStack(temp, 80) {
if (!StrFromI64(&temp, *v, &config)) {
return false;
}- In
Str.c:622:
}
Str *StrFromI64(Str *str, i64 value, const StrIntFormat *config) {
ValidateStr(str);- In
Convert.c:97:
// Test StrFromI64 function
bool test_str_from_i64(void) {
WriteFmt("Testing StrFromI64\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:105:
// Test positive decimal conversion
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromI64(&s, 12345, &config);
bool result = (ZstrCompare(StrBegin(&s), "12345") == 0);
if (!result) {- In
Convert.c:114:
StrClear(&s);
config = (StrIntFormat) {.base = 10, .uppercase = false};
StrFromI64(&s, -12345, &config);
result = result && (ZstrCompare(StrBegin(&s), "-12345") == 0);
if (!result) {- In
Convert.c:123:
StrClear(&s);
config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
StrFromI64(&s, -0xABCD, &config);
// For negative numbers in non-decimal bases, it uses unsigned representation
// -0xABCD = -(43981) = large positive number when treated as unsigned
- In
Convert.c:134:
StrClear(&s);
config = (StrIntFormat) {.base = 10, .uppercase = false};
StrFromI64(&s, 0, &config);
result = result && (ZstrCompare(StrBegin(&s), "0") == 0);
if (!result) {- In
Convert.c:143:
StrClear(&s);
config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
StrFromI64(&s, 42, &config);
result = result && (ZstrCompare(StrBegin(&s), "0b101010") == 0);
if (!result) {- In
Convert.c:460:
// Test decimal round-trip
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromI64(&s, i64_values[i], &config);
i64 recovered_i64 = 0;
bool success = StrToI64(&s, &recovered_i64, NULL);- In
Convert.c:530:
StrClear(&s);
config = (StrIntFormat) {.base = 10, .uppercase = false};
StrFromI64(&s, INT64_MIN + 1, &config);
i64 recovered_min = 0;
success = StrToI64(&s, &recovered_min, NULL);- In
Convert.c:809:
// Test StrFromI64 with NULL pointer - should abort
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromI64(NULL, 42, &config);
return false;- In
Convert.c:1861:
// be the full magnitude.
static bool test_from_i64_int64_min_exact_digits(void) {
WriteFmt("Testing StrFromI64(INT64_MIN) exact digits\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:1866:
Str s = StrInit(&alloc);
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromI64(&s, INT64_MIN, &config);
bool result = (ZstrCompare(StrBegin(&s), "-9223372036854775808") == 0);
Last updated on