Skip to content

JReadFloat

Description

Read a floating-point number from input string.

Parameters

Name Direction Description
si in Current reading position in input string
val out Pointer to f64 to store parsed value

Success

Returns StrIter advanced past parsed float

Failure

Returns original StrIter on error

Usage example (Cross-references)

Usage examples (Cross-references)
        do {                                                                                                               \
            f64 UNPL(my_flt) = 0;                                                                                          \
            si               = JReadFloat((si), &UNPL(my_flt));                                                            \
            (f)              = UNPL(my_flt);                                                                               \
        } while (0)
            if (!StrCmp(&key, (k))) {                                                                                      \
                f64 UNPL(my_flt) = 0;                                                                                      \
                si               = JReadFloat((si), &UNPL(my_flt));                                                        \
                (f)              = UNPL(my_flt);                                                                           \
            }                                                                                                              \
    }
    
    StrIter JReadFloat(StrIter si, f64 *val) {
        if (!StrIterRemainingLength(&si)) {
            return si;
            StrIter si  = StrIterFromStr(j);
            f64     v   = 0.0;
            StrIter out = JReadFloat(si, &v);
            if (!(StrIterIndex(&out) != StrIterIndex(&si) && v == -2.5)) {
                WriteFmtLn("[DEBUG] JReadFloat '-2.5' -> {}", v);
            StrIter out = JReadFloat(si, &v);
            if (!(StrIterIndex(&out) != StrIterIndex(&si) && v == -2.5)) {
                WriteFmtLn("[DEBUG] JReadFloat '-2.5' -> {}", v);
                success = false;
            }
    // 42.0 for any integer. Parse "7" and pin *val == 7.0 (and advance).
    bool test_js_float_reads_integer_valued_number(void) {
        WriteFmtLn("Testing JReadFloat promotes an integer-valued number exactly");
    
        DefaultAllocator alloc   = DefaultAllocatorInit();
        StrIter si  = StrIterFromStr(j);
        f64     val = 0.0;
        StrIter out = JReadFloat(si, &val);
    
        // Must advance (token consumed) and yield exactly 7.0 -- not 42.0.
        // Must advance (token consumed) and yield exactly 7.0 -- not 42.0.
        if (StrIterIndex(&out) == StrIterIndex(&si)) {
            WriteFmtLn("[DEBUG] JReadFloat did not advance on integer '7'");
            success = false;
        }
        }
        if (val != 7.0) {
            WriteFmtLn("[DEBUG] JReadFloat integer-promotion wrong: expected 7.0, got {}", val);
            success = false;
        }
    // cannot coincide with the literal. 13 -> 13.0, never 42.0.
    bool test_js_float_reads_zero_valued_integer(void) {
        WriteFmtLn("Testing JReadFloat promotes a second integer value exactly");
    
        DefaultAllocator alloc   = DefaultAllocatorInit();
        StrIter si  = StrIterFromStr(j);
        f64     val = 0.0;
        StrIter out = JReadFloat(si, &val);
    
        if (StrIterIndex(&out) == StrIterIndex(&si)) {
    
        if (StrIterIndex(&out) == StrIterIndex(&si)) {
            WriteFmtLn("[DEBUG] JReadFloat did not advance on integer '13'");
            success = false;
        }
        }
        if (val != 13.0) {
            WriteFmtLn("[DEBUG] JReadFloat integer-promotion wrong: expected 13.0, got {}", val);
            success = false;
        }
Last updated on