Skip to content

StrIntFormat

StrIntFormat

Description

Configuration for integer formatting

Usage example (Cross-references)

Usage examples (Cross-references)
            if (fmt_info->flags & FMT_FLAG_HEX) {
                // Format each character as hex
                StrIntFormat config = {.base = 16, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0};
                StrForeachIdx(s, c, i) {
                    if (i > 0) {
                    // Create hex string for each character
                    Str          hex    = StrInit();
                    StrIntFormat config = {.base = 16, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0};
                    StrFromU64(&hex, (u8)xs[i], &config);
                    // Ensure 2 digits with leading zero
        // Use StrFromU64 directly with the appropriate base
        bool         use_prefix = (base != 10); // Add prefix for non-decimal bases
        StrIntFormat config = {.base = base, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0, .use_prefix = use_prefix};
        StrFromU64(&temp, *v, &config);
        // Use StrFromI64 directly with the appropriate base
        bool         use_prefix = (base != 10); // Add prefix for non-decimal bases
        StrIntFormat config = {.base = base, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0, .use_prefix = use_prefix};
        StrFromI64(&temp, *v, &config);
                // Convert to integer (up to 64 bits) and format as hex
                u64          value  = BitVecToInteger(bv);
                StrIntFormat config = {.base = 16, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0, .use_prefix = true};
                StrFromU64(o, value, &config);
            }
            } else {
                u64          value  = BitVecToInteger(bv);
                StrIntFormat config = {.base = 8, .uppercase = false, .use_prefix = true};
                StrFromU64(o, value, &config);
            }
    // ======================================
    
    Str *StrFromU64(Str *str, u64 value, const StrIntFormat *config) {
        ValidateStr(str);
    }
    
    Str *StrFromI64(Str *str, i64 value, const StrIntFormat *config) {
        ValidateStr(str);
    
    // Default configurations
    const StrIntFormat STR_INT_DEFAULT =
        {.base = 10, .uppercase = false, .use_prefix = false, .pad_zeros = false, .min_width = 0};
    
        // Test decimal conversion
        StrIntFormat config = {.base = 10, .uppercase = false};
        StrFromU64(&s, 12345, &config);
        bool result = (ZstrCompare(s.data, "12345") == 0);
        // Test hexadecimal conversion (lowercase)
        StrClear(&s);
        config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
        StrFromU64(&s, 0xABCD, &config);
        result = result && (ZstrCompare(s.data, "0xabcd") == 0);
        // Test hexadecimal conversion (uppercase)
        StrClear(&s);
        config = (StrIntFormat) {.base = 16, .uppercase = true, .use_prefix = true};
        StrFromU64(&s, 0xABCD, &config);
        result = result && (ZstrCompare(s.data, "0xABCD") == 0);
        // Test binary conversion
        StrClear(&s);
        config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
        StrFromU64(&s, 42, &config);
        result = result && (ZstrCompare(s.data, "0b101010") == 0);
        // Test octal conversion
        StrClear(&s);
        config = (StrIntFormat) {.base = 8, .uppercase = false, .use_prefix = true};
        StrFromU64(&s, 42, &config);
        result = result && (ZstrCompare(s.data, "0o52") == 0);
        // Test zero
        StrClear(&s);
        config = (StrIntFormat) {.base = 10, .uppercase = false};
        StrFromU64(&s, 0, &config);
        result = result && (ZstrCompare(s.data, "0") == 0);
    
        // Test positive decimal conversion
        StrIntFormat config = {.base = 10, .uppercase = false};
        StrFromI64(&s, 12345, &config);
        bool result = (ZstrCompare(s.data, "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(s.data, "-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(s.data, "0") == 0);
        // Test binary conversion
        StrClear(&s);
        config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
        StrFromI64(&s, 42, &config);
        result = result && (ZstrCompare(s.data, "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();
    
            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();
    
            StrIntFormat config = {.base = base, .uppercase = true, .use_prefix = false};
            StrFromU64(&s, test_value, &config);
                Str s = StrInit();
    
                StrIntFormat config = {.base = base, .uppercase = false, .use_prefix = false};
                StrFromU64(&s, test_values[i], &config);
    
            Str          s      = StrInit();
            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);
            bool pad_zeros;  ///< Pad with leading zeros
            u8   min_width;  ///< Minimum field width
        } StrIntFormat;
    
        /// Configuration for float formatting
    
        /// Default configurations
        extern const StrIntFormat   STR_INT_DEFAULT;
        extern const StrFloatFormat STR_FLOAT_DEFAULT;
        extern const StrParseConfig STR_PARSE_DEFAULT;
        /// FAILURE : Returns NULL if config is invalid
        ///
        Str *StrFromU64(Str *str, u64 value, const StrIntFormat *config);
    
        ///
        /// FAILURE : Returns NULL if config is invalid
        ///
        Str *StrFromI64(Str *str, i64 value, const StrIntFormat *config);
    
        ///
Last updated on