-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Lib: Util: String conversion API #43289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lib: Util: String conversion API #43289
Conversation
45961c6 to
5113d0d
Compare
|
After talking to @PavelVPV we have agreed that this API should not be public. Further I have decided that the changes to subsys\bluetooth\shell.c should be done later as a separate task since there are additional changes that needs to be done. |
b7edf0a to
983b8d7
Compare
alwa-nordic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like these API additions to go into the shell subsystem. They are not mesh-specific and I think others could benefit. And it will hopefully encourage a uniform interface.
983b8d7 to
f8ff9dc
Compare
alxelax
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, agree with @PavelVPV comments
Adds string conversion API. Implements basic string to parameter convertion functions. Signed-off-by: Anders Storrø <[email protected]>
2f90f8a to
5b37b12
Compare
|
Any CO that wants to add something? If there is no more dire requests for changes I would appreciate approval of this since it is a blocker for other tasks. |
|
Is there a particular reason why you are not augmenting Zephyr's minimal C library instead? i.e. adding |
|
@de-nordic and @alwa-nordic please take another look |
stephanosio
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not convinced why we need non-standard functions like this when there already exist the C standard functions to do this:
string_conv_str2long - strtol
string_conv_str2ulong - strtoul
string_conv_str2dbl - strtod
strtol and strtoul are already part of the minimal libc.
strtod is currently not part of the minimal libc (it is available if you use newlib though).
| int frac_len = strlen(trimmed_buf + comma_idx + 1); | ||
|
|
||
| /* Covers corner case "." input */ | ||
| if (strlen(trimmed_buf) < 2 && trimmed_buf[comma_idx] != 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why srlen(trimmed_buf) again? isnt line 122 already assigning the len with trimmed_buf len?
Maybe that should be done instead. |
Well for my own part I do not have a strong opinion on this, other than that what I am trying to accomplish is not a unique usecase. This is the original description of the task that caused the PR: So the main objective I want to acheive is to get error feedback on conversion of corrupt numeric strings. The implementation of strtol has, to the best of my knowledge, only error feedback on conversion overflow. Initially this was placed as utility under the shell module for Bluetooth Mesh. Then it was pointed out that this was not mesh spesific functionality, and that it should be general shell utility. Then it was pointed out that this was not a shell spesific usecase either, and here we are. I guess moving it back to where I started is one viable option. |
See https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html Although the minimal libc implementation may not be fully compliant to the specification linked above at this time, it is important to note that the |
Waiting until dicussion on why not instead use libc functions is resolved.
Please, no. If these functions really are safer to use, they should be available and preferred everywhere. |
We could add them as wrappers around the existing functions, instead of re-inventing the logic. Basically just use the existing functions and add additional error handling. |
|
I believe there is merit in a simpler API. /* The value of `out` is undefined in case of error. */
int parse_dec_to_long(long * out, char * str) {
if (isspace(str[0])) {
return -EINVAL;
}
char * endptr;
errno = 0;
*out = strtol(str, &endptr, 10);
if (endptr[0] != '\0') {
return -EINVAL;
}
return -errno;
}Ignoring white space is not always the right thing to do. E.g. for protocols. But even when the user of We should also consider the parsing of non-null-terminated strings. This cannot be easily done with |
Adds string conversion API.
Implements basic string to parameter convertion functions.
Signed-off-by: Anders Storrø [email protected]