Hi Andrei,
do you have anything against that change?

Cheers,
Andrey

Andrei Zmievski wrote:
Uh, would be nice to check with me before making this patch..

On Sat, 12 Mar 2005, Andrey Hristov wrote:

andrey          Sat Mar 12 07:03:51 2005 EDT

Modified files: /php-src NEWS /php-src/ext/pcre php_pcre.c php_pcre.h /php-src/main SAPI.c /php-src/win32 sendmail.c Log:
FR 32275 - fifth parameter to preg_replace() to count number of replaces
made.
#it would be nice if someone of the doc team documents it. thanks!
http://cvs.php.net/diff.php/php-src/NEWS?r1=1.1850&r2=1.1851&ty=u
Index: php-src/NEWS
diff -u php-src/NEWS:1.1850 php-src/NEWS:1.1851
--- php-src/NEWS:1.1850 Tue Mar 8 01:38:58 2005
+++ php-src/NEWS Sat Mar 12 07:03:48 2005
@@ -8,6 +8,8 @@
. ext/mnogosearch (Jani, Derick)
. ext/w32api (Jani, Derick)
. ext/yp (Jani, Derick)
+- Added additional fifth parameter count to preg_replace_callback() and
+ preg_replace() to count the number of replacements made. FR #32275. (Andrey)
- Changed stream_filter_(ap|pre)pend() to return resource. (Sara)
- Changed mysqli_exception and sqlite_exception to use RuntimeException as base if SPL extension is present. (Georg, Marcus)
http://cvs.php.net/diff.php/php-src/ext/pcre/php_pcre.c?r1=1.164&r2=1.165&ty=u
Index: php-src/ext/pcre/php_pcre.c
diff -u php-src/ext/pcre/php_pcre.c:1.164 php-src/ext/pcre/php_pcre.c:1.165
--- php-src/ext/pcre/php_pcre.c:1.164 Fri Dec 24 14:45:54 2004
+++ php-src/ext/pcre/php_pcre.c Sat Mar 12 07:03:49 2005
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/


-/* $Id: php_pcre.c,v 1.164 2004/12/24 19:45:54 tony2001 Exp $ */
+/* $Id: php_pcre.c,v 1.165 2005/03/12 12:03:49 andrey Exp $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -807,7 +807,7 @@
PHPAPI char *php_pcre_replace(char *regex,   int regex_len,
                                                          char *subject, int 
subject_len,
                                                          zval *replace_val, 
int is_callable_replace,
-                                                         int *result_len, int 
limit TSRMLS_DC)
+                                                         int *result_len, int 
limit, int *replace_count TSRMLS_DC)
{
        pcre                    *re = NULL;                     /* Compiled 
regular expression */
        pcre_extra              *extra = NULL;          /* Holds results of 
studying */
@@ -887,9 +887,12 @@
                piece = subject + start_offset;

if (count > 0 && (limit == -1 || limit > 0)) {
+ if (replace_count) {
+ ++*replace_count;
+ }
/* Set the match location in subject */
match = subject + offsets[0];
-
+ new_len = *result_len + offsets[0] - start_offset; /* part before the match */

/* If evaluating, do it and add the return string's length */
@@ -1019,7 +1022,7 @@


/* {{{ php_replace_in_subject
*/
-static char *php_replace_in_subject(zval *regex, zval *replace, zval **subject, int *result_len, int limit, zend_bool is_callable_replace TSRMLS_DC)
+static char *php_replace_in_subject(zval *regex, zval *replace, zval **subject, int *result_len, int limit, zend_bool is_callable_replace, int *replace_count TSRMLS_DC)
{
zval **regex_entry,
**replace_entry = NULL,
@@ -1076,7 +1079,8 @@
replace_value,
is_callable_replace,
result_len,
- limit TSRMLS_CC)) != NULL) {
+ limit,
+ replace_count TSRMLS_CC)) != NULL) {
efree(subject_value);
subject_value = result;
subject_len = *result_len;
@@ -1094,7 +1098,8 @@
replace,
is_callable_replace,
result_len,
- limit TSRMLS_CC);
+ limit,
+ replace_count TSRMLS_CC);
return result;
}
}
@@ -1108,17 +1113,20 @@
**replace,
**subject,
**limit,
- **subject_entry;
+ **subject_entry,
+ **zcount;
char *result;
int result_len;
int limit_val = -1;
char *string_key;
ulong num_key;
char *callback_name = NULL;
+ int replace_count=0;
+ int *replace_count_ptr=NULL;
/* Get function parameters and do error-checking. */
- if (ZEND_NUM_ARGS() < 3 || ZEND_NUM_ARGS() > 4 ||
- zend_get_parameters_ex(ZEND_NUM_ARGS(), &regex, &replace, &subject, &limit) == FAILURE) {
+ if (ZEND_NUM_ARGS() < 3 || ZEND_NUM_ARGS() > 5 ||
+ zend_get_parameters_ex(ZEND_NUM_ARGS(), &regex, &replace, &subject, &limit, &zcount) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (!is_callable_replace && Z_TYPE_PP(replace) == IS_ARRAY && Z_TYPE_PP(regex) != IS_ARRAY) {
@@ -1147,6 +1155,9 @@
convert_to_long_ex(limit);
limit_val = Z_LVAL_PP(limit);
}
+ if (ZEND_NUM_ARGS() > 4) {
+ replace_count_ptr =& replace_count;
+ }

if (Z_TYPE_PP(regex) != IS_ARRAY)
convert_to_string_ex(regex);
@@ -1160,7 +1171,7 @@
and add the result to the return_value array. */
while (zend_hash_get_current_data(Z_ARRVAL_PP(subject), (void **)&subject_entry) == SUCCESS) {
SEPARATE_ZVAL(subject_entry);
- if ((result = php_replace_in_subject(*regex, *replace, subject_entry, &result_len, limit_val, is_callable_replace TSRMLS_CC)) != NULL) {
+ if ((result = php_replace_in_subject(*regex, *replace, subject_entry, &result_len, limit_val, is_callable_replace, replace_count_ptr TSRMLS_CC)) != NULL) {
/* Add to return array */
switch(zend_hash_get_current_key(Z_ARRVAL_PP(subject), &string_key, &num_key, 0))
{
@@ -1176,16 +1187,20 @@

zend_hash_move_forward(Z_ARRVAL_PP(subject));
}
- }
- else { /* if subject is not an array */
- if ((result = php_replace_in_subject(*regex, *replace, subject, &result_len, limit_val, is_callable_replace TSRMLS_CC)) != NULL) {
+ } else { /* if subject is not an array */
+ if ((result = php_replace_in_subject(*regex, *replace, subject, &result_len, limit_val, is_callable_replace, replace_count_ptr TSRMLS_CC)) != NULL) {
RETVAL_STRINGL(result, result_len, 0);
}
- }
+ }
+ if (replace_count_ptr) {
+ zval_dtor(*zcount);
+ ZVAL_LONG(*zcount, replace_count);
+ }
+
}
/* }}} */


-/* {{{ proto string preg_replace(mixed regex, mixed replace, mixed subject [, 
int limit])
+/* {{{ proto string preg_replace(mixed regex, mixed replace, mixed subject [, 
int limit [, count]])
   Perform Perl-style regular expression replacement. */
PHP_FUNCTION(preg_replace)
{
@@ -1193,7 +1208,7 @@
}
/* }}} */

-/* {{{ proto string preg_replace_callback(mixed regex, mixed callback, mixed subject [, int limit])
+/* {{{ proto string preg_replace_callback(mixed regex, mixed callback, mixed subject [, int limit [, count]])
Perform Perl-style regular expression replacement using replacement callback. */
PHP_FUNCTION(preg_replace_callback)
{
http://cvs.php.net/diff.php/php-src/ext/pcre/php_pcre.h?r1=1.39&r2=1.40&ty=u
Index: php-src/ext/pcre/php_pcre.h
diff -u php-src/ext/pcre/php_pcre.h:1.39 php-src/ext/pcre/php_pcre.h:1.40
--- php-src/ext/pcre/php_pcre.h:1.39 Sat Jan 31 22:02:47 2004
+++ php-src/ext/pcre/php_pcre.h Sat Mar 12 07:03:50 2005
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_pcre.h,v 1.39 2004/02/01 03:02:47 moriyoshi Exp $ */
+/* $Id: php_pcre.h,v 1.40 2005/03/12 12:03:50 andrey Exp $ */


#ifndef PHP_PCRE_H
#define PHP_PCRE_H
@@ -41,7 +41,7 @@
PHP_FUNCTION(preg_quote);
PHP_FUNCTION(preg_grep);

-PHPAPI char *php_pcre_replace(char *regex,   int regex_len, char *subject, int 
subject_len, zval *replace_val, int is_callable_replace, int *result_len, int 
limit TSRMLS_DC);
+PHPAPI char *php_pcre_replace(char *regex,   int regex_len, char *subject, int 
subject_len, zval *replace_val, int is_callable_replace, int *result_len, int 
limit, int *replace_count TSRMLS_DC);
PHPAPI pcre* pcre_get_compiled_regex(char *regex, pcre_extra **extra, int 
*options TSRMLS_DC);
PHPAPI pcre* pcre_get_compiled_regex_ex(char *regex, pcre_extra **extra, int 
*preg_options, int *coptions TSRMLS_DC);

http://cvs.php.net/diff.php/php-src/main/SAPI.c?r1=1.197&r2=1.198&ty=u
Index: php-src/main/SAPI.c
diff -u php-src/main/SAPI.c:1.197 php-src/main/SAPI.c:1.198
--- php-src/main/SAPI.c:1.197   Sat Mar  5 11:41:13 2005
+++ php-src/main/SAPI.c Sat Mar 12 07:03:50 2005
@@ -18,7 +18,7 @@
   +----------------------------------------------------------------------+
*/

-/* $Id: SAPI.c,v 1.197 2005/03/05 16:41:13 moriyoshi Exp $ */
+/* $Id: SAPI.c,v 1.198 2005/03/12 12:03:50 andrey Exp $ */

#include <ctype.h>
#include <sys/stat.h>
@@ -637,7 +637,7 @@
result = php_pcre_replace("/realm=\"(.*?)\"/i", 16,
ptr, ptr_len,
repl_temp,
- 0, &result_len, -1 TSRMLS_CC);
+ 0, &result_len, -1, NULL TSRMLS_CC);
if(result_len==ptr_len) {
efree(result);
sprintf(Z_STRVAL_P(repl_temp), "realm=\\1-%ld\\2", myuid);
@@ -645,7 +645,7 @@
result = php_pcre_replace("/realm=([^\\s]+)(.*)/i", 21, ptr, ptr_len,
repl_temp,
- 0, &result_len, -1 TSRMLS_CC);
+ 0, &result_len, -1, NULL TSRMLS_CC);
if(result_len==ptr_len) {
char *lower_temp = estrdup(ptr);
char conv_temp[32];
http://cvs.php.net/diff.php/php-src/win32/sendmail.c?r1=1.62&r2=1.63&ty=u
Index: php-src/win32/sendmail.c
diff -u php-src/win32/sendmail.c:1.62 php-src/win32/sendmail.c:1.63
--- php-src/win32/sendmail.c:1.62 Fri Feb 25 00:57:41 2005
+++ php-src/win32/sendmail.c Sat Mar 12 07:03:50 2005
@@ -17,7 +17,7 @@
*
*/


-/* $Id: sendmail.c,v 1.62 2005/02/25 05:57:41 hyanantha Exp $ */
+/* $Id: sendmail.c,v 1.63 2005/03/12 12:03:50 andrey Exp $ */

#include "php.h"                              /*php specific */
#include <stdio.h>
@@ -182,7 +182,8 @@
                                                          replace,
                                                          0,
                                                          &result_len,
-                                                         -1 TSRMLS_CC);
+                                                         -1,
+                                                         NULL TSRMLS_CC);
        if (NULL == result) {
                FREE_ZVAL(replace);
                return NULL;
@@ -195,7 +196,8 @@
                                                           replace,
                                                           0,
                                                           &result_len,
-                                                          -1 TSRMLS_CC);
+                                                          -1,
+                                                          NULL TSRMLS_CC);
        efree(result);
        FREE_ZVAL(replace);
        return result2;



--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




- Andrei


-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to