From: Pierre Habouzit Date: Sun, 13 Jan 2008 20:14:45 +0000 (+0100) Subject: More string and buffer functions. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=6d838d9aef36d95fa439b3f7cc06d4e81c8581bd;p=apps%2Fmadmutt.git More string and buffer functions. Signed-off-by: Pierre Habouzit --- diff --git a/lib-lib/buffer.c b/lib-lib/buffer.c index f27dc51..4cc4dc5 100644 --- a/lib-lib/buffer.c +++ b/lib-lib/buffer.c @@ -33,6 +33,8 @@ void buffer_splice(buffer_t *buf, ssize_t pos, ssize_t len, { if (dlen >= len) buffer_extend(buf, dlen - len); + if (len >= buf->len) + len = buf->len; memmove(buf->data + pos + dlen, buf->data + pos + len, buf->len - pos - len); @@ -40,6 +42,12 @@ void buffer_splice(buffer_t *buf, ssize_t pos, ssize_t len, buffer_setlen(buf, buf->len + dlen - len); } +void buffer_consume_upto(buffer_t *buf, const char *s) +{ + assert (buf->data <= s && s <= buf->data + buf->len); + buffer_splice(buf, 0, s - buf->data, NULL, 0); +} + ssize_t buffer_addvf(buffer_t *buf, const char *fmt, va_list args) { ssize_t len; diff --git a/lib-lib/buffer.h b/lib-lib/buffer.h index 41f9ccf..b73e506 100644 --- a/lib-lib/buffer.h +++ b/lib-lib/buffer.h @@ -101,6 +101,7 @@ static inline void buffer_reset(buffer_t *buf) { } void buffer_splice(buffer_t *, ssize_t pos, ssize_t len, const void *, ssize_t); +void buffer_consume_upto(buffer_t *, const char *s); ssize_t buffer_addvf(buffer_t *buf, const char *fmt, va_list) __attribute__((format(printf, 2, 0))); diff --git a/lib-lib/str.h b/lib-lib/str.h index d455666..f9ef19f 100644 --- a/lib-lib/str.h +++ b/lib-lib/str.h @@ -218,6 +218,28 @@ ssize_t m_strwidth(const char *s); * \return 1 if a match is found, 0 otherwise. */ static inline int m_strstart(const char *s, const char *p, const char **pp) +{ + if (!s) + return 0; + + while (*p) { + if (ascii_tolower(*s++) != ascii_tolower(*p++)) + return 0; + } + if (pp) + *pp = s; + return 1; +} + +/** \brief Tells whether s begins with p, case insensitive. + * + * \param[in] s the input string + * \param[in] p the prefix + * \param[out] pp position in s + * + * \return 1 if a match is found, 0 otherwise. + */ +static inline int m_strcasestart(const char *s, const char *p, const char **pp) { if (!s) return 0;