CVS update: php3/functions

From: Date: Fri, 25 Dec 1998 20:09:47 +0000
Subject: CVS update: php3/functions
Groups: php.dev 
Request: Send a blank email to php-dev+get-3039@lists.php.net to get a copy of this message
Date: Friday December 25, 1998 @ 15:09 Author: sas Update of /repository/php3/functions In directory asf:/u/temp/cvs-serv5567/functions Modified Files: reg.c reg.h Log Message: speed up regexes by introducing a simple cache which is effective in the simplest case - using always the same regex Index: php3/functions/reg.c diff -c php3/functions/reg.c:1.88 php3/functions/reg.c:1.89 *** php3/functions/reg.c:1.88 Wed Nov 18 16:23:11 1998 --- php3/functions/reg.c Fri Dec 25 15:09:47 1998 *************** *** 28,34 **** | Jaakko Hyvätti <jaakko@hyvatti.iki.fi> | +----------------------------------------------------------------------+ */ ! /* $Id: reg.c,v 1.88 1998/11/18 21:23:11 ssb Exp $ */ #ifdef THREAD_SAFE #include "tls.h" #endif --- 28,34 ---- | Jaakko Hyvätti <jaakko@hyvatti.iki.fi> | +----------------------------------------------------------------------+ */ ! /* $Id: reg.c,v 1.89 1998/12/25 20:09:47 sas Exp $ */ #ifdef THREAD_SAFE #include "tls.h" #endif *************** *** 36,41 **** --- 36,42 ---- #include "php.h" #include "internal_functions.h" #include "php3_string.h" + #include "php3_list.h" #include "reg.h" unsigned char third_argument_force_ref[] = { 3, BYREF_NONE, BYREF_NONE, BYREF_FORCE }; *************** *** 58,64 **** --- 59,103 ---- /* This is the maximum number of (..) constructs we'll generate from a call to ereg() or eregi() with the optional third argument. */ #define NS 10 + #ifndef THREAD_SAFE + reg_cache *rc = NULL; + + #define php3_regcomp(a,b,c) \ + _php3_regcomp(INTERNAL_FUNCTION_PARAM_PASSTHRU,a,b,c) + + static int _php3_regcomp(INTERNAL_FUNCTION_PARAMETERS, + regex_t *preg, const char *pattern, int cflags) + { + int r = 0; + + if(!rc) { + rc = emalloc(sizeof *rc); + php3_list_insert((void *) rc, LE_REGEX); + rc->pattern = NULL; + rc->cflags = -1; + } + if(!rc->pattern || cflags != rc->cflags || + strcmp(pattern, rc->pattern)) { + r = regcomp(preg, pattern, cflags); + if(r == 0) { + if(rc->cflags != -1) { + regfree(&rc->preg); + efree(rc->pattern); + } + memcpy(&rc->preg, preg, sizeof(*preg)); + rc->pattern = estrdup(pattern); + rc->cflags = cflags; + } + } else { + memcpy(preg, &rc->preg, sizeof(*preg)); + } + return r; + } + #else + #define php3_regcomp(a,b,c) regcomp(a,b,c) + #endif + /* * _php3_reg_eprint - convert error number to name */ *************** *** 141,154 **** /* compile the regular expression from the supplied regex */ if (regex->type == IS_STRING) { ! err = regcomp(&re, regex->value.str.val, REG_EXTENDED | copts); } else { /* we convert numbers to integers and treat them as a string */ if (regex->type == IS_DOUBLE) convert_to_long(regex); /* get rid of decimal places */ convert_to_string(regex); /* don't bother doing an extended regex with just a number */ ! err = regcomp(&re, regex->value.str.val, copts); } if (err) { --- 180,193 ---- /* compile the regular expression from the supplied regex */ if (regex->type == IS_STRING) { ! err = php3_regcomp(&re, regex->value.str.val, REG_EXTENDED | copts); } else { /* we convert numbers to integers and treat them as a string */ if (regex->type == IS_DOUBLE) convert_to_long(regex); /* get rid of decimal places */ convert_to_string(regex); /* don't bother doing an extended regex with just a number */ ! err = php3_regcomp(&re, regex->value.str.val, copts); } if (err) { *************** *** 164,170 **** err = regexec(&re, string, (size_t) NS, subs, 0); if (err && err != REG_NOMATCH) { _php3_reg_eprint(err, &re); - regfree(&re); RETURN_FALSE; } match_len = 1; --- 203,208 ---- *************** *** 206,212 **** match_len = 1; RETVAL_LONG(match_len); } - regfree(&re); } /* {{{ proto int ereg(string pattern, string string [, array registers]) --- 244,249 ---- *************** *** 226,232 **** /* }}} */ /* this is the meat and potatoes of regex replacement! */ ! char *_php3_regreplace(const char *pattern, const char *replace, const char *string, int icase, int extended) { regex_t re; regmatch_t subs[NS]; --- 263,270 ---- /* }}} */ /* this is the meat and potatoes of regex replacement! */ ! char *__php3_regreplace(INTERNAL_FUNCTION_PARAMETERS, const char *pattern, ! const char *replace, const char *string, int icase, int extended) { regex_t re; regmatch_t subs[NS]; *************** *** 247,253 **** copts = REG_ICASE; if (extended) copts |= REG_EXTENDED; ! err = regcomp(&re, pattern, copts); if (err) { _php3_reg_eprint(err, &re); return ((char *) -1); --- 285,291 ---- copts = REG_ICASE; if (extended) copts |= REG_EXTENDED; ! err = php3_regcomp(&re, pattern, copts); if (err) { _php3_reg_eprint(err, &re); return ((char *) -1); *************** *** 259,265 **** buf = emalloc(buf_len * sizeof(char)); if (!buf) { php3_error(E_WARNING, "Unable to allocate memory in _php3_regreplace"); - regfree(&re); return ((char *) -1); } --- 297,302 ---- *************** *** 271,277 **** if (err && err != REG_NOMATCH) { _php3_reg_eprint(err, &re); - regfree(&re); return ((char *) -1); } if (!err) { --- 308,313 ---- *************** *** 359,367 **** } } - /* don't want to leak memory .. */ - regfree(&re); - /* whew. */ return (buf); } --- 395,400 ---- *************** *** 475,488 **** strp = str->value.str.val; endp = str->value.str.val + strlen(str->value.str.val); ! err = regcomp(&re, spliton->value.str.val, REG_EXTENDED); if (err) { php3_error(E_WARNING, "unexpected regex error (%d)", err); RETURN_FALSE; } if (array_init(return_value) == FAILURE) { - regfree(&re); RETURN_FALSE; } --- 508,520 ---- strp = str->value.str.val; endp = str->value.str.val + strlen(str->value.str.val); ! err = php3_regcomp(&re, spliton->value.str.val, REG_EXTENDED); if (err) { php3_error(E_WARNING, "unexpected regex error (%d)", err); RETURN_FALSE; } if (array_init(return_value) == FAILURE) { RETURN_FALSE; } *************** *** 495,501 **** strp+=subs[0].rm_eo; } else if (subs[0].rm_so==0 && subs[0].rm_eo==0) { /* No more matches */ - regfree(&re); php3_error(E_WARNING, "bad regular expression for split()"); _php3_hash_destroy(return_value->value.ht); efree(return_value->value.ht); --- 527,532 ---- *************** *** 521,527 **** /* see if we encountered an error */ if (err && err != REG_NOMATCH) { php3_error(E_WARNING, "unexpected regex error (%d)", err); - regfree(&re); _php3_hash_destroy(return_value->value.ht); efree(return_value->value.ht); RETURN_FALSE; --- 552,557 ---- *************** *** 532,538 **** add_next_index_stringl(return_value, strp, size, 1); - regfree(&re); return; } --- 562,567 ---- Index: php3/functions/reg.h diff -c php3/functions/reg.h:1.12 php3/functions/reg.h:1.13 *** php3/functions/reg.h:1.12 Wed Apr 29 09:24:27 1998 --- php3/functions/reg.h Fri Dec 25 15:09:47 1998 *************** *** 28,34 **** */ ! /* $Id: reg.h,v 1.12 1998/04/29 13:24:27 ssb Exp $ */ #ifndef _REG_H #define _REG_H --- 28,34 ---- */ ! /* $Id: reg.h,v 1.13 1998/12/25 20:09:47 sas Exp $ */ #ifndef _REG_H #define _REG_H *************** *** 36,42 **** extern php3_module_entry regexp_module_entry; #define regexp_module_ptr &regexp_module_entry ! extern char *_php3_regreplace(const char *pattern, const char *replace, const char *string, int icase, int extended); extern void php3_ereg(INTERNAL_FUNCTION_PARAMETERS); extern void php3_eregi(INTERNAL_FUNCTION_PARAMETERS); --- 36,45 ---- extern php3_module_entry regexp_module_entry; #define regexp_module_ptr &regexp_module_entry ! #define _php3_regreplace(a,b,c,d,e) \ ! __php3_regreplace(INTERNAL_FUNCTION_PARAM_PASSTHRU,a,b,c,d,e) ! ! extern char *__php3_regreplace(INTERNAL_FUNCTION_PARAMETERS,const char *pattern, const char *replace, const char *string, int icase, int extended); extern void php3_ereg(INTERNAL_FUNCTION_PARAMETERS); extern void php3_eregi(INTERNAL_FUNCTION_PARAMETERS); -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#3039) next »