[PATCH v1] Added string conversion utility functions
spitzak at gmail.com
Tue Dec 2 13:08:45 PST 2014
I think the desired function was to fail if there was text after the
number, fail on blank strings, and fail on overflow. All of these are
somewhat cryptic with strtol.
> However, writing a helper function whose benefit over a direct call to
> strto*() is only the handling of errno, it might not be worth it in my
> opinion. Such a case is probably simpler to just fix in place than make
> it use a helper function. (This might change with libweston, though.)
> If the helper can assume that the whole given string must be a valid
> number, I think that would be much more worth it, because it would then
> hide the end pointer and nul checks. Such a function would be simple to
> use and make the code where it is used much more obvious.
That I believe was the original intent. For that reason it should not
return endptr, as that is useless. If existing code is using endptr that
indicates it cannot use the replacement function.
The function should do some/all of the following, all of which are
clumsy to do with the library functions, and are often attempted by the
calling code in sometimes-cryptic ways:
- Fail if there is non-whitespace after the number
- Fail without crashing if the string pointer is null
- Fail if there is no number (all whitespace or zero-length string)
- Fail if there is overflow
- Don't change errno if it does not fail
- Always set errno non-zero if it does fail, or return a different value
for each type of failure and don't change errno.
- Don't do octal conversion for leading zero, but allow 0x for hex.
Maybe use "0o" or something for octal if people want it.
- Floating point can parse "nan" and "inf" including sign
- Floating point accepts hex integers (not hex fractions, that would be
nice but is much harder. The main point is to make an existing config
file not fail if it uses hex for an integer parameter and that parameter
is changed to floating point).
More information about the wayland-devel