[PATCH] strchr, strchrnul: implement strchr() as strchrnul()
James
tirtajames45@gmail.com
Tue Dec 5 15:52:41 GMT 2023
Currently, strchrnul() is implemented as strchr() and falls back to a
strlen() to find the null-terminator if the character is not
found, scanning the string twice. However, strchr() is going to scan the
whole string anyway and discard the pointer to the null-terminator if the
character is not found, returning NULL.
Instead, we can just implement strchr() with discarding strchrnul()'s
pointer to null-terminator returning NULL and by that avoid calling
strlen() in strchrnul() if a character is not found.
I made a typo in the strchr(), it should be:
return s1 && *s1 ? (char *) s1 : NULL;
which should be:
return *s1 ? (char *) s1 : NULL;
since strchrnul will never return NULL.
We can avoid the strchrnul() function call in strchr() by implementing it
in a separate header like str-two-way.h and including them in both files.
The same could be done with the strlen() part in strchr() since the
implementations look to be the same.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/newlib/attachments/20231205/9bd3335c/attachment.htm>
More information about the Newlib
mailing list