This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Better/more strtol variants


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]