cvs: php4 /ext/pcre php_pcre.c php_pcre.h /ext/standard array.c

From: Date: Wed, 28 Jun 2000 20:07:26 +0000
Subject: cvs: php4 /ext/pcre php_pcre.c php_pcre.h /ext/standard array.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-534@lists.php.net to get a copy of this message
andrei Wed Jun 28 13:07:26 2000 EDT Modified files: /php4/ext/pcre php_pcre.c php_pcre.h /php4/ext/standard array.c Log: @- Added an optional parameter to preg_replace() that can be used to @ specify how many replacements to make. (Andrei) Index: php4/ext/pcre/php_pcre.c diff -u php4/ext/pcre/php_pcre.c:1.60 php4/ext/pcre/php_pcre.c:1.61 --- php4/ext/pcre/php_pcre.c:1.60 Mon Jun 12 12:55:57 2000 +++ php4/ext/pcre/php_pcre.c Wed Jun 28 13:07:25 2000 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_pcre.c,v 1.60 2000/06/12 19:55:57 andrei Exp $ */ +/* $Id: php_pcre.c,v 1.61 2000/06/28 20:07:25 andrei Exp $ */ /* TODO: @@ -596,7 +596,7 @@ char *php_pcre_replace(char *regex, int regex_len, char *subject, int subject_len, char *replace, int replace_len, - int *result_len) + int *result_len, int limit) { pcre *re = NULL; /* Compiled regular expression */ pcre_extra *extra = NULL; /* Holds results of studying */ @@ -660,7 +660,7 @@ piece = subject + start_offset; - if (count > 0) { + if (count > 0 && (limit == -1 || limit > 0)) { /* Set the match location in subject */ match = subject + offsets[0]; @@ -750,8 +750,11 @@ advance to the next character. */ g_notempty = (offsets[1] == offsets[0])? PCRE_NOTEMPTY | PCRE_ANCHORED : 0; - /* Advance to the next piece */ + /* Advance to the next piece. */ start_offset = offsets[1]; + + if (limit != -1) + limit--; } efree(offsets); @@ -760,8 +763,7 @@ } -static char *php_replace_in_subject(zval *regex, zval *replace, zval **subject, - int *result_len) +static char *php_replace_in_subject(zval *regex, zval *replace, zval **subject, int *result_len, int limit) { zval **regex_entry, **replace_entry = NULL; @@ -824,7 +826,8 @@ subject_len, replace_value, replace_len, - result_len)) != NULL) { + result_len, + limit)) != NULL) { efree(subject_value); subject_value = result; subject_len = *result_len; @@ -841,33 +844,42 @@ (*subject)->value.str.len, replace->value.str.val, replace->value.str.len, - result_len); + result_len, + limit); return result; } } -/* {{{ proto string preg_replace(string|array regex, string|array replace, string|array subject) - Perform Perl-style regular expression replacement */ +/* {{{ proto string preg_replace(string|array regex, string|array replace, string|array subject [, int limit]) + Perform Perl-style regular expression replacement. */ PHP_FUNCTION(preg_replace) { zval **regex, **replace, **subject, + **limit, **subject_entry; char *result; int result_len; + int limit_val = -1; char *string_key; ulong num_key; /* Get function parameters and do error-checking. */ - if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &regex, &replace, &subject) == FAILURE) { + if (ZEND_NUM_ARGS() < 3 || ZEND_NUM_ARGS() > 4 || + zend_get_parameters_ex(ZEND_NUM_ARGS(), &regex, &replace, &subject, &limit) == FAILURE) { WRONG_PARAM_COUNT; } SEPARATE_ZVAL(regex); SEPARATE_ZVAL(replace); SEPARATE_ZVAL(subject); + + if (ZEND_NUM_ARGS() > 3) { + convert_to_long_ex(limit); + limit_val = Z_LVAL_PP(limit); + } /* Make sure we're dealing with strings and do the replacement */ if ((*regex)->type != IS_ARRAY) { @@ -885,7 +897,7 @@ and add the result to the return_value array. */ while (zend_hash_get_current_data((*subject)->value.ht, (void **)&subject_entry) == SUCCESS) { - if ((result = php_replace_in_subject(*regex, *replace, subject_entry, &result_len)) != NULL) { + if ((result = php_replace_in_subject(*regex, *replace, subject_entry, &result_len, limit_val)) != NULL) { /* Add to return array */ switch(zend_hash_get_current_key((*subject)->value.ht, &string_key, &num_key)) { @@ -904,7 +916,7 @@ } } else { /* if subject is not an array */ - if ((result = php_replace_in_subject(*regex, *replace, subject, &result_len)) != NULL) { + if ((result = php_replace_in_subject(*regex, *replace, subject, &result_len, limit_val)) != NULL) { RETVAL_STRINGL(result, result_len, 0); } } Index: php4/ext/pcre/php_pcre.h diff -u php4/ext/pcre/php_pcre.h:1.20 php4/ext/pcre/php_pcre.h:1.21 --- php4/ext/pcre/php_pcre.h:1.20 Thu Jun 1 09:34:50 2000 +++ php4/ext/pcre/php_pcre.h Wed Jun 28 13:07:25 2000 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_pcre.h,v 1.20 2000/06/01 16:34:50 andrei Exp $ */ +/* $Id: php_pcre.h,v 1.21 2000/06/28 20:07:25 andrei Exp $ */ #ifndef _PHP_PCRE_H #define _PHP_PCRE_H @@ -43,7 +43,7 @@ char *php_pcre_replace(char *regex, int regex_len, char *subject, int subject_len, char *replace, int replace_len, - int *result_len); + int *result_len, int limit); extern zend_module_entry pcre_module_entry; #define pcre_module_ptr &pcre_module_entry Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.58 php4/ext/standard/array.c:1.59 --- php4/ext/standard/array.c:1.58 Sun Jun 25 06:21:37 2000 +++ php4/ext/standard/array.c Wed Jun 28 13:07:26 2000 @@ -1373,7 +1373,7 @@ if (p->nKeyLength) zend_hash_update(out_hash, p->arKey, p->nKeyLength, &entry, sizeof(zval *), NULL); else - zend_hash_next_index_insert(out_hash, &entry, sizeof(zval *), NULL); + zend_hash_index_update(out_hash, p->h, &entry, sizeof(zval *), NULL); } /* If hash for removed entries exists, go until offset+length @@ -1385,7 +1385,7 @@ if (p->nKeyLength) zend_hash_update(*removed, p->arKey, p->nKeyLength, &entry, sizeof(zval *), NULL); else - zend_hash_next_index_insert(*removed, &entry, sizeof(zval *), NULL); + zend_hash_index_update(*removed, p->h, &entry, sizeof(zval *), NULL); } } else /* otherwise just skip those entries */ for( ; pos<offset+length && p; pos++, p=p->pListNext);

« previous php.cvs (#534) next »