src/string.c

changeset 1412
a6f357c18ac6
parent 1334
7763892ed801
--- a/src/string.c	Wed Oct 01 22:45:48 2025 +0200
+++ b/src/string.c	Thu Oct 02 17:58:43 2025 +0200
@@ -25,9 +25,14 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
+#define _GNU_SOURCE
+#include <string.h>
+#if defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >=2
+#define FEATURE_MEMRCHR_AVAILABLE
+#endif
+
 #include "cx/string.h"
 
-#include <string.h>
 #include <stdarg.h>
 #include <assert.h>
 #include <errno.h>
@@ -231,9 +236,14 @@
 }
 
 cxstring cx_strrchr(
-        cxstring string,
-        int chr
+    cxstring string,
+    int chr
 ) {
+#ifdef FEATURE_MEMRCHR_AVAILABLE
+    char *ret = memrchr(string.ptr, 0xFF & chr, string.length);
+    if (ret == NULL) return (cxstring) {NULL, 0};
+    return (cxstring) {ret, string.length - (ret - string.ptr)};
+#else
     chr = 0xFF & chr;
     size_t i = string.length;
     while (i > 0) {
@@ -244,6 +254,7 @@
         }
     }
     return (cxstring) {NULL, 0};
+#endif
 }
 
 cxmutstr cx_strrchr_m(

mercurial