Mercurial > code
changeset 141:5ed54050ae31
Added buf_cut, cut the buffer
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 02 May 2012 20:20:22 +0200 |
parents | 09fb5267c906 |
children | e3cf5ac9a5aa |
files | buf.c buf.h |
diffstat | 2 files changed, 28 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/buf.c Wed Apr 18 21:30:29 2012 +0200 +++ b/buf.c Wed May 02 20:20:22 2012 +0200 @@ -130,7 +130,9 @@ int buf_putc(struct buf *buf, int c) { - return buf_printf(buf, "%c", c); + char str[2] = { c, '\0' }; + + return buf_ncat(buf, str, 1); } /* @@ -204,6 +206,28 @@ } /* + * Remove `n' characters from the buffer, a positive value will cut the string + * from beginning, negative value will cut from end. + */ + +void +buf_cut(struct buf *buf, int n) +{ + int pos; + + if (n < 0 || (unsigned int)n >= buf->length) + return; + + if (n < 0 && buf->length - (-n) > 0) + pos = buf->length - (-n); + else if ((unsigned int)n < buf->length) + pos = n; + + buf->text[pos] = '\0'; + buf->length -= buf->length - pos; +} + +/* * Clear the string buffer. */
--- a/buf.h Wed Apr 18 21:30:29 2012 +0200 +++ b/buf.h Wed May 02 20:20:22 2012 +0200 @@ -31,10 +31,6 @@ #ifdef __GNUC__ # define _buf_at_printf(i1, i2) __attribute__ ((format (printf, i1, i2))) -# if __GNUC_MINOR__ > 2 -# pragma GCC diagnostic push -# pragma GCC diagnostic warning "-Wformat" -# endif #else # define _buf_at_printf(i1, i2) #endif @@ -82,15 +78,14 @@ buf_trim(struct buf *); void +buf_cut(struct buf *, int); + +void buf_clear(struct buf *); void buf_free(struct buf *); -#if defined(__GNUC__) && __GNUC_MINOR__ > 2 -# pragma GCC diagnostic pop -#endif - #ifdef __cplusplus } #endif