* StrBufTrim; port of striplt to strbuf; since the rest of the world calls this Trim...
authorWilfried Göesgens <willi@citadel.org>
Tue, 14 Oct 2008 22:15:45 +0000 (22:15 +0000)
committerWilfried Göesgens <willi@citadel.org>
Tue, 14 Oct 2008 22:15:45 +0000 (22:15 +0000)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c

index 942dcbea79fb7f825768d8e584246720934a6912..c89a511d459ce0fad3ffba3349037f4661ad306b 100644 (file)
@@ -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);
index 9093049bff844c64b2fcbb99cec84009d5e088fc..892f99e31d07fc0472e593799c0881ae5a5b7d46 100644 (file)
@@ -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;