Changeset 214ffc9 in serd


Ignore:
Timestamp:
07/31/17 00:37:57 (3 weeks ago)
Author:
David Robillard <d@…>
Branches:
serd1
Children:
6bb9ccb
Parents:
9709d1b
git-author:
David Robillard <d@…> (07/07/16 20:50:39)
git-committer:
David Robillard <d@…> (07/31/17 00:37:57)
Message:

Make serd_strtod API const-correct

This is an API breakage, but a minor one (particularly since NULL is
allowed) that avoids the flaw in the C API.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • serd/serd.h

    r9709d1b r214ffc9  
    313313   Parse a string to a double. 
    314314 
    315    The API of this function is identical to the standard C strtod function, 
     315   The API of this function is similar to the standard C strtod function, 
    316316   except this function is locale-independent and always matches the lexical 
    317    format used in the Turtle grammar (the decimal point is always "."). 
     317   format used in the Turtle grammar (the decimal point is always ".").  The 
     318   end parameter is an offset from the start of `str` to avoid the 
     319   const-correctness issues of the strtod API. 
    318320*/ 
    319321SERD_API 
    320322double 
    321 serd_strtod(const char* str, char** endptr); 
     323serd_strtod(const char* str, size_t* end); 
    322324 
    323325/** 
  • src/string.c

    ra26622b r214ffc9  
    9696SERD_API 
    9797double 
    98 serd_strtod(const char* str, char** endptr) 
     98serd_strtod(const char* str, size_t* end) 
    9999{ 
    100100    double result = 0.0; 
     
    132132    } 
    133133 
    134     if (endptr) { 
    135         *endptr = (char*)s; 
     134    if (end) { 
     135        *end = s - str; 
    136136    } 
    137137 
  • tests/serd_test.c

    raf0beac r214ffc9  
    4848    snprintf(buf, sizeof(buf), "%f", dbl); 
    4949 
    50     char* endptr = NULL; 
    51     const double out = serd_strtod(buf, &endptr); 
     50    size_t       end = 0; 
     51    const double out = serd_strtod(buf, &end); 
    5252 
    5353    const double diff = fabs(out - dbl); 
     
    5555        return !failure("Parsed %lf != %lf (delta %lf)\n", dbl, out, diff); 
    5656    } 
     57 
     58    if (end != strlen(buf)) { 
     59        return !failure("Parsed %lf length %zu != %zu\n", end, strlen(buf)); 
     60    } 
     61 
    5762    return true; 
    5863} 
Note: See TracChangeset for help on using the changeset viewer.