From ae3ff5faaecde9c14e52e7c65295c524d19b2d15 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 27 Nov 2010 18:29:39 +0100 Subject: [PATCH] strbuf.c: use memrchr in strbuf_rchop --- Makefile | 2 +- Makefile.include | 3 +++ Makefile.local.mk-dist | 3 +++ src/strbuf.c | 14 +++++--------- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index e49924d..f304bab 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ ifdef DEBUG endif obj := $(obj-ini) $(obj-log) $(obj-notify) $(obj-path) \ - $(obj-strbuf) $($obj-xalloc) + $(obj-strbuf) $($obj-xalloc) $(obj-compat) ifeq ($(database), mongo) LDFLAGS += -lmongoc -lbson diff --git a/Makefile.include b/Makefile.include index 2a805c7..f5b0435 100644 --- a/Makefile.include +++ b/Makefile.include @@ -15,6 +15,9 @@ ifeq ($(VERBOSE), 2) endif # modules definitions +ifdef NO_MEMRCHR + obj-compat = src/compat/memrchr.o +endif obj-xalloc = src/xalloc.o src/die.o obj-strbuf = src/strbuf.o $(obj-xalloc) obj-path = src/path.o $(obj-strbuf) diff --git a/Makefile.local.mk-dist b/Makefile.local.mk-dist index aa13840..cc0e893 100644 --- a/Makefile.local.mk-dist +++ b/Makefile.local.mk-dist @@ -10,3 +10,6 @@ # how to install the libray and header files by yourself. # database = mysql # database = mongo + +# Uncomment this if you don't have memrchr on your system. (GNU extension as of >=glibc 2.1.91) +# NO_MEMRCHR = 1 diff --git a/src/strbuf.c b/src/strbuf.c index c098563..9e291f9 100644 --- a/src/strbuf.c +++ b/src/strbuf.c @@ -9,7 +9,7 @@ */ #include -#include +#include "compat/string.h" #include "xalloc.h" #include "strbuf.h" @@ -111,15 +111,11 @@ void strbuf_append_repeat(strbuf_t *s, char ch, size_t len) { void strbuf_rchop(strbuf_t *s, char ch) { - int i; - - for(i=s->len-1; i >= 0; i--) { + char *n = memrchr(s->buf, ch, s->len); - if (s->buf[i] == ch) { - s->buf[i] = '\0'; - s->len = i; - break; - } + if (n) { + *n = '\0'; + s->len = n - s->buf; } }