From e216094e2192c05ddbd0988458eb8c0012e7baf8 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Tue, 2 Sep 2014 01:10:54 +0200 Subject: lol --- src/buffer.c | 313 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 313 insertions(+) create mode 100644 src/buffer.c (limited to 'src/buffer.c') diff --git a/src/buffer.c b/src/buffer.c new file mode 100644 index 0000000..b81e7fa --- /dev/null +++ b/src/buffer.c @@ -0,0 +1,313 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "buffer.h" + +/* Used as default value for gh_buf->ptr so that people can always + * assume ptr is non-NULL and zero terminated even for new gh_bufs. + */ +unsigned char gh_buf__initbuf[1]; +unsigned char gh_buf__oom[1]; + +#define ENSURE_SIZE(b, d) \ + if ((d) > buf->asize && gh_buf_grow(b, (d)) < 0)\ + return -1; + +void gh_buf_init(gh_buf *buf, int initial_size) +{ + buf->asize = 0; + buf->size = 0; + buf->ptr = gh_buf__initbuf; + + if (initial_size) + gh_buf_grow(buf, initial_size); +} + +int gh_buf_try_grow(gh_buf *buf, int target_size, bool mark_oom) +{ + char *new_ptr; + size_t new_size; + + if (buf->ptr == gh_buf__oom || buf->asize < 0) + return -1; + + if (target_size <= buf->asize) + return 0; + + if (buf->asize == 0) { + new_size = target_size; + new_ptr = NULL; + } else { + new_size = buf->asize; + new_ptr = buf->ptr; + } + + /* grow the buffer size by 1.5, until it's big enough + * to fit our target size */ + while (new_size < target_size) + new_size = (new_size << 1) - (new_size >> 1); + + /* round allocation up to multiple of 8 */ + new_size = (new_size + 7) & ~7; + + new_ptr = realloc(new_ptr, new_size); + + if (!new_ptr) { + if (mark_oom) + buf->ptr = gh_buf__oom; + return -1; + } + + buf->asize = new_size; + buf->ptr = new_ptr; + + /* truncate the existing buffer size if necessary */ + if (buf->size >= buf->asize) + buf->size = buf->asize - 1; + buf->ptr[buf->size] = '\0'; + + return 0; +} + +void gh_buf_free(gh_buf *buf) +{ + if (!buf) return; + + if (buf->asize > 0 && buf->ptr != gh_buf__initbuf && buf->ptr != gh_buf__oom) + free(buf->ptr); + + gh_buf_init(buf, 0); +} + +void gh_buf_clear(gh_buf *buf) +{ + buf->size = 0; + + if (buf->asize > 0) + buf->ptr[0] = '\0'; + + if (buf->asize < 0) { + buf->ptr = gh_buf__initbuf; + buf->asize = 0; + } +} + +int gh_buf_set(gh_buf *buf, const char *data, int len) +{ + if (len == 0 || data == NULL) { + gh_buf_clear(buf); + } else { + if (data != buf->ptr) { + ENSURE_SIZE(buf, len + 1); + memmove(buf->ptr, data, len); + } + buf->size = len; + buf->ptr[buf->size] = '\0'; + } + return 0; +} + +int gh_buf_sets(gh_buf *buf, const char *string) +{ + return gh_buf_set(buf, string, string ? strlen(string) : 0); +} + +int gh_buf_putc(gh_buf *buf, char c) +{ + ENSURE_SIZE(buf, buf->size + 2); + buf->ptr[buf->size++] = c; + buf->ptr[buf->size] = '\0'; + return 0; +} + +int gh_buf_put(gh_buf *buf, const char *data, int len) +{ + ENSURE_SIZE(buf, buf->size + len + 1); + memmove(buf->ptr + buf->size, data, len); + buf->size += len; + buf->ptr[buf->size] = '\0'; + return 0; +} + +int gh_buf_puts(gh_buf *buf, const char *string) +{ + assert(string); + return gh_buf_put(buf, string, strlen(string)); +} + +int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap) +{ + const int expected_size = buf->size + (strlen(format) * 2); + int len; + + ENSURE_SIZE(buf, expected_size); + + while (1) { + va_list args; + va_copy(args, ap); + + len = vsnprintf( + buf->ptr + buf->size, + buf->asize - buf->size, + format, args + ); + + if (len < 0) { + free(buf->ptr); + buf->ptr = gh_buf__oom; + return -1; + } + + if (len + 1 <= buf->asize - buf->size) { + buf->size += len; + break; + } + + ENSURE_SIZE(buf, buf->size + len + 1); + } + + return 0; +} + +int gh_buf_printf(gh_buf *buf, const char *format, ...) +{ + int r; + va_list ap; + + va_start(ap, format); + r = gh_buf_vprintf(buf, format, ap); + va_end(ap); + + return r; +} + +void gh_buf_copy_cstr(char *data, size_t datasize, const gh_buf *buf) +{ + size_t copylen; + + assert(data && datasize && buf); + + data[0] = '\0'; + + if (buf->size == 0 || buf->asize <= 0) + return; + + copylen = buf->size; + if (copylen > datasize - 1) + copylen = datasize - 1; + memmove(data, buf->ptr, copylen); + data[copylen] = '\0'; +} + +void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b) +{ + gh_buf t = *buf_a; + *buf_a = *buf_b; + *buf_b = t; +} + +char *gh_buf_detach(gh_buf *buf) +{ + char *data = buf->ptr; + + if (buf->asize == 0 || buf->ptr == gh_buf__oom) + return NULL; + + gh_buf_init(buf, 0); + + return data; +} + +void gh_buf_attach(gh_buf *buf, char *ptr, int asize) +{ + gh_buf_free(buf); + + if (ptr) { + buf->ptr = ptr; + buf->size = strlen(ptr); + if (asize) + buf->asize = (asize < buf->size) ? buf->size + 1 : asize; + else /* pass 0 to fall back on strlen + 1 */ + buf->asize = buf->size + 1; + } else { + gh_buf_grow(buf, asize); + } +} + +int gh_buf_cmp(const gh_buf *a, const gh_buf *b) +{ + int result = memcmp(a->ptr, b->ptr, MIN(a->size, b->size)); + return (result != 0) ? result : + (a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0; +} + +int gh_buf_strchr(const gh_buf *buf, int c, int pos) +{ + const char *p = memchr(buf->ptr + pos, c, buf->size - pos); + if (!p) + return -1; + + return (int)(p - p->ptr); +} + +int gh_buf_strrchr(const gh_buf *buf, int c, int pos) +{ + int i; + + for (i = pos; i >= 0; i--) { + if (buf->ptr[i] == (unsigned char) c) + return i; + } + + return -1; +} + +void gh_buf_truncate(gh_buf *buf, size_t len) +{ + assert(buf->asize >= 0); + + if (len < buf->size) { + buf->size = len; + buf->ptr[buf->size] = '\0'; + } +} + +void gh_buf_ltruncate(gh_buf *buf, size_t len) +{ + assert(buf->asize >= 0); + + if (len && len < buf->size) { + memmove(buf->ptr, buf->ptr + len, buf->size - len); + buf->size -= len; + buf->ptr[buf->size] = '\0'; + } +} + +void gh_buf_trim(gh_buf *buf) +{ + size_t i = 0; + + assert(buf->asize >= 0); + + /* ltrim */ + while (i < buf->size && isspace(buf->ptr[i])) + i++; + + gh_buf_truncate(buf, i); + + /* rtrim */ + while (buf->size > 0) { + if (!isspace(buf->ptr[buf->size - 1])) + break; + + buf->size--; + } + + buf->ptr[buf->size] = '\0'; +} -- cgit v1.2.3 From 582674e662d1f8757350c51486a5e0a837195e15 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Tue, 2 Sep 2014 13:18:04 +0200 Subject: ffffix --- src/buffer.c | 69 ++++++++++++++++++++++-------------------------------------- 1 file changed, 25 insertions(+), 44 deletions(-) (limited to 'src/buffer.c') diff --git a/src/buffer.c b/src/buffer.c index b81e7fa..17dc864 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -31,10 +31,10 @@ void gh_buf_init(gh_buf *buf, int initial_size) int gh_buf_try_grow(gh_buf *buf, int target_size, bool mark_oom) { - char *new_ptr; - size_t new_size; + unsigned char *new_ptr; + int new_size; - if (buf->ptr == gh_buf__oom || buf->asize < 0) + if (buf->ptr == gh_buf__oom) return -1; if (target_size <= buf->asize) @@ -79,7 +79,7 @@ void gh_buf_free(gh_buf *buf) { if (!buf) return; - if (buf->asize > 0 && buf->ptr != gh_buf__initbuf && buf->ptr != gh_buf__oom) + if (buf->ptr != gh_buf__initbuf && buf->ptr != gh_buf__oom) free(buf->ptr); gh_buf_init(buf, 0); @@ -91,14 +91,9 @@ void gh_buf_clear(gh_buf *buf) if (buf->asize > 0) buf->ptr[0] = '\0'; - - if (buf->asize < 0) { - buf->ptr = gh_buf__initbuf; - buf->asize = 0; - } } -int gh_buf_set(gh_buf *buf, const char *data, int len) +int gh_buf_set(gh_buf *buf, const unsigned char *data, int len) { if (len == 0 || data == NULL) { gh_buf_clear(buf); @@ -115,10 +110,12 @@ int gh_buf_set(gh_buf *buf, const char *data, int len) int gh_buf_sets(gh_buf *buf, const char *string) { - return gh_buf_set(buf, string, string ? strlen(string) : 0); + return gh_buf_set(buf, + (const unsigned char *)string, + string ? strlen(string) : 0); } -int gh_buf_putc(gh_buf *buf, char c) +int gh_buf_putc(gh_buf *buf, int c) { ENSURE_SIZE(buf, buf->size + 2); buf->ptr[buf->size++] = c; @@ -126,7 +123,7 @@ int gh_buf_putc(gh_buf *buf, char c) return 0; } -int gh_buf_put(gh_buf *buf, const char *data, int len) +int gh_buf_put(gh_buf *buf, const unsigned char *data, int len) { ENSURE_SIZE(buf, buf->size + len + 1); memmove(buf->ptr + buf->size, data, len); @@ -137,8 +134,7 @@ int gh_buf_put(gh_buf *buf, const char *data, int len) int gh_buf_puts(gh_buf *buf, const char *string) { - assert(string); - return gh_buf_put(buf, string, strlen(string)); + return gh_buf_put(buf, (const unsigned char *)string, strlen(string)); } int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap) @@ -153,7 +149,7 @@ int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap) va_copy(args, ap); len = vsnprintf( - buf->ptr + buf->size, + (char *)buf->ptr + buf->size, buf->asize - buf->size, format, args ); @@ -187,9 +183,9 @@ int gh_buf_printf(gh_buf *buf, const char *format, ...) return r; } -void gh_buf_copy_cstr(char *data, size_t datasize, const gh_buf *buf) +void gh_buf_copy_cstr(char *data, int datasize, const gh_buf *buf) { - size_t copylen; + int copylen; assert(data && datasize && buf); @@ -212,9 +208,9 @@ void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b) *buf_b = t; } -char *gh_buf_detach(gh_buf *buf) +unsigned char *gh_buf_detach(gh_buf *buf) { - char *data = buf->ptr; + unsigned char *data = buf->ptr; if (buf->asize == 0 || buf->ptr == gh_buf__oom) return NULL; @@ -224,13 +220,13 @@ char *gh_buf_detach(gh_buf *buf) return data; } -void gh_buf_attach(gh_buf *buf, char *ptr, int asize) +void gh_buf_attach(gh_buf *buf, unsigned char *ptr, int asize) { gh_buf_free(buf); if (ptr) { buf->ptr = ptr; - buf->size = strlen(ptr); + buf->size = strlen((char *)ptr); if (asize) buf->asize = (asize < buf->size) ? buf->size + 1 : asize; else /* pass 0 to fall back on strlen + 1 */ @@ -249,11 +245,11 @@ int gh_buf_cmp(const gh_buf *a, const gh_buf *b) int gh_buf_strchr(const gh_buf *buf, int c, int pos) { - const char *p = memchr(buf->ptr + pos, c, buf->size - pos); - if (!p) - return -1; + const char *p = memchr(buf->ptr + pos, c, buf->size - pos); + if (!p) + return -1; - return (int)(p - p->ptr); + return (int)(p - buf->ptr); } int gh_buf_strrchr(const gh_buf *buf, int c, int pos) @@ -270,36 +266,21 @@ int gh_buf_strrchr(const gh_buf *buf, int c, int pos) void gh_buf_truncate(gh_buf *buf, size_t len) { - assert(buf->asize >= 0); - if (len < buf->size) { buf->size = len; buf->ptr[buf->size] = '\0'; } } -void gh_buf_ltruncate(gh_buf *buf, size_t len) -{ - assert(buf->asize >= 0); - - if (len && len < buf->size) { - memmove(buf->ptr, buf->ptr + len, buf->size - len); - buf->size -= len; - buf->ptr[buf->size] = '\0'; - } -} - void gh_buf_trim(gh_buf *buf) { - size_t i = 0; - - assert(buf->asize >= 0); - - /* ltrim */ + /* TODO: leading whitespace? */ + /* while (i < buf->size && isspace(buf->ptr[i])) i++; gh_buf_truncate(buf, i); + */ /* rtrim */ while (buf->size > 0) { -- cgit v1.2.3 From c28af79329264a7cf331a1b1c414919e4ed9e9f9 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Tue, 2 Sep 2014 13:37:34 +0200 Subject: It buiiiilds --- src/buffer.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/buffer.c') diff --git a/src/buffer.c b/src/buffer.c index 17dc864..cfc6a7e 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -245,11 +245,11 @@ int gh_buf_cmp(const gh_buf *a, const gh_buf *b) int gh_buf_strchr(const gh_buf *buf, int c, int pos) { - const char *p = memchr(buf->ptr + pos, c, buf->size - pos); + const unsigned char *p = memchr(buf->ptr + pos, c, buf->size - pos); if (!p) return -1; - return (int)(p - buf->ptr); + return (int)(p - (const unsigned char *)buf->ptr); } int gh_buf_strrchr(const gh_buf *buf, int c, int pos) @@ -264,7 +264,7 @@ int gh_buf_strrchr(const gh_buf *buf, int c, int pos) return -1; } -void gh_buf_truncate(gh_buf *buf, size_t len) +void gh_buf_truncate(gh_buf *buf, int len) { if (len < buf->size) { buf->size = len; -- cgit v1.2.3 From a7314deae649646f1f7ce5ede972641b5b62538c Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Wed, 3 Sep 2014 03:40:23 +0200 Subject: 338/103 --- src/buffer.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src/buffer.c') diff --git a/src/buffer.c b/src/buffer.c index cfc6a7e..dc4a405 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -95,7 +95,7 @@ void gh_buf_clear(gh_buf *buf) int gh_buf_set(gh_buf *buf, const unsigned char *data, int len) { - if (len == 0 || data == NULL) { + if (len <= 0 || data == NULL) { gh_buf_clear(buf); } else { if (data != buf->ptr) { @@ -125,6 +125,9 @@ int gh_buf_putc(gh_buf *buf, int c) int gh_buf_put(gh_buf *buf, const unsigned char *data, int len) { + if (len <= 0) + return 0; + ENSURE_SIZE(buf, buf->size + len + 1); memmove(buf->ptr + buf->size, data, len); buf->size += len; @@ -272,15 +275,28 @@ void gh_buf_truncate(gh_buf *buf, int len) } } +void gh_buf_drop(gh_buf *buf, int n) +{ + if (n > 0) { + buf->size = buf->size - n; + if (buf->size) + memmove(buf->ptr, buf->ptr + n, buf->size); + + buf->ptr[buf->size] = '\0'; + } +} + void gh_buf_trim(gh_buf *buf) { - /* TODO: leading whitespace? */ - /* + int i = 0; + + if (!buf->size) + return; + while (i < buf->size && isspace(buf->ptr[i])) i++; - gh_buf_truncate(buf, i); - */ + gh_buf_drop(buf, i); /* rtrim */ while (buf->size > 0) { -- cgit v1.2.3 From 543c2c94d71adee42c7bd2f8027d75c87ed8120d Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 4 Sep 2014 18:38:14 +0200 Subject: Rename to strbuf --- src/buffer.c | 86 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 43 insertions(+), 43 deletions(-) (limited to 'src/buffer.c') diff --git a/src/buffer.c b/src/buffer.c index dc4a405..90c2186 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -9,32 +9,32 @@ #include "buffer.h" -/* Used as default value for gh_buf->ptr so that people can always - * assume ptr is non-NULL and zero terminated even for new gh_bufs. +/* Used as default value for strbuf->ptr so that people can always + * assume ptr is non-NULL and zero terminated even for new strbufs. */ -unsigned char gh_buf__initbuf[1]; -unsigned char gh_buf__oom[1]; +unsigned char strbuf__initbuf[1]; +unsigned char strbuf__oom[1]; #define ENSURE_SIZE(b, d) \ - if ((d) > buf->asize && gh_buf_grow(b, (d)) < 0)\ + if ((d) > buf->asize && strbuf_grow(b, (d)) < 0)\ return -1; -void gh_buf_init(gh_buf *buf, int initial_size) +void strbuf_init(strbuf *buf, int initial_size) { buf->asize = 0; buf->size = 0; - buf->ptr = gh_buf__initbuf; + buf->ptr = strbuf__initbuf; if (initial_size) - gh_buf_grow(buf, initial_size); + strbuf_grow(buf, initial_size); } -int gh_buf_try_grow(gh_buf *buf, int target_size, bool mark_oom) +int strbuf_try_grow(strbuf *buf, int target_size, bool mark_oom) { unsigned char *new_ptr; int new_size; - if (buf->ptr == gh_buf__oom) + if (buf->ptr == strbuf__oom) return -1; if (target_size <= buf->asize) @@ -60,7 +60,7 @@ int gh_buf_try_grow(gh_buf *buf, int target_size, bool mark_oom) if (!new_ptr) { if (mark_oom) - buf->ptr = gh_buf__oom; + buf->ptr = strbuf__oom; return -1; } @@ -75,17 +75,17 @@ int gh_buf_try_grow(gh_buf *buf, int target_size, bool mark_oom) return 0; } -void gh_buf_free(gh_buf *buf) +void strbuf_free(strbuf *buf) { if (!buf) return; - if (buf->ptr != gh_buf__initbuf && buf->ptr != gh_buf__oom) + if (buf->ptr != strbuf__initbuf && buf->ptr != strbuf__oom) free(buf->ptr); - gh_buf_init(buf, 0); + strbuf_init(buf, 0); } -void gh_buf_clear(gh_buf *buf) +void strbuf_clear(strbuf *buf) { buf->size = 0; @@ -93,10 +93,10 @@ void gh_buf_clear(gh_buf *buf) buf->ptr[0] = '\0'; } -int gh_buf_set(gh_buf *buf, const unsigned char *data, int len) +int strbuf_set(strbuf *buf, const unsigned char *data, int len) { if (len <= 0 || data == NULL) { - gh_buf_clear(buf); + strbuf_clear(buf); } else { if (data != buf->ptr) { ENSURE_SIZE(buf, len + 1); @@ -108,14 +108,14 @@ int gh_buf_set(gh_buf *buf, const unsigned char *data, int len) return 0; } -int gh_buf_sets(gh_buf *buf, const char *string) +int strbuf_sets(strbuf *buf, const char *string) { - return gh_buf_set(buf, + return strbuf_set(buf, (const unsigned char *)string, string ? strlen(string) : 0); } -int gh_buf_putc(gh_buf *buf, int c) +int strbuf_putc(strbuf *buf, int c) { ENSURE_SIZE(buf, buf->size + 2); buf->ptr[buf->size++] = c; @@ -123,7 +123,7 @@ int gh_buf_putc(gh_buf *buf, int c) return 0; } -int gh_buf_put(gh_buf *buf, const unsigned char *data, int len) +int strbuf_put(strbuf *buf, const unsigned char *data, int len) { if (len <= 0) return 0; @@ -135,12 +135,12 @@ int gh_buf_put(gh_buf *buf, const unsigned char *data, int len) return 0; } -int gh_buf_puts(gh_buf *buf, const char *string) +int strbuf_puts(strbuf *buf, const char *string) { - return gh_buf_put(buf, (const unsigned char *)string, strlen(string)); + return strbuf_put(buf, (const unsigned char *)string, strlen(string)); } -int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap) +int strbuf_vprintf(strbuf *buf, const char *format, va_list ap) { const int expected_size = buf->size + (strlen(format) * 2); int len; @@ -159,7 +159,7 @@ int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap) if (len < 0) { free(buf->ptr); - buf->ptr = gh_buf__oom; + buf->ptr = strbuf__oom; return -1; } @@ -174,19 +174,19 @@ int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap) return 0; } -int gh_buf_printf(gh_buf *buf, const char *format, ...) +int strbuf_printf(strbuf *buf, const char *format, ...) { int r; va_list ap; va_start(ap, format); - r = gh_buf_vprintf(buf, format, ap); + r = strbuf_vprintf(buf, format, ap); va_end(ap); return r; } -void gh_buf_copy_cstr(char *data, int datasize, const gh_buf *buf) +void strbuf_copy_cstr(char *data, int datasize, const strbuf *buf) { int copylen; @@ -204,28 +204,28 @@ void gh_buf_copy_cstr(char *data, int datasize, const gh_buf *buf) data[copylen] = '\0'; } -void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b) +void strbuf_swap(strbuf *buf_a, strbuf *buf_b) { - gh_buf t = *buf_a; + strbuf t = *buf_a; *buf_a = *buf_b; *buf_b = t; } -unsigned char *gh_buf_detach(gh_buf *buf) +unsigned char *strbuf_detach(strbuf *buf) { unsigned char *data = buf->ptr; - if (buf->asize == 0 || buf->ptr == gh_buf__oom) + if (buf->asize == 0 || buf->ptr == strbuf__oom) return NULL; - gh_buf_init(buf, 0); + strbuf_init(buf, 0); return data; } -void gh_buf_attach(gh_buf *buf, unsigned char *ptr, int asize) +void strbuf_attach(strbuf *buf, unsigned char *ptr, int asize) { - gh_buf_free(buf); + strbuf_free(buf); if (ptr) { buf->ptr = ptr; @@ -235,18 +235,18 @@ void gh_buf_attach(gh_buf *buf, unsigned char *ptr, int asize) else /* pass 0 to fall back on strlen + 1 */ buf->asize = buf->size + 1; } else { - gh_buf_grow(buf, asize); + strbuf_grow(buf, asize); } } -int gh_buf_cmp(const gh_buf *a, const gh_buf *b) +int strbuf_cmp(const strbuf *a, const strbuf *b) { int result = memcmp(a->ptr, b->ptr, MIN(a->size, b->size)); return (result != 0) ? result : (a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0; } -int gh_buf_strchr(const gh_buf *buf, int c, int pos) +int strbuf_strchr(const strbuf *buf, int c, int pos) { const unsigned char *p = memchr(buf->ptr + pos, c, buf->size - pos); if (!p) @@ -255,7 +255,7 @@ int gh_buf_strchr(const gh_buf *buf, int c, int pos) return (int)(p - (const unsigned char *)buf->ptr); } -int gh_buf_strrchr(const gh_buf *buf, int c, int pos) +int strbuf_strrchr(const strbuf *buf, int c, int pos) { int i; @@ -267,7 +267,7 @@ int gh_buf_strrchr(const gh_buf *buf, int c, int pos) return -1; } -void gh_buf_truncate(gh_buf *buf, int len) +void strbuf_truncate(strbuf *buf, int len) { if (len < buf->size) { buf->size = len; @@ -275,7 +275,7 @@ void gh_buf_truncate(gh_buf *buf, int len) } } -void gh_buf_drop(gh_buf *buf, int n) +void strbuf_drop(strbuf *buf, int n) { if (n > 0) { buf->size = buf->size - n; @@ -286,7 +286,7 @@ void gh_buf_drop(gh_buf *buf, int n) } } -void gh_buf_trim(gh_buf *buf) +void strbuf_trim(strbuf *buf) { int i = 0; @@ -296,7 +296,7 @@ void gh_buf_trim(gh_buf *buf) while (i < buf->size && isspace(buf->ptr[i])) i++; - gh_buf_drop(buf, i); + strbuf_drop(buf, i); /* rtrim */ while (buf->size > 0) { -- cgit v1.2.3 From 94a79a605f3e76a43f1f87a5044f6761b99e5ca5 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Wed, 10 Sep 2014 18:33:27 +0200 Subject: Cleanup reference implementation --- src/buffer.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/buffer.c') diff --git a/src/buffer.c b/src/buffer.c index 90c2186..cdf8ca0 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -308,3 +308,46 @@ void strbuf_trim(strbuf *buf) buf->ptr[buf->size] = '\0'; } + +// Destructively modify string, collapsing consecutive +// space and newline characters into a single space. +void strbuf_normalize_whitespace(strbuf *s) +{ + bool last_char_was_space = false; + int r, w; + + for (r = 0, w = 0; r < s->size; ++r) { + switch (s->ptr[r]) { + case ' ': + case '\n': + if (last_char_was_space) + break; + + s->ptr[w++] = ' '; + last_char_was_space = true; + break; + + default: + s->ptr[w++] = s->ptr[r]; + last_char_was_space = false; + } + } + + strbuf_truncate(s, w); +} + +// Destructively unescape a string: remove backslashes before punctuation chars. +extern void strbuf_unescape(strbuf *buf) +{ + int r, w; + + for (r = 0, w = 0; r < buf->size; ++r) { + if (buf->ptr[r] == '\\' && ispunct(buf->ptr[r + 1])) + continue; + + buf->ptr[w++] = buf->ptr[r]; + } + + strbuf_truncate(buf, w); +} + -- cgit v1.2.3 From 8c028e1a88c2d2aac4a4086202568bee43678aa8 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Wed, 10 Sep 2014 19:50:29 +0200 Subject: Do not create references with empty names --- src/buffer.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/buffer.c') diff --git a/src/buffer.c b/src/buffer.c index cdf8ca0..7c2b86b 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -215,11 +215,12 @@ unsigned char *strbuf_detach(strbuf *buf) { unsigned char *data = buf->ptr; - if (buf->asize == 0 || buf->ptr == strbuf__oom) - return NULL; + if (buf->asize == 0 || buf->ptr == strbuf__oom) { + /* return an empty string */ + return calloc(1, 1); + } strbuf_init(buf, 0); - return data; } -- cgit v1.2.3