]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/tools.c
* move utf8 handling stuff into strbuf, so we can be more exact about our buffer...
[citadel.git] / libcitadel / lib / tools.c
index a772bcadec1f2ee60ea26d732500480d453f24c6..03d3213de62542568426f793b5cbac89fc4321c0 100644 (file)
@@ -456,30 +456,68 @@ char *rfc2047encode(char *line, long length)
        return result;
 }
 
-
 /*
- * Strip leading and trailing spaces from a string
+ * removes double slashes from pathnames
+ * allows / disallows trailing slashes
  */
-void striplt(char *buf)
+void StripSlashes(char *Dir, int TrailingSlash)
 {
-       size_t len;
-       int a;
+       char *a, *b;
+
+       a = b = Dir;
+
+       while (!IsEmptyStr(a)) {
+               if (*a == '/') {
+                       while (*a == '/')
+                               a++;
+                       *b = '/';
+                       b++;
+               }
+               else {
+                       *b = *a;
+                       b++; a++;
+               }
+       }
+       if ((TrailingSlash) && (*(b - 1) != '/')){
+               *b = '/';
+               b++;
+       }
+       *b = '\0';
 
-       if (buf==NULL) return;
-       if (IsEmptyStr(buf)) return;
-       len = strlen(buf);
-        while ((!IsEmptyStr(buf)) && (isspace(buf[len - 1])))
-                buf[--len] = 0;
-       if (IsEmptyStr(buf)) return;
-       a = 0;
-        while ((!IsEmptyStr(buf)) && (isspace(buf[a])))
-               a++;
-       if (a > 0)
-                memmove(buf, &buf[a], len - a + 1);
 }
 
+/*
+ * Strip leading and trailing spaces from a string
+ */
+size_t striplt(char *buf) {
+       char *first_nonspace = NULL;
+       char *last_nonspace = NULL;
+       char *ptr;
+       size_t new_len = 0;
+
+       if (!buf) {
+               return 0;
+       }
+
+       for (ptr=buf; *ptr!=0; ++ptr) {
+               if (!isspace(*ptr)) {
+                       if (!first_nonspace) {
+                               first_nonspace = ptr;
+                       }
+                       last_nonspace = ptr;
+               }
+       }
 
+       if ((!first_nonspace) || (!last_nonspace)) {
+               buf[0] = 0;
+               return 0;
+       }
 
+       new_len = last_nonspace - first_nonspace + 1;
+       memmove(buf, first_nonspace, new_len);
+       buf[new_len] = 0;
+       return new_len;
+}
 
 
 /**
@@ -767,21 +805,16 @@ void generate_uuid(char *buf) {
  * The code is roughly based on the strstr() replacement from 'tin' written
  * by Urs Jannsen.
  */
-char *bmstrcasestr(char *text, char *pattern) {
+inline char *_bmstrcasestr_len(char *text, size_t textlen, char *pattern, size_t patlen) {
 
        register unsigned char *p, *t;
        register int i, j, *delta;
        register size_t p1;
        int deltaspace[256];
-       size_t textlen;
-       size_t patlen;
 
        if (!text) return(NULL);
        if (!pattern) return(NULL);
 
-       textlen = strlen (text);
-       patlen = strlen (pattern);
-
        /* algorithm fails if pattern is empty */
        if ((p1 = patlen) == 0)
                return (text);
@@ -823,7 +856,29 @@ char *bmstrcasestr(char *text, char *pattern) {
        return (NULL);
 }
 
+/*
+ * bmstrcasestr() -- case-insensitive substring search
+ *
+ * This uses the Boyer-Moore search algorithm and is therefore quite fast.
+ * The code is roughly based on the strstr() replacement from 'tin' written
+ * by Urs Jannsen.
+ */
+char *bmstrcasestr(char *text, char *pattern) {
+       size_t textlen;
+       size_t patlen;
 
+       if (!text) return(NULL);
+       if (!pattern) return(NULL);
+
+       textlen = strlen (text);
+       patlen = strlen (pattern);
+
+       return _bmstrcasestr_len(text, textlen, pattern, patlen);
+}
+
+char *bmstrcasestr_len(char *text, size_t textlen, char *pattern, size_t patlen) {
+       return _bmstrcasestr_len(text, textlen, pattern, patlen);
+}
 
 /*
  * Local replacement for controversial C library function that generates
@@ -921,97 +976,6 @@ void stripltlen(char *buf, int *len)
        }
 }
 
-/**
- * \brief detect whether this char starts an utf-8 encoded char
- * \param Char character to inspect
- * \returns yes or no
- */
-inline int Ctdl_IsUtf8SequenceStart(char Char)
-{
-/** 11??.???? indicates an UTF8 Sequence. */
-       return ((Char & 0xC0) != 0);
-}
-
-/**
- * \brief evaluate the length of an utf8 special character sequence
- * \param Char the character to examine
- * \returns width of utf8 chars in bytes
- */
-inline int Ctdl_GetUtf8SequenceLength(char Char)
-{
-       int n = 1;
-        char test = (1<<7);
-       
-       while ((n < 8) && ((test & Char) != 0)) {
-               test = test << 1;
-               n ++;
-       }
-       if (n > 6)
-               n = 1;
-       return n;
-}
-
-/**
- * \brief measure the number of glyphs in an UTF8 string...
- * \param str string to measure
- * \returns the length of str
- */
-int Ctdl_Utf8StrLen(char *str)
-{
-       int n = 0;
-       int m = 0;
-       char *aptr;
-
-       if (str == NULL)
-               return n;
-       aptr = str;
-       while (*aptr != '\0') {
-               if (Ctdl_IsUtf8SequenceStart(*aptr)){
-                       m = Ctdl_GetUtf8SequenceLength(*aptr);
-                       while ((m-- > 0) && (*aptr++ != '\0'))
-                               n ++;
-               }
-               else {
-                       n++;
-                       aptr++;
-               }
-                       
-       }
-       return n;
-}
-
-/**
- * \brief cuts a string after maxlen glyphs
- * \param str string to cut to maxlen glyphs
- * \param maxlen how long may the string become?
- * \returns pointer to maxlen or the end of the string
- */
-char *Ctdl_Utf8StrCut(char *str, int maxlen)
-{
-       int n = 0, m = 0;
-       char *aptr;
-
-       if (str == NULL)
-               return NULL;
-       aptr = str;
-       while (*aptr != '\0') {
-               if (Ctdl_IsUtf8SequenceStart(*aptr)){
-                       m = Ctdl_GetUtf8SequenceLength(*aptr);
-                       while ((m-- > 0) && (*aptr++ != '\0'))
-                               n ++;
-               }
-               else {
-                       n++;
-                       aptr++;
-               }
-               if (n > maxlen) {
-                       *aptr = '\0';
-                       return aptr;
-               }                       
-       }
-       return aptr;
-}
-
 
 /*
  * Convert all whitespace characters in a supplied string to underscores