StrIntFormat
Description
Configuration for integer formatting
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Convert.h:23:
bool pad_zeros; ///< Pad with leading zeros
u8 min_width; ///< Minimum field width
} StrIntFormat;
/// Configuration for float formatting
- In
Convert.h:42:
/// Default configurations
extern const StrIntFormat STR_INT_DEFAULT;
extern const StrFloatFormat STR_FLOAT_DEFAULT;
extern const StrParseConfig STR_PARSE_DEFAULT;- In
Convert.h:61:
/// TAGS: Str, Convert, U64
///
Str *StrFromU64(Str *str, u64 value, const StrIntFormat *config);
///
- In
Convert.h:78:
/// TAGS: Str, Convert, I64
///
Str *StrFromI64(Str *str, i64 value, const StrIntFormat *config);
///
- In
Io.c:1772:
// single-space separator (no separator before the first
// byte). Two hex digits per byte, zero-padded.
StrIntFormat config = {.base = 16, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0};
StrForeachIdx(s, c, i) {
if (i > 0) {- In
Io.c:1867:
}
}
StrIntFormat config = {.base = 16, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0};
// Hex of one byte is <= 2 glyphs (+ a leading '0' pad), so
// an 8-byte stack scratch suffices; no allocator needed.
- In
Io.c:1968:
bool zero_pad = (fmt_info->flags & FMT_FLAG_ZERO_PAD) != 0;
bool use_prefix = (base != 10) && !zero_pad;
StrIntFormat config = {.base = base, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0, .use_prefix = use_prefix};
// Render into a stack scratch, then merge into `o`: keeps the base-
- In
Io.c:2067:
bool zero_pad = (fmt_info->flags & FMT_FLAG_ZERO_PAD) != 0;
bool use_prefix = (base != 10) && !zero_pad;
StrIntFormat config = {.base = base, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0, .use_prefix = use_prefix};
// Render into a stack scratch, then merge into `o` (see _write_u64):
- In
Io.c:3063:
// lose their upper bits in hex/octal display modes.
u64 value = BitVecToInteger(bv);
StrIntFormat config = {.base = 16, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0, .use_prefix = true};
if (!StrFromU64(o, value, &config)) {
return false;- In
Io.c:3075:
} else {
u64 value = BitVecToInteger(bv);
StrIntFormat config = {.base = 8, .uppercase = false, .use_prefix = true};
if (!StrFromU64(o, value, &config)) {
return false;- In
Str.c:557:
}
Str *StrFromU64(Str *str, u64 value, const StrIntFormat *config) {
ValidateStr(str);- In
Str.c:622:
}
Str *StrFromI64(Str *str, i64 value, const StrIntFormat *config) {
ValidateStr(str);- In
Str.c:1159:
}
const StrIntFormat STR_INT_DEFAULT =
{.base = 10, .uppercase = false, .use_prefix = false, .pad_zeros = false, .min_width = 0};- In
Convert.c:38:
// Test decimal conversion
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromU64(&s, 12345, &config);
bool result = (ZstrCompare(StrBegin(&s), "12345") == 0);- In
Convert.c:47:
// Test hexadecimal conversion (lowercase)
StrClear(&s);
config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
StrFromU64(&s, 0xABCD, &config);
result = result && (ZstrCompare(StrBegin(&s), "0xabcd") == 0);- In
Convert.c:56:
// Test hexadecimal conversion (uppercase)
StrClear(&s);
config = (StrIntFormat) {.base = 16, .uppercase = true, .use_prefix = true};
StrFromU64(&s, 0xABCD, &config);
result = result && (ZstrCompare(StrBegin(&s), "0xABCD") == 0);- In
Convert.c:65:
// Test binary conversion
StrClear(&s);
config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
StrFromU64(&s, 42, &config);
result = result && (ZstrCompare(StrBegin(&s), "0b101010") == 0);- In
Convert.c:74:
// Test octal conversion
StrClear(&s);
config = (StrIntFormat) {.base = 8, .uppercase = false, .use_prefix = true};
StrFromU64(&s, 42, &config);
result = result && (ZstrCompare(StrBegin(&s), "0o52") == 0);- In
Convert.c:83:
// Test zero
StrClear(&s);
config = (StrIntFormat) {.base = 10, .uppercase = false};
StrFromU64(&s, 0, &config);
result = result && (ZstrCompare(StrBegin(&s), "0") == 0);- In
Convert.c:104:
// Test positive decimal conversion
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromI64(&s, 12345, &config);
bool result = (ZstrCompare(StrBegin(&s), "12345") == 0);- In
Convert.c:113:
// Test negative decimal conversion (only decimal supports negative sign)
StrClear(&s);
config = (StrIntFormat) {.base = 10, .uppercase = false};
StrFromI64(&s, -12345, &config);
result = result && (ZstrCompare(StrBegin(&s), "-12345") == 0);- In
Convert.c:122:
// Test hexadecimal conversion of negative number (negative non-decimal treated as unsigned)
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
- In
Convert.c:133:
// Test zero
StrClear(&s);
config = (StrIntFormat) {.base = 10, .uppercase = false};
StrFromI64(&s, 0, &config);
result = result && (ZstrCompare(StrBegin(&s), "0") == 0);- In
Convert.c:142:
// Test binary conversion
StrClear(&s);
config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
StrFromI64(&s, 42, &config);
result = result && (ZstrCompare(StrBegin(&s), "0b101010") == 0);- In
Convert.c:438:
// Test decimal round-trip
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromU64(&s, u64_values[i], &config);
u64 recovered_u64 = 0;- In
Convert.c:446:
// Test hex round-trip
StrClear(&s);
config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
StrFromU64(&s, u64_values[i], &config);
recovered_u64 = 0;- In
Convert.c:459:
// Test decimal round-trip
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromI64(&s, i64_values[i], &config);
i64 recovered_i64 = 0;- In
Convert.c:505:
// Test with base value itself
StrIntFormat config = {.base = base, .uppercase = false, .use_prefix = (base == 2 || base == 8 || base == 16)};
StrFromU64(&s, base, &config);
u64 recovered = 0;- In
Convert.c:521:
// Test maximum u64
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromU64(&s, UINT64_MAX, &config);
u64 recovered_max = 0;- In
Convert.c:529:
// Test minimum i64 (avoid INT64_MIN due to negation UB)
StrClear(&s);
config = (StrIntFormat) {.base = 10, .uppercase = false};
StrFromI64(&s, INT64_MIN + 1, &config);
i64 recovered_min = 0;- In
Convert.c:636:
Str s = StrInit(&alloc);
StrIntFormat config = {.base = base, .uppercase = false};
StrFromU64(&s, large_value, &config);
u64 recovered = 0;- In
Convert.c:666:
// Test StrFromU64
StrIntFormat config = {.base = base, .uppercase = false, .use_prefix = false};
StrFromU64(&s, test_value, &config);- In
Convert.c:683:
Str s = StrInit(&alloc);
StrIntFormat config = {.base = base, .uppercase = true, .use_prefix = false};
StrFromU64(&s, test_value, &config);- In
Convert.c:702:
Str s = StrInit(&alloc);
StrIntFormat config = {.base = base, .uppercase = false, .use_prefix = false};
StrFromU64(&s, test_values[i], &config);- In
Convert.c:736:
Str s = StrInit(&alloc);
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromU64(&s, test_value, &config);- In
Convert.c:798:
// Test NULL string pointer - should abort
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromU64(NULL, 42, &config);- In
Convert.c:808:
// Test StrFromI64 with NULL pointer - should abort
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromI64(NULL, 42, &config);- In
Convert.c:1865:
Str s = StrInit(&alloc);
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromI64(&s, INT64_MIN, &config);
bool result = (ZstrCompare(StrBegin(&s), "-9223372036854775808") == 0);- In
Convert.c:1929:
Str s = StrInit(&alloc);
StrIntFormat config = {.base = 37, .uppercase = false};
Str *ret = StrFromU64(&s, 5, &config);- In
Convert.c:1954:
Str s = StrInit(&alloc);
StrIntFormat config = {.base = 10, .uppercase = false};
Str *ret = StrFromU64(&s, 12345, &config);- In
Convert.c:1986:
Str s = StrInit(&bp);
StrIntFormat config = {.base = 10, .uppercase = false};
// 10-digit value: any growth request exceeds the 8-byte slot.
Str *ret = StrFromU64(&s, 9999999999ULL, &config);
Last updated on