From 533d77dd32bdd29a9ba18130e6161b8a253450c4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Tue, 14 Oct 2008 22:15:45 +0000 Subject: [PATCH] * StrBufTrim; port of striplt to strbuf; since the rest of the world calls this Trim(), lets do so too. --- libcitadel/lib/libcitadel.h | 1 + libcitadel/lib/stringbuf.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 942dcbea7..c89a511d4 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -254,6 +254,7 @@ void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap); void StrBufPrintf(StrBuf *Buf, const char *format, ...) __attribute__((__format__(__printf__,2,3))); void StrBufCutLeft(StrBuf *Buf, int nChars); void StrBufCutRight(StrBuf *Buf, int nChars); +void StrBufTrim(StrBuf *Buf); void StrBufUpCase(StrBuf *Buf); void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source); void StrBufEUid_escapize(StrBuf *target, const StrBuf *source); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 9093049bf..892f99e31 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -1140,6 +1140,29 @@ void StrBufCutRight(StrBuf *Buf, int nChars) } +/* + * Strip leading and trailing spaces from a string; with premeasured and adjusted length. + * buf - the string to modify + * len - length of the string. + */ +void StrBufTrim(StrBuf *Buf) +{ + int delta = 0; + if ((Buf == NULL) || (Buf->BufUsed == 0)) return; + + while ((Buf->BufUsed > delta) && (isspace(Buf->buf[delta]))){ + delta ++; + } + if (delta > 0) StrBufCutLeft(Buf, delta); + + if (Buf->BufUsed == 0) return; + while (isspace(Buf->buf[Buf->BufUsed - 1])){ + Buf->BufUsed --; + } + Buf->buf[Buf->BufUsed] = '\0'; +} + + void StrBufUpCase(StrBuf *Buf) { char *pch, *pche; -- 2.39.2