From 1cf6ea15712dd5ef03456f57cc4bcc5049017442 Mon Sep 17 00:00:00 2001 From: H Hautakoski Date: Wed, 21 Jul 2010 20:52:08 +0200 Subject: [PATCH] common/strbuf: a little cleanup in rev --- src/common/strbuf.c | 20 +++++++++----------- test/t_strbuf.c | 3 +++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/common/strbuf.c b/src/common/strbuf.c index c7d0835..8687f64 100644 --- a/src/common/strbuf.c +++ b/src/common/strbuf.c @@ -28,13 +28,6 @@ static void buf_expand(strbuf_t *s, size_t l) { assert(s->buf != NULL); } -static inline void swap(char *a, char *b) { - - char tmp = *a; - *a = *b; - *b = tmp; -} - void strbuf_init(strbuf_t *s) { s->buf = NULL; @@ -82,10 +75,13 @@ void strbuf_ltrim(strbuf_t *s) { void strbuf_rev(strbuf_t *s) { - int i; - - for(i=0; i < s->len / 2; i++) - swap(s->buf + i, s->buf + (s->len-1)-i); + char tmp, *st = s->buf, *en = s->buf + s->len - 1; + + while(st < en) { + tmp = *st; + *st++ = *en; + *en-- = tmp; + } } char* strbuf_release(strbuf_t *s) { @@ -110,3 +106,5 @@ void strbuf_free(strbuf_t *s) { free(s->buf); s->buf = NULL; } + + diff --git a/test/t_strbuf.c b/test/t_strbuf.c index badc40f..a942acf 100644 --- a/test/t_strbuf.c +++ b/test/t_strbuf.c @@ -1,10 +1,13 @@ +#include #include #include #include "../src/common/strbuf.h" void print_strbuf(strbuf_t *s) { + assert(s->len = strlen(s->buf)); + printf("block: %i, len: %i |%s|\n", s->alloc_size, s->len, s->buf); }