improve cx_strchr()

Sun, 26 Jan 2025 14:37:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Jan 2025 14:37:07 +0100
changeset 1161
747c4baed44f
parent 1160
4f02c1101f2e
child 1162
e3bb67b72d33

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(

mercurial