Skip to content
StrParseConfig

StrParseConfig

Description

Configuration for parsing

Usage example (Cross-references)

Usage examples (Cross-references)
            bool trim_space; ///< Trim leading/trailing whitespace
            u8   base;       ///< Base for integers (0 = auto-detect)
        } StrParseConfig;
    
        /// Default configurations
        extern const StrIntFormat   STR_INT_DEFAULT;
        extern const StrFloatFormat STR_FLOAT_DEFAULT;
        extern const StrParseConfig STR_PARSE_DEFAULT;
    
        ///
        /// TAGS: Str, Convert, U64
        ///
        bool StrToU64(const Str *str, u64 *value, const StrParseConfig *config);
    
        ///
        /// TAGS: Str, Convert, I64
        ///
        bool StrToI64(const Str *str, i64 *value, const StrParseConfig *config);
    
        ///
        /// TAGS: Str, Convert, F64
        ///
        bool StrToF64(const Str *str, f64 *value, const StrParseConfig *config);
    
            );
            u64            value;
            StrParseConfig config = {.base = 16};
            if (!StrToU64(&hex_str, &value, &config)) {
                LOG_ERROR("Failed to parse hex value");
            );
            u64            value;
            StrParseConfig config = {.base = 8};
            if (!StrToU64(&oct_str, &value, &config)) {
                LOG_ERROR("Failed to parse octal value");
    }
    
    bool StrToU64(const Str *str, u64 *value, const StrParseConfig *config) {
        ValidateStr(str);
    }
    
    bool StrToI64(const Str *str, i64 *value, const StrParseConfig *config) {
        ValidateStr(str);
    }
    
    bool StrToF64(const Str *str, f64 *value, const StrParseConfig *config) {
        ValidateStr(str);
        {.precision = 6, .force_sci = false, .uppercase = false, .trim_zeros = false, .always_sign = false};
    
    const StrParseConfig STR_PARSE_DEFAULT = {.strict = false, .trim_space = true, .base = 0};
        StrDeinit(&s);
        s                     = StrInitFromZstr("ABCD", &alloc); // No 0x prefix when base is explicitly 16
        StrParseConfig config = {.base = 16};
        success               = StrToU64(&s, &value, &config);
        result                = result && (success && value == 0xABCD);
    
            // For bases with prefixes, use auto-detect. For others, use explicit base parsing
            StrParseConfig parse_config = {.base = base};
            bool           success      = StrToU64(&s, &recovered, config.use_prefix ? NULL : &parse_config);
            result                      = result && success && (recovered == base);
            Str            test_str = StrInitFromZstr(prefix_tests[i].input, &alloc);
            u64            value    = 0;
            StrParseConfig config   = {.base = prefix_tests[i].base};
            bool           success  = StrToU64(&test_str, &value, prefix_tests[i].base == 0 ? NULL : &config);
            result                  = result && success && (value == prefix_tests[i].expected);
            StrFromU64(&s, large_value, &config);
            u64            recovered = 0;
            StrParseConfig pconfig   = {.base = base};
            bool           success   = StrToU64(&s, &recovered, &pconfig); // Can now use explicit base with prefixes
            result                   = result && success && (recovered == large_value);
            // Test StrToU64 round-trip
            u64            recovered    = 0;
            StrParseConfig parse_config = {.base = base};
            bool           success      = StrToU64(&s, &recovered, &parse_config);
    
            u64            recovered    = 0;
            StrParseConfig parse_config = {.base = base};
            bool           success      = StrToU64(&s, &recovered, &parse_config);
    
                u64            recovered    = 0;
                StrParseConfig parse_config = {.base = base};
                bool           success      = StrToU64(&s, &recovered, &parse_config);
        Str              s      = StrInitFromZstr("5 ", &alloc);
        f64              value  = 0.0;
        StrParseConfig   config = {.strict = true, .trim_space = true, .base = 0};
        bool             ok     = StrToF64(&s, &value, &config);
        bool             pass   = ok && F64Abs(value - 5.0) < 1e-9;
        Str              s      = StrInitFromZstr("5 ", &alloc);
        f64              value  = 0.0;
        StrParseConfig   config = {.strict = true, .trim_space = true, .base = 0};
        bool             ok     = StrToF64(&s, &value, &config);
        bool             pass   = ok && F64Abs(value - 5.0) < 1e-9;
        Str              s      = StrInitFromZstr("5", &alloc);
        f64              value  = 0.0;
        StrParseConfig   config = {.strict = true, .trim_space = true, .base = 0};
        bool             ok     = StrToF64(&s, &value, &config);
        bool             pass   = ok && F64Abs(value - 5.0) < 1e-9;
        Str              s      = StrInitFromZstr("42", &alloc);
        f64              value  = 0.0;
        StrParseConfig   config = {.strict = true, .trim_space = true, .base = 0};
        bool             ok     = StrToF64(&s, &value, &config);
        bool             pass   = ok && F64Abs(value - 42.0) < 1e-9;
        Str            s      = StrInitFromZstr("5", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.base = 99};
        bool           ok     = StrToU64(&s, &value, &config);
        bool           result = (!ok);
        Str            s      = StrInitFromZstr("ff", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.base = 16};
        bool           ok     = StrToU64(&s, &value, &config);
        bool           result = (ok && value == 255);
        Str            s      = StrInitFromZstr("42 ", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.strict = true};
        bool           ok     = StrToU64(&s, &value, &config);
        bool           result = (ok && value == 42);
        Str            s      = StrInitFromZstr("42  ", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.strict = true};
        bool           ok     = StrToU64(&s, &value, &config);
        bool           result = (ok && value == 42);
        Str            s      = StrInitFromZstr("42x", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.strict = true};
        bool           ok     = StrToU64(&s, &value, &config);
        bool           result = (!ok);
        Str            s      = StrInitFromZstr("42", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.strict = true};
        bool           ok     = StrToU64(&s, &value, &config);
        bool           result = (ok && value == 42);
        Str            bad    = {0}; // zeroed magic: ValidateStr must LOG_FATAL
        u64            value  = 0;
        StrParseConfig config = {.base = 10};
        StrToU64(&bad, &value, &config);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0x1F", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
        bool             ok    = StrToU64(&s, &out, &cfg);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0x5", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
        bool             ok    = StrToU64(&s, &out, &cfg);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0x5", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
        bool             ok    = StrToU64(&s, &out, &cfg);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0b1", &alloc);
        StrParseConfig   cfg   = {.base = 2};
        u64              out   = 0;
        bool             ok    = StrToU64(&s, &out, &cfg);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0b1", &alloc);
        StrParseConfig   cfg   = {.base = 2};
        u64              out   = 0;
        bool             ok    = StrToU64(&s, &out, &cfg);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0o7", &alloc);
        StrParseConfig   cfg   = {.base = 8};
        u64              out   = 0;
        bool             ok    = StrToU64(&s, &out, &cfg);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0O7", &alloc);
        StrParseConfig   cfg   = {.base = 8};
        u64              out   = 0;
        bool             ok    = StrToU64(&s, &out, &cfg);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0o7", &alloc);
        StrParseConfig   cfg   = {.base = 8};
        u64              out   = 0;
        bool             ok    = StrToU64(&s, &out, &cfg);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0x1F", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
        bool             ok    = StrToU64(&s, &out, &cfg);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0X1F", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
        bool             ok    = StrToU64(&s, &out, &cfg);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0x1F", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
        bool             ok    = StrToU64(&s, &out, &cfg);
        Str            s       = StrInitFromZstr("2", &alloc);
        u64            value   = 999; // sentinel
        StrParseConfig config  = {.base = 2};
        bool           success = StrToU64(&s, &value, &config);
        // Real code: '2' is invalid in base 2 -> parse fails. The mutant accepts
        DefaultAllocator alloc  = DefaultAllocatorInit();
        Str              s      = StrInitFromZstr("0B101", &alloc);
        StrParseConfig   config = {.base = 2};
        u64              value  = 0;
        bool             ok     = StrToU64(&s, &value, &config);
Last updated on