Sun, 26 Jan 2025 14:37:07 +0100
improve cx_strchr()
src/string.c | file | annotate | diff | comparison | revisions |
--- a/src/string.c Sun Jan 26 14:23:13 2025 +0100 +++ b/src/string.c Sun Jan 26 14:37:07 2025 +0100 @@ -220,14 +220,9 @@ cxstring string, int chr ) { - chr = 0xFF & chr; - // TODO: improve by comparing multiple bytes at once - for (size_t i = 0; i < string.length; i++) { - if (string.ptr[i] == chr) { - return cx_strsubs(string, i); - } - } - return (cxstring) {NULL, 0}; + char *ret = memchr(string.ptr, 0xFF & chr, string.length); + if (ret == NULL) return (cxstring) {NULL, 0}; + return (cxstring) {ret, string.length - (ret - string.ptr)}; } cxmutstr cx_strchr_m(