Better/more strtol variants

Olaf van der Spek ml@vdspek.org
Wed Mar 26 15:08:00 GMT 2014


Hi,

This is a request for better and more strtol variants. Consider a
simple invocation of strtol:

const char* in = "s5";
char* endp;
errno = 0;
int out = strtol(in, &endp, 10);
if (errno || endp == in)
    // oops
// use out

1. Only null-terminated input is supported, support for a ptr, size_t
pair would be nice.
2. Long and long long variants exist, but an int variant is missing.
3. It's verbose and easy to get wrong.
4. There's no option to not skip whitespace, the separation of
concerns rule is (basically) being violated.

I'd like to see functions like:
int f0(int* out, const char* in, char** endp, int base);
int f1(int* out, size_t sz, const char* in, char** endp, int base);
with variants for int, long, long long etc. Names TBD. Errors (EINVAL,
ERANGE) are returned, the value gets stored in out if there's no
error.
If !endp, the entire input is required to be consumed to catch inputs
like "0seven".
Whitespace would not be skipped.

The much simpler invocation would now be:
int out;
if (f0(out, "s5", NULL, 10))
    // oops
// use out


What do you think?


-- 
Olaf



More information about the Libc-alpha mailing list