Enlightenment CVS committal Author : pfritz Project : e17 Module : libs/ecore
Dir : e17/libs/ecore/src/lib/ecore Modified Files: ecore_str.c Ecore_Str.h Log Message: speed up ecore_str_split(); thanks to mej =================================================================== RCS file: /cvs/e/e17/libs/ecore/src/lib/ecore/ecore_str.c,v retrieving revision 1.6 retrieving revision 1.7 diff -u -3 -r1.6 -r1.7 --- ecore_str.c 13 Mar 2007 01:17:33 -0000 1.6 +++ ecore_str.c 13 Mar 2007 06:46:14 -0000 1.7 @@ -136,89 +136,45 @@ * delimiter. If max_tokens is reached, the final string in the returned * string array contains the remainder of string. * - * @param string A string to split. - * @param delimiter A string which specifies the places at which to split the + * @param str A string to split. + * @param delim A string which specifies the places at which to split the * string. The delimiter is not included in any of the * resulting strings, unless max_tokens is reached. * @param max_tokens The maximum number of strings to split string into. * If this is less than 1, the string is split completely. * @return A newly-allocated NULL-terminated array of strings. - * Use ecore_str_vector_free() to free it. + * To free it: free the first element of the array + * and the array itself. */ -char** -ecore_str_split(const char *string, const char *delimiter, int max_tokens) +char ** +ecore_str_split(const char *str, const char *delim, int max_tokens) { - char **str_array = NULL; - char *s; - size_t n = 0; - int max = max_tokens; - const char *remainder; - size_t delimiter_len; - - CHECK_PARAM_POINTER_RETURN("string", string, NULL); - CHECK_PARAM_POINTER_RETURN("delimiter", delimiter, NULL); - - /* on the first run we just count the number of the strings we'll finally - * have */ - remainder = string; - s = strstr(remainder, delimiter); - if (s) - { - delimiter_len = strlen(delimiter); - while (--max_tokens && s) - { - remainder = s + delimiter_len; - s = strstr(remainder, delimiter); - n++; - } - } - if (*string != '\0') n++; - - str_array = malloc(sizeof(char *)*(n + 1)); - str_array[n] = NULL; - - /* reset to the initial values */ - n = 0; - max_tokens = max; - remainder = string; - s = strstr(remainder, delimiter); - if (s) - { - while (--max_tokens && s) - { - size_t len; - char *new_string; + char *s, *sep, **str_array; + size_t len, dlen; + int i; - len = s - remainder; - new_string = malloc(sizeof(char)*(len + 1)); - memcpy(new_string, remainder, len); - new_string[len] = 0; - str_array[n++] = new_string; + CHECK_PARAM_POINTER_RETURN("str", str, NULL); + CHECK_PARAM_POINTER_RETURN("delim", delim, NULL); - remainder = s + delimiter_len; - s = strstr(remainder, delimiter); - } - } - if (*string != '\0') str_array[n] = strdup(remainder); + if (*delim == '\0') + return NULL; + max_tokens = ((max_tokens <= 0) ? (INT_MAX) : (max_tokens - 1)); + len = strlen(str); + dlen = strlen(delim); + s = strdup(str); + str_array = malloc(sizeof(char *) * (len + 1)); + for (i = 0; (i < max_tokens) && (sep = strstr(s, delim)); i++) + { + str_array[i] = s; + s = sep + dlen; + *sep = 0; + } + + str_array[i++] = s; + str_array = realloc(str_array, sizeof(char *) * (i + 1)); + str_array[i] = NULL; + return str_array; -} - -/** - * Free an array of strings and the array itself - * - * @param str_array An NULL-terminated array of strings to free. - */ -void -ecore_str_vector_free(char **str_array) -{ - CHECK_PARAM_POINTER("str_array", str_array); - int i; - - for(i=0; str_array[i] != NULL; i++) - { - FREE(str_array[i]); - } - FREE(str_array); } =================================================================== RCS file: /cvs/e/e17/libs/ecore/src/lib/ecore/Ecore_Str.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -3 -r1.5 -r1.6 --- Ecore_Str.h 13 Mar 2007 01:17:33 -0000 1.5 +++ Ecore_Str.h 13 Mar 2007 06:46:14 -0000 1.6 @@ -48,7 +48,6 @@ EAPI int ecore_str_has_suffix(const char *str, const char *suffix); EAPI char **ecore_str_split(const char *string, const char *delimiter, int max_tokens); -EAPI void ecore_str_vector_free(char **str_array); #ifdef __cplusplus } ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ enlightenment-cvs mailing list enlightenment-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs