Skip to content
FloatTryFromStr

FloatTryFromStr

Description

Parse a decimal string into a float. Supports an optional sign, decimal point, and scientific exponent ([+-]?digits(.digits)?([eE][+-]?digits)?).

Parameters

Name Direction Description
out in,out Float to overwrite with the parsed value. The existing storage is reused; *out must already be a valid initialised Float bound to an allocator.
text in Null-terminated decimal string.

Success

Returns true. *out is deinitialised and replaced with the normalised parsed value (sign, significand, and exponent set).

Failure

Returns false on a malformed string, an exponent overflow, or an allocation failure. *out retains its pre-call value.

Usage example (Cross-references)

Usage examples (Cross-references)
        StrDeinit(&temp);
        temp = StrInitFromCstr(start, token_len, FloatAllocator(value));
        if (!FloatTryFromStr(&parsed, StrBegin(&temp))) {
            StrDeinit(&temp);
            FloatDeinit(&parsed);
        Float f = FloatInit(&alloc.base);
        // Distinct sentinel so a no-op (failed) read is observable.
        (void)FloatTryFromStr(&f, "99");
    
        StrReadFmt(input, "{}", f);
        Float v = FloatFromStr("99999", &dbg.base); // pre-populated target
    
        bool ok = FloatTryFromStr(&v, "3.14159");
    
        FloatDeinit(&v);
        Allocator       *ab    = ALLOCATOR_OF(&alloc);
        Float            f     = FloatInit(ab);
        (void)FloatTryFromStr(&f, "99"); // sentinel
        StrReadFmt(in, "{}", f);
        Str  t  = FloatToStr(&f);
        DebugAllocator dbg = DebugAllocatorInit();
        Float          f   = FloatInit(&dbg.base);
        (void)FloatTryFromStr(&f, "42");
        Zstr z   = "xyz"; // no float token
        Zstr out = str_read_fmt(z, "{}", (TypeSpecificIO[]) {TO_TYPE_SPECIFIC_IO(Float, &f)}, 1);
        Float parsed = FloatFromStr("12.3.4", ALLOCATOR_OF(&alloc));
        Float value  = FloatInit(ALLOCATOR_OF(&alloc));
        bool  result = !FloatTryFromStr(&value, "12.3.4");
    
        result = result && FloatIsZero(&parsed);
    
    bool test_float_try_from_str_null(void) {
        WriteFmt("Testing FloatTryFromStr NULL handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float value = FloatInit(ALLOCATOR_OF(&alloc));
        FloatTryFromStr(&value, (Zstr)NULL);
        FloatDeinit(&value);
        DefaultAllocatorDeinit(&alloc);
        Str   text  = StrInitFromZstr("not-a-number", &alloc.base);
        Float value = FloatInit(&alloc.base);
        bool  ok    = (FloatTryFromStr(&value, &text) == false);
    
        FloatDeinit(&value);
        Str   text  = StrInitFromZstr("3.14", &alloc.base);
        Float value = FloatInit(&alloc.base);
        bool  ok    = FloatTryFromStr(&value, &text);
    
        Str printed = FloatToStr(&value);
    
        Float value = FloatInit(&alloc.base);
        bool  ok    = (FloatTryFromStr(&value, ".") == false);
    
        FloatDeinit(&value);
    
        Float value = FloatInit(&alloc.base);
        bool  ok    = (FloatTryFromStr(&value, "+.") == false);
    
        FloatDeinit(&value);
    
        Float value = FloatInit(&alloc.base);
        bool  ok    = FloatTryFromStr(&value, "1.");
    
        Str text = FloatToStr(&value);
    // leading '+' must yield a positive value.
    bool test_m3_leading_plus_is_positive(void) {
        WriteFmt("Testing FloatTryFromStr keeps leading-'+' positive\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float f    = FloatInit(&alloc.base);
        bool  ok   = FloatTryFromStr(&f, "+5");
        Str   text = StrInit(&alloc.base);
    // succeed and produce the right value.
    bool test_m3_digit_nine_accepted(void) {
        WriteFmt("Testing FloatTryFromStr accepts the digit '9'\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float f    = FloatInit(&alloc.base);
        bool  ok   = FloatTryFromStr(&f, "19");
        Str   text = StrInit(&alloc.base);
    // exponent branch; the mutant fails to recognise 'E' and rejects the string.
    bool test_m3_uppercase_exponent_marker(void) {
        WriteFmt("Testing FloatTryFromStr handles uppercase 'E' exponent\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float f    = FloatInit(&alloc.base);
        bool  ok   = FloatTryFromStr(&f, "1E2");
        Str   text = StrInit(&alloc.base);
    // exact resulting exponent.
    bool test_m3_min_exponent_boundary(void) {
        WriteFmt("Testing FloatTryFromStr accepts the INT64_MIN exponent boundary\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float     f      = FloatInit(&alloc.base);
        bool      ok     = FloatTryFromStr(&f, "1e-9223372036854775808");
        const i64 minexp = (i64)(-9223372036854775807LL - 1);
Last updated on