]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/stringbuf.c
* StrBufTrim; port of striplt to strbuf; since the rest of the world calls this Trim...
[citadel.git] / libcitadel / lib / stringbuf.c
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;