fix size of returned array in str_split, doc clarification incoming
[framework/uifw/eina.git] / src / lib / eina_str.c
index 7776eb2..2a54c00 100644 (file)
@@ -56,6 +56,7 @@ eina_str_has_suffix_helper(const char *str,
    size_t str_len;
    size_t suffix_len;
 
+   if ((!str) || (!suffix)) return EINA_FALSE;
    str_len = strlen(str);
    suffix_len = eina_strlen_bounded(suffix, str_len);
    if (suffix_len == (size_t)-1)
@@ -70,11 +71,38 @@ eina_str_split_full_helper(const char *str,
                            int max_tokens,
                            unsigned int *elements)
 {
-   char *s, **str_array;
+   char *s, *pos, **str_array;
    const char *src;
    size_t len, dlen;
-   unsigned int tokens;
+   unsigned int tokens = 0, x;
+   const char *idx[256] = {NULL};
 
+   if (max_tokens < 0) max_tokens = 0;
+   if (max_tokens == 1)
+     {
+        str_array = malloc(sizeof(char *) * 2);
+        if (!str_array)
+          {
+             if (elements)
+                *elements = 0;
+
+             return NULL;
+          }
+
+        s = strdup(str);
+        if (!s)
+          {
+             free(str_array);
+             if (elements)
+                *elements = 0;
+
+             return NULL;
+          }
+        if (elements)
+          *elements = 1;
+        str_array[0] = s;
+        return str_array;
+     }
    dlen = strlen(delim);
    if (dlen == 0)
      {
@@ -84,7 +112,6 @@ eina_str_split_full_helper(const char *str,
         return NULL;
      }
 
-   tokens = 0;
    src = str;
    /* count tokens and check strlen(str) */
    while (*src != '\0')
@@ -99,15 +126,18 @@ eina_str_split_full_helper(const char *str,
         if (EINA_UNLIKELY(d == d_end))
           {
              src = tmp;
+             if (tokens < (sizeof(idx) / sizeof(idx[0])))
+               {
+                  idx[tokens] = tmp;
+                  //printf("token %d='%s'\n", tokens + 1, idx[tokens]);
+               }
              tokens++;
+             if (tokens && (tokens == (unsigned int)max_tokens)) break;
           }
         else
            src++;
      }
-   len = src - str;
-
-   if ((max_tokens > 0) && (tokens > (unsigned int)max_tokens))
-      tokens = max_tokens;
+   len = src - str + strlen(src);
 
    str_array = malloc(sizeof(char *) * (tokens + 2));
    if (!str_array)
@@ -118,6 +148,24 @@ eina_str_split_full_helper(const char *str,
         return NULL;
      }
 
+   if (!tokens)
+     {
+        s = strdup(str);
+        if (!s)
+          {
+             free(str_array);
+             if (elements)
+                *elements = 0;
+
+             return NULL;
+          }
+        str_array[0] = s;
+        str_array[1] = NULL;
+        if (elements)
+          *elements = 1;
+        return str_array;
+     }
+
    s = malloc(len + 1);
    if (!s)
      {
@@ -128,38 +176,114 @@ eina_str_split_full_helper(const char *str,
         return NULL;
      }
 
-   /* copy tokens and string */
-   tokens = 0;
    str_array[0] = s;
-   src = str;
-   while (*src != '\0')
+
+   if (len == tokens * dlen)
      {
-        const char *d = delim, *d_end = d + dlen;
-        const char *tmp = src;
-        for (; (d < d_end) && (*tmp != '\0'); d++, tmp++)
+        /* someone's having a laugh somewhere */
+        memset(s, 0, len + 1);
+        for (x = 1; x < tokens + 1; x++)
+          str_array[x] = s + x;
+        str_array[x] = NULL;
+        if (elements)
+          *elements = x;
+        return str_array;
+     }
+   /* copy tokens and string */
+   if (idx[0] - str - dlen > len)
+     {
+        /* FIXME: don't think this can happen but putting this here just in case */
+        abort();
+     }
+   pos = s;
+   for (x = 0; x < MIN(tokens, (sizeof(idx) / sizeof(idx[0]))); x++)
+     {
+        if (x + 1 < (sizeof(idx) / sizeof(idx[0])))
           {
-             if (EINA_LIKELY(*d != *tmp))
-                break;
+             /* first one is special */
+             if (!x)
+               {
+                  eina_strlcpy(pos, str, idx[x] - str - dlen + 1);
+                  str_array[x] = pos;
+                  //printf("str_array[%d] = '%s'\n", x, str_array[x]);
+                  pos += idx[x] - str - dlen + 1;
+                  if ((tokens == 1) && (idx[0]))
+                    {
+                       eina_strlcpy(pos, idx[x], len + 1 - (pos - s));
+                       x++, tokens++;
+                       str_array[x] = pos;
+                    }
+               }
+             /* more tokens */
+             else if (idx[x + 1])
+               {
+                  eina_strlcpy(pos, idx[x - 1], idx[x] - idx[x - 1] - dlen + 1);
+                  str_array[x] = pos;
+                  //printf("str_array[%d] = '%s'\n", x, str_array[x]);
+                  pos += idx[x] - idx[x - 1] - dlen + 1;
+               }
+             /* last token */
+             else
+               {
+                  if (max_tokens && ((unsigned int)max_tokens < tokens + 1))
+                    eina_strlcpy(pos, idx[x - 1], len + 1 - (pos - s));
+                  else
+                    {
+                       //printf("diff: %d\n", len + 1 - (pos - s));
+                       eina_strlcpy(pos, idx[x - 1], idx[x] - idx[x - 1] - dlen + 1);
+                       str_array[x] = pos;
+                       //printf("str_array[%d] = '%s'\n", x, str_array[x]);
+                       pos += idx[x] - idx[x - 1] - dlen + 1;
+                       x++, tokens++;
+                       eina_strlcpy(pos, idx[x - 1], len + 1 - (pos - s));
+                    }
+                  str_array[x] = pos;
+                  //printf("str_array[%d] = '%s'\n", x, str_array[x]);
+               }
           }
-        if (EINA_UNLIKELY(d == d_end))
+        /* no more tokens saved after this one */
+        else
           {
-             src = tmp;
-             *s = '\0';
-             s += dlen;
-             tokens++;
-             str_array[tokens] = s;
+             eina_strlcpy(pos, idx[x - 1], idx[x] - idx[x - 1] - dlen + 1);
+             str_array[x] = pos;
+             //printf("str_array[%d] = '%s'\n", x, str_array[x]);
+             pos += idx[x] - idx[x - 1] - dlen + 1;
+             src = idx[x];
+             x++, tokens++;
+             str_array[x] = s = pos;
+             break;
           }
-        else
+     }
+   if ((x != tokens) && ((!max_tokens) || (x < tokens)))
+     {
+        while (*src != '\0')
           {
-             *s = *src;
-             s++;
-             src++;
+             const char *d = delim, *d_end = d + dlen;
+             const char *tmp = src;
+             for (; (d < d_end) && (*tmp != '\0'); d++, tmp++)
+               {
+                  if (EINA_LIKELY(*d != *tmp))
+                     break;
+               }
+             if (((!max_tokens) || (((tokens == (unsigned int)max_tokens) || x < tokens - 2))) && (EINA_UNLIKELY(d == d_end)))
+               {
+                  src = tmp;
+                  *s = '\0';
+                  s++, x++;
+                  //printf("str_array[%d] = '%s'\n", x, str_array[x - 1]);
+                  str_array[x] = s;
+               }
+             else
+               {
+                  *s = *src;
+                  s++, src++;
+               }
           }
+        *s = 0;
      }
-   *s = '\0';
-   str_array[tokens + 1] = NULL;
+   str_array[tokens] = NULL;
    if (elements)
-      *elements = (tokens + 1);
+     *elements = tokens;
 
    return str_array;
 }
@@ -176,29 +300,6 @@ eina_str_split_full_helper(const char *str,
 *                                   API                                      *
 *============================================================================*/
 
-/**
- * @addtogroup Eina_String_Group String
- *
- * @brief These functions provide useful C string management.
- *
- * @{
- */
-
-
-/**
- * @brief Copy a c-string to another.
- *
- * @param dst The destination string.
- * @param src The source string.
- * @param siz The size of the destination string.
- * @return The length of the source string.
- *
- * This function copies up to @p siz - 1 characters from the
- * NUL-terminated string @p src to @p dst, NUL-terminating the result
- * (unless @p siz is equal to 0). The returned value is the length of
- * @p src. If the returned value is greater than @p siz, truncation
- * occured.
- */
 EAPI size_t
 eina_strlcpy(char *dst, const char *src, size_t siz)
 {
@@ -231,21 +332,6 @@ eina_strlcpy(char *dst, const char *src, size_t siz)
 #endif
 }
 
-/**
- * @brief Append a c-string.
- *
- * @param dst The destination string.
- * @param src The source string.
- * @param siz The size of the destination string.
- * @return The length of the source string plus MIN(siz, strlen(initial dst))
- *
- * This function appends @p src to @p dst of size @p siz (unlike
- * strncat, @p siz is the full size of @p dst, not space left).  At
- * most @p siz - 1 characters will be copied.  Always NUL terminates
- * (unless @p siz <= strlen(dst)). This function returns strlen(src) +
- * MIN(siz, strlen(initial dst)). If the returned value is greater or
- * equal than @p siz, truncation occurred.
- */
 EAPI size_t
 eina_strlcat(char *dst, const char *src, size_t siz)
 {
@@ -277,17 +363,6 @@ eina_strlcat(char *dst, const char *src, size_t siz)
    return(dlen + (s - src)); /* count does not include NUL */
 }
 
-/**
- * @brief Check if the given string has the given prefix.
- *
- * @param str The string to work with.
- * @param prefix The prefix to check for.
- * @return #EINA_TRUE if the string has the given prefix, #EINA_FALSE otherwise.
- *
- * This function returns #EINA_TRUE if @p str has the prefix
- * @p prefix, #EINA_FALSE otherwise. If the length of @p prefix is
- * greater than @p str, #EINA_FALSE is returned.
- */
 EAPI Eina_Bool
 eina_str_has_prefix(const char *str, const char *prefix)
 {
@@ -302,68 +377,18 @@ eina_str_has_prefix(const char *str, const char *prefix)
    return (strncmp(str, prefix, prefix_len) == 0);
 }
 
-/**
- * @brief Check if the given string has the given suffix.
- *
- * @param str The string to work with.
- * @param suffix The suffix to check for.
- * @return #EINA_TRUE if the string has the given suffix, #EINA_FALSE otherwise.
- *
- * This function returns #EINA_TRUE if @p str has the suffix
- * @p suffix, #EINA_FALSE otherwise. If the length of @p suffix is
- * greater than @p str, #EINA_FALSE is returned.
- */
-/**
- * @param str the string to work with
- * @param suffix the suffix to check for
- * @return true if str has the given suffix
- * @brief checks if the string has the given suffix
- */
 EAPI Eina_Bool
 eina_str_has_suffix(const char *str, const char *suffix)
 {
    return eina_str_has_suffix_helper(str, suffix, strcmp);
 }
 
-/**
- * @brief Check if the given string has the given suffix.
- *
- * @param str The string to work with.
- * @param ext The  extension to check for.
- * @return #EINA_TRUE if the string has the given extension, #EINA_FALSE otherwise.
- *
- * This function does the same like eina_str_has_suffix(), but with a
- * case insensitive compare.
- */
 EAPI Eina_Bool
 eina_str_has_extension(const char *str, const char *ext)
 {
    return eina_str_has_suffix_helper(str, ext, strcasecmp);
 }
 
-/**
- * @brief Split a string using a delimiter and returns number of elements.
- *
- * @param str The string to split.
- * @param delim The string which specifies the places at which to split the string.
- * @param max_tokens The maximum number of strings to split string into.
- * @param elements Where to return the number of elements in returned
- *        array (not counting the terminating @c NULL). May be @c NULL.
- * @return A newly-allocated NULL-terminated array of strings or NULL if it
- * fails to allocate the array.
- *
- * This functin splits @p str into a maximum of @p max_tokens pieces,
- * using the given delimiter @p delim. @p delim is not included in any
- * of the resulting strings, unless @p max_tokens is reached. If
- * @p max_tokens is less than @c 1, the string is splitted completely. If
- * @p max_tokens is reached, the last string in the returned string
- * array contains the remainder of string. The returned value is a
- * newly allocated NULL-terminated array of strings or NULL if it fails to
- * allocate the array. To free it, free the first element of the array and the
- * array itself.
- *
- * @see eina_str_split()
- */
 EAPI char **
 eina_str_split_full(const char *str,
                     const char *delim,
@@ -374,57 +399,12 @@ eina_str_split_full(const char *str,
 }
 
 
-/**
- * @brief Split a string using a delimiter.
- *
- * @param str The string to split.
- * @param delim The string which specifies the places at which to split the string.
- * @param max_tokens The maximum number of strings to split string into.
- * @return A newly-allocated NULL-terminated array of strings or NULL if it
- * fails to allocate the array.
- *
- * This functin splits @p str into a maximum of @p max_tokens pieces,
- * using the given delimiter @p delim. @p delim is not included in any
- * of the resulting strings, unless @p max_tokens is reached. If
- * @p max_tokens is less than @c 1, the string is splitted completely. If
- * @p max_tokens is reached, the last string in the returned string
- * array contains the remainder of string. The returned value is a
- * newly allocated NULL-terminated array of strings or NULL if it fails to
- * allocate the array. To free it, free the first element of the array and the
- * array itself.
- */
 EAPI char **
 eina_str_split(const char *str, const char *delim, int max_tokens)
 {
    return eina_str_split_full_helper(str, delim, max_tokens, NULL);
 }
 
-/**
- * @brief Join two strings of known length.
- *
- * @param dst The buffer to store the result.
- * @param size Size (in byte) of the buffer.
- * @param sep The separator character to use.
- * @param a First string to use, before @p sep.
- * @param a_len length of @p a.
- * @param b Second string to use, after @p sep.
- * @param b_len length of @p b.
- * @return The number of characters printed.
- *
- * This function joins the strings @p a and @p b (in that order) and
- * separate them with @p sep. The result is stored in the buffer
- * @p dst and at most @p size - 1 characters will be written and the
- * string is NULL-terminated. @p a_len is the length of @p a (not
- * including '\\0') and @p b_len is the length of @p b (not including
- * '\\0'). This function returns the number of characters printed (not
- * including the trailing '\\0' used to end output to strings). Just
- * like snprintf(), it will not write more than @p size bytes, thus a
- * returned value of @p size or more means that the output was
- * truncated.
- *
- * @see eina_str_join()
- * @see eina_str_join_static()
- */
 EAPI size_t
 eina_str_join_len(char *dst,
                   size_t size,
@@ -471,14 +451,6 @@ eina_str_join_len(char *dst,
    return ret;
 }
 
-/**
- * @brief Use iconv to convert a text string from one encoding to another
- *
- * @param enc_from encoding to convert from
- * @param enc_to   encoding to convert to
- * @param text     text to convert
- *
- */
 #ifdef HAVE_ICONV
 EAPI char *
 eina_str_convert(const char *enc_from, const char *enc_to, const char *text)
@@ -566,13 +538,6 @@ eina_str_convert(const char *enc_from __UNUSED__,
 }
 #endif
 
-/**
- * @brief Put a \ before and Space( ), \ or ' in a string.
- *
- * @param str the string to escape
- *
- * A newly allocated string is returned.
- */
 EAPI char *
 eina_str_escape(const char *str)
 {
@@ -597,13 +562,6 @@ eina_str_escape(const char *str)
    return s2;
 }
 
-/**
- * @brief Lowercase all the characters in range [A-Z] in the given string.
- *
- * @param str the string to lowercase
- *
- * This modifies the original string, changing all characters in [A-Z] to lowercase.
- */
 EAPI void
 eina_str_tolower(char **str)
 {
@@ -612,16 +570,9 @@ eina_str_tolower(char **str)
       return;
 
    for (p = *str; (*p); p++)
-      *p = tolower(*p);
+      *p = tolower((unsigned char )(*p));
 }
 
-/**
- * @brief Uppercase all the characters in range [a-z] in the given string.
- *
- * @param str the string to uppercase
- *
- * This modifies the original string, changing all characters in [a-z] to uppercase.
- */
 EAPI void
 eina_str_toupper(char **str)
 {
@@ -630,10 +581,5 @@ eina_str_toupper(char **str)
       return;
 
    for (p = *str; (*p); p++)
-      *p = toupper(*p);
+      *p = toupper((unsigned char)(*p));
 }
-
-
-/**
- * @}
- */