cvs: php4 /ext/standard basic_functions.c basic_functions.h string.c

From: Date: Fri, 03 Aug 2001 08:49:19 +0000
Subject: cvs: php4 /ext/standard basic_functions.c basic_functions.h string.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-6503@lists.php.net to get a copy of this message
sas Fri Aug 3 04:49:19 2001 EDT Modified files: /php4/ext/standard basic_functions.c basic_functions.h string.c Log: Replace our strtok implementation with a binary-safe one which code does not cause headaches when reading it. Also happens to be a bit quicker. Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.373 php4/ext/standard/basic_functions.c:1.374 --- php4/ext/standard/basic_functions.c:1.373 Wed Aug 1 12:55:23 2001 +++ php4/ext/standard/basic_functions.c Fri Aug 3 04:49:19 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.373 2001/08/01 16:55:23 fmk Exp $ */ +/* $Id: basic_functions.c,v 1.374 2001/08/03 08:49:19 sas Exp $ */ #include "php.h" #include "php_main.h" @@ -847,6 +847,7 @@ PHP_RINIT_FUNCTION(basic) { + memset(BG(strtok_table), 0, 256); BG(strtok_string) = NULL; BG(locale_string) = NULL; BG(user_compare_func_name) = NULL; Index: php4/ext/standard/basic_functions.h diff -u php4/ext/standard/basic_functions.h:1.84 php4/ext/standard/basic_functions.h:1.85 --- php4/ext/standard/basic_functions.h:1.84 Tue Jul 31 03:09:44 2001 +++ php4/ext/standard/basic_functions.h Fri Aug 3 04:49:19 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.h,v 1.84 2001/07/31 07:09:44 zeev Exp $ */ +/* $Id: basic_functions.h,v 1.85 2001/08/03 08:49:19 sas Exp $ */ #ifndef BASIC_FUNCTIONS_H #define BASIC_FUNCTIONS_H @@ -143,8 +143,9 @@ HashTable putenv_ht; char *strtok_string; char *locale_string; - char *strtok_pos1; - char *strtok_pos2; + char *strtok_last; + char strtok_table[256]; + ulong strtok_len; char str_ebuf[40]; zval **array_walk_func_name; zval **user_compare_func_name; Index: php4/ext/standard/string.c diff -u php4/ext/standard/string.c:1.211 php4/ext/standard/string.c:1.212 --- php4/ext/standard/string.c:1.211 Mon Jul 30 04:24:36 2001 +++ php4/ext/standard/string.c Fri Aug 3 04:49:19 2001 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: string.c,v 1.211 2001/07/30 08:24:36 zeev Exp $ */ +/* $Id: string.c,v 1.212 2001/08/03 08:49:19 sas Exp $ */ /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */ @@ -803,60 +803,82 @@ } /* }}} */ +#define STRTOK_TABLE(p) BG(strtok_table)[(unsigned char) *p] + /* {{{ proto string strtok([string str,] string token) Tokenize a string */ PHP_FUNCTION(strtok) { - zval **str, **tok; - char *token = NULL, *tokp=NULL; - char *first = NULL; - int argc; - - argc = ZEND_NUM_ARGS(); - - if ((argc == 1 && zend_get_parameters_ex(1, &tok) == FAILURE) || - (argc == 2 && zend_get_parameters_ex(2, &str, &tok) == FAILURE) || - argc < 1 || argc > 2) { + zval **args[2]; + zval **tok, **str; + char *token; + char *token_end; + char *p; + char *pe; + + if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 || + zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) WRONG_PARAM_COUNT; - } - convert_to_string_ex(tok); - tokp = token = (*tok)->value.str.val; - - if (argc == 2) { + + switch (ZEND_NUM_ARGS()) { + case 1: + tok = args[0]; + break; + case 2: + str = args[0]; + tok = args[1]; convert_to_string_ex(str); STR_FREE(BG(strtok_string)); - BG(strtok_string) = estrndup((*str)->value.str.val,(*str)->value.str.len); - BG(strtok_pos1) = BG(strtok_string); - BG(strtok_pos2) = NULL; - } - if (BG(strtok_pos1) && *BG(strtok_pos1)) { - for ( /* NOP */ ; token && *token; token++) { - BG(strtok_pos2) = strchr(BG(strtok_pos1), (int) *token); - if (!first || (BG(strtok_pos2) && BG(strtok_pos2) < first)) { - first = BG(strtok_pos2); - } - } /* NB: token is unusable now */ + BG(strtok_last) = BG(strtok_string) = estrndup(Z_STRVAL_PP(str), Z_STRLEN_PP(str)); + BG(strtok_len) = Z_STRLEN_PP(str); + break; + } + + p = BG(strtok_last); /* Where we start to search */ + pe = BG(strtok_string) + BG(strtok_len); - BG(strtok_pos2) = first; - if (BG(strtok_pos2)) { - *BG(strtok_pos2) = '\0'; - } - RETVAL_STRING(BG(strtok_pos1),1); -#if 0 - /* skip 'token' white space for next call to strtok */ - while (BG(strtok_pos2) && - strchr(tokp, *(BG(strtok_pos2)+1))) { - BG(strtok_pos2)++; + if (!p || p >= pe) + RETURN_FALSE; + + convert_to_string_ex(tok); + + token = Z_STRVAL_PP(tok); + token_end = token + Z_STRLEN_PP(tok); + + while (token < token_end) + STRTOK_TABLE(token++) = 1; + + /* Skip leading delimiters */ + while (STRTOK_TABLE(p)) + if (++p >= pe) { + /* no other chars left */ + BG(strtok_last) = NULL; + RETVAL_FALSE; + goto restore; } -#endif - if (BG(strtok_pos2)) - BG(strtok_pos1) = BG(strtok_pos2) + 1; - else - BG(strtok_pos1) = NULL; + + /* We know at this place that *p is no delimiter, so skip it */ + while (++p < pe) + if (STRTOK_TABLE(p)) + goto return_token; + + if (p - BG(strtok_last)) { +return_token: + RETVAL_STRINGL(BG(strtok_last), p - BG(strtok_last), 1); + BG(strtok_last) = p + 1; } else { RETVAL_FALSE; + BG(strtok_last) = NULL; } + + /* Restore table -- usually faster then memset'ing the table + on every invocation */ +restore: + token = Z_STRVAL_PP(tok); + + while (token < token_end) + STRTOK_TABLE(token++) = 0; } /* }}} */

« previous php.cvs (#6503) next »