StrIntFormat
Description
Configuration for integer formatting
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Io.c:1635:
// 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:1730:
}
Str hex = StrInit(StrAllocator(o));
StrIntFormat config = {.base = 16, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0};
if (!StrFromU64(&hex, (u8)xs[i], &config)) {
StrDeinit(&hex);- In
Io.c:1836:
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};
if (!StrFromU64(&temp, *v, &config)) {
StrDeinit(&temp);- In
Io.c:1936:
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};
if (!StrFromI64(&temp, *v, &config)) {
StrDeinit(&temp);- In
Io.c:2929:
// 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:2941:
} 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
Str.Convert.c:37:
// Test decimal conversion
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromU64(&s, 12345, &config);
bool result = (ZstrCompare(StrBegin(&s), "12345") == 0);- In
Str.Convert.c:46:
// 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
Str.Convert.c:55:
// 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
Str.Convert.c:64:
// 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
Str.Convert.c:73:
// 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
Str.Convert.c:82:
// Test zero
StrClear(&s);
config = (StrIntFormat) {.base = 10, .uppercase = false};
StrFromU64(&s, 0, &config);
result = result && (ZstrCompare(StrBegin(&s), "0") == 0);
// Test positive decimal conversion
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromI64(&s, 12345, &config);
bool result = (ZstrCompare(StrBegin(&s), "12345") == 0); // 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); // 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
// Test zero
StrClear(&s);
config = (StrIntFormat) {.base = 10, .uppercase = false};
StrFromI64(&s, 0, &config);
result = result && (ZstrCompare(StrBegin(&s), "0") == 0); // 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);
// Test decimal round-trip
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromU64(&s, u64_values[i], &config);
u64 recovered_u64 = 0; // Test hex round-trip
StrClear(&s);
config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
StrFromU64(&s, u64_values[i], &config);
recovered_u64 = 0;
// Test decimal round-trip
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromI64(&s, i64_values[i], &config);
i64 recovered_i64 = 0;
// 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;
// Test maximum u64
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromU64(&s, UINT64_MAX, &config);
u64 recovered_max = 0; // 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; Str s = StrInit(&alloc);
StrIntFormat config = {.base = base, .uppercase = false};
StrFromU64(&s, large_value, &config);
u64 recovered = 0;
// Test StrFromU64
StrIntFormat config = {.base = base, .uppercase = false, .use_prefix = false};
StrFromU64(&s, test_value, &config); Str s = StrInit(&alloc);
StrIntFormat config = {.base = base, .uppercase = true, .use_prefix = false};
StrFromU64(&s, test_value, &config); Str s = StrInit(&alloc);
StrIntFormat config = {.base = base, .uppercase = false, .use_prefix = false};
StrFromU64(&s, test_values[i], &config);
Str s = StrInit(&alloc);
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromU64(&s, test_value, &config);
// Test NULL string pointer - should abort
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromU64(NULL, 42, &config);
// Test StrFromI64 with NULL pointer - should abort
StrIntFormat config = {.base = 10, .uppercase = false};
StrFromI64(NULL, 42, &config);- 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);
///
Last updated on