moriyoshi               Mon Oct  7 13:59:49 2002 EDT

  Added files:                 
    /php4/ext/mbstring  php_mbregex.h 

  Modified files:              
    /php4/ext/mbstring  mbstring.c mbstring.h php_mbregex.c 
  Log:
  Lots of clean-up for upcoming mbstring merging event.
  Added mb_regex_set_options().
  The Options parameters of various mbregex functions are now deprecated.
  @- Added mb_regex_set_options(). The Options parameters of 
  @  various mbregex functions are now deprecated. (Moriyoshi).
  
  
Index: php4/ext/mbstring/mbstring.c
diff -u php4/ext/mbstring/mbstring.c:1.99 php4/ext/mbstring/mbstring.c:1.100
--- php4/ext/mbstring/mbstring.c:1.99   Wed Sep 25 20:53:47 2002
+++ php4/ext/mbstring/mbstring.c        Mon Oct  7 13:59:48 2002
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: mbstring.c,v 1.99 2002/09/26 00:53:47 wez Exp $ */
+/* $Id: mbstring.c,v 1.100 2002/10/07 17:59:48 moriyoshi Exp $ */
 
 /*
  * PHP4 Multibyte String module "mbstring" (currently only for Japanese)
@@ -219,6 +219,7 @@
        PHP_FALIAS(i18n_ja_jp_hantozen,         mb_convert_kana,                NULL)
 #if HAVE_MBREGEX
        PHP_FE(mb_regex_encoding,       NULL)
+       PHP_FE(mb_regex_set_options,    NULL)
        PHP_FE(mb_ereg,                 (unsigned char *)third_argument_force_ref)
        PHP_FE(mb_eregi,                        (unsigned char 
*)third_argument_force_ref)
        PHP_FE(mb_ereg_replace,                 NULL)
@@ -807,7 +808,9 @@
                        p++;
                }
        }
-
+#if HAVE_MBREGEX
+       MBSTRG(regex_default_options) = 0;
+#endif
        return SUCCESS;
 }
 
Index: php4/ext/mbstring/mbstring.h
diff -u php4/ext/mbstring/mbstring.h:1.30 php4/ext/mbstring/mbstring.h:1.31
--- php4/ext/mbstring/mbstring.h:1.30   Wed Sep 25 20:53:47 2002
+++ php4/ext/mbstring/mbstring.h        Mon Oct  7 13:59:48 2002
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: mbstring.h,v 1.30 2002/09/26 00:53:47 wez Exp $ */
+/* $Id: mbstring.h,v 1.31 2002/10/07 17:59:48 moriyoshi Exp $ */
 
 /*
  * PHP4 Multibyte String module "mbstring" (currently only for Japanese)
@@ -64,7 +64,7 @@
 PHPAPI int php_mb_check_encoding_list(const char *encoding_list TSRMLS_DC);
 
 #if HAVE_MBREGEX
-#include "mbregex.h"
+#include "php_mbregex.h"
 #endif
 
 extern zend_module_entry mbstring_module_entry;
@@ -106,27 +106,6 @@
 PHP_FUNCTION(mb_decode_numericentity);
 PHP_FUNCTION(mb_send_mail);
 PHP_FUNCTION(mb_get_info);
-#if HAVE_MBREGEX
-PHP_FUNCTION(mb_regex_encoding);
-PHP_FUNCTION(mb_ereg);
-PHP_FUNCTION(mb_eregi);
-PHP_FUNCTION(mb_ereg_replace);
-PHP_FUNCTION(mb_eregi_replace);
-PHP_FUNCTION(mb_split);
-PHP_FUNCTION(mb_ereg_match);
-PHP_FUNCTION(mb_ereg_search);
-PHP_FUNCTION(mb_ereg_search_pos);
-PHP_FUNCTION(mb_ereg_search_regs);
-PHP_FUNCTION(mb_ereg_search_init);
-PHP_FUNCTION(mb_ereg_search_getregs);
-PHP_FUNCTION(mb_ereg_search_getpos);
-PHP_FUNCTION(mb_ereg_search_setpos);
-#endif
-
-#if HAVE_MBREGEX
-#define PHP_MBREGEX_MAXCACHE 50
-int php_mbregex_name2mbctype(const char *pname);
-#endif
 
 char *mbstr_strrchr(const char *s, char c TSRMLS_DC);
 int mbstr_is_mb_leadbyte(const char *s TSRMLS_DC);
@@ -161,15 +140,8 @@
        long func_overload;
        int encoding_translation;
        mbfl_buffer_converter *outconv;
-#if HAVE_MBREGEX
-       int default_mbctype;
-       int current_mbctype;
-       HashTable ht_rc;
-       zval **search_str;
-       zval *search_str_val;
-       unsigned int search_pos;
-       mb_regex_t *search_re;
-       struct mbre_registers *search_regs;
+#if HAVE_MBREGEX && defined(PHP_MBREGEX_GLOBALS)
+       PHP_MBREGEX_GLOBALS     
 #endif
 ZEND_END_MODULE_GLOBALS(mbstring)
 
Index: php4/ext/mbstring/php_mbregex.c
diff -u php4/ext/mbstring/php_mbregex.c:1.11 php4/ext/mbstring/php_mbregex.c:1.12
--- php4/ext/mbstring/php_mbregex.c:1.11        Mon Oct  7 12:46:42 2002
+++ php4/ext/mbstring/php_mbregex.c     Mon Oct  7 13:59:48 2002
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_mbregex.c,v 1.11 2002/10/07 16:46:42 moriyoshi Exp $ */
+/* $Id: php_mbregex.c,v 1.12 2002/10/07 17:59:48 moriyoshi Exp $ */
 
 
 #ifdef HAVE_CONFIG_H
@@ -25,6 +25,7 @@
 
 #include "php.h"
 #include "php_ini.h"
+#include "php_mbregex.h"
 #include "mbregex.h"
 #include "mbstring.h"
 
@@ -43,7 +44,7 @@
 };
 
 static void
-php_mbregex_strbuf_init(struct strbuf *pd)
+_php_mb_regex_strbuf_init(struct strbuf *pd)
 {
        if (pd) {
                pd->buffer = (char*)0;
@@ -54,7 +55,7 @@
 }
 
 static int
-php_mbregex_strbuf_ncat(struct strbuf *pd, const unsigned char *psrc, int len)
+_php_mb_regex_strbuf_ncat(struct strbuf *pd, const unsigned char *psrc, int len)
 {
        if (pd == NULL || psrc == NULL) {
                return -1;
@@ -83,8 +84,7 @@
 /*
  * encoding name resolver
  */
-int
-php_mbregex_name2mbctype(const char *pname)
+static int _php_mb_regex_name2mbctype(const char *pname)
 {
        int mbctype;
 
@@ -170,43 +170,43 @@
 }
 
 static void
-php_mbregex_init_option(const char *parg, int narg, int *option, int *eval) 
+_php_mb_regex_init_options(const char *parg, int narg, int *option, int *eval) 
 {
        int n;
        char c;
+       int optm = 0; 
 
-       if (parg) {
+       if (parg != NULL) {
                n = 0;
                while(n < narg) {
                        c = parg[n++];
-                       if (option) {
-                               switch (c) {
+                       switch (c) {
                                case 'i':
-                                       *option |= MBRE_OPTION_IGNORECASE;
+                                       optm |= MBRE_OPTION_IGNORECASE;
                                        break;
                                case 'x':
-                                       *option |= MBRE_OPTION_EXTENDED;
+                                       optm |= MBRE_OPTION_EXTENDED;
                                        break;
                                case 'm':
-                                       *option |= MBRE_OPTION_MULTILINE;
+                                       optm |= MBRE_OPTION_MULTILINE;
                                        break;
                                case 's':
-                                       *option |= MBRE_OPTION_SINGLELINE;
+                                       optm |= MBRE_OPTION_SINGLELINE;
                                        break;
                                case 'p':
-                                       *option |= MBRE_OPTION_POSIXLINE;
+                                       optm |= MBRE_OPTION_POSIXLINE;
                                        break;
                                case 'l':
-                                       *option |= MBRE_OPTION_LONGEST;
+                                       optm |= MBRE_OPTION_LONGEST;
+                                       break;
+                               case 'e':
+                                       if (eval != NULL) *eval = 1; 
                                        break;
                                default:
                                        break;
-                               }
-                       }
-                       if (eval && (c == 'e')) {
-                               *eval = 1;
                        }
                }
+               if (option != NULL) *option|=optm; 
        }
 }
 
@@ -232,7 +232,7 @@
        } else if (ZEND_NUM_ARGS() == 1 &&
                   zend_get_parameters_ex(1, &arg1) != FAILURE) {
                convert_to_string_ex(arg1);
-               mbctype = php_mbregex_name2mbctype(Z_STRVAL_PP(arg1));
+               mbctype = _php_mb_regex_name2mbctype(Z_STRVAL_PP(arg1));
                if (mbctype < 0) {
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding 
\"%s\"", Z_STRVAL_PP(arg1));
                        RETVAL_FALSE;
@@ -247,88 +247,72 @@
 /* }}} */
 
 
-/* regex match */
+/* {{{ _php_mb_regex_ereg_exec */
 static void
-php_mbereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
+_php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
 {
-       zval **arg_pattern, **arg_string, **array = NULL;
+       zval *arg_pattern, *array;
+       char *string;
+       int string_len;
        mb_regex_t re;
        struct mbre_registers regs = {0, 0, 0, 0};
-       int i, err, match_len, string_len, option, beg, end;
+       int i, err, match_len, option, beg, end;
        char *str;
 
-       switch(ZEND_NUM_ARGS()) {
-       case 2:
-               if (zend_get_parameters_ex(2, &arg_pattern, &arg_string) == FAILURE) {
-                       WRONG_PARAM_COUNT;
-               }
-               break;
-
-       case 3:
-               if (zend_get_parameters_ex(3, &arg_pattern, &arg_string, &array) == 
FAILURE) {
-                       WRONG_PARAM_COUNT;
-               }
-               break;
+       array = NULL;
 
-       default:
-               WRONG_PARAM_COUNT;
-               break;
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs|z", &arg_pattern, 
+&string, &string_len, &array) == FAILURE) {
+               RETURN_FALSE;
        }
 
-       option = 0;
+       option = MBSTRG(regex_default_options);
        if (icase) {
                option |= MBRE_OPTION_IGNORECASE;
        }
 
        /* compile the regular expression from the supplied regex */
-       if (Z_TYPE_PP(arg_pattern) == IS_STRING) {
-               option |= MBRE_OPTION_EXTENDED;
-       } else {
+       if (Z_TYPE_P(arg_pattern) != IS_STRING) {
                /* we convert numbers to integers and treat them as a string */
-               if (Z_TYPE_PP(arg_pattern) == IS_DOUBLE) {
-                       convert_to_long_ex(arg_pattern);        /* get rid of decimal 
places */
+               if (Z_TYPE_P(arg_pattern) == IS_DOUBLE) {
+                       convert_to_long_ex(&arg_pattern);       /* get rid of decimal 
+places */
                }
-               convert_to_string_ex(arg_pattern);
+               convert_to_string_ex(&arg_pattern);
                /* don't bother doing an extended regex with just a number */
        }
        err = php_mbregex_compile_pattern(
             &re,
-            Z_STRVAL_PP(arg_pattern),
-            Z_STRLEN_PP(arg_pattern),
+            Z_STRVAL_P(arg_pattern),
+            Z_STRLEN_P(arg_pattern),
             option, MBSTRG(current_mbctype) TSRMLS_CC);
        if (err) {
                RETURN_FALSE;
        }
 
        /* actually execute the regular expression */
-       convert_to_string_ex(arg_string);
        err = mbre_search(
             &re,
-            Z_STRVAL_PP(arg_string),
-            Z_STRLEN_PP(arg_string),
-             0, Z_STRLEN_PP(arg_string),
+            string,
+            string_len,
+             0, string_len,
             &regs);
        if (err < 0) {
                mbre_free_registers(&regs);
                RETURN_FALSE;
        }
-       if (regs.beg[0] == regs.end[0]) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty regular 
expression");
-       }
+
        match_len = 1;
-       str = Z_STRVAL_PP(arg_string);
+       str = string;
        if (array) {
                match_len = regs.end[0] - regs.beg[0];
-               string_len = Z_STRLEN_PP(arg_string);
-               zval_dtor(*array);      /* start with clean array */
-               array_init(*array);
+               zval_dtor(array);       /* start with clean array */
+               array_init(array);
                for (i = 0; i < regs.num_regs; i++) {
                        beg = regs.beg[i];
                        end = regs.end[i];
                        if (beg >= 0 && beg < end && end <= string_len) {
-                               add_index_stringl(*array, i, &str[beg], end - beg, 1);
+                               add_index_stringl(array, i, &str[beg], end - beg, 1);
                        } else {
-                               add_index_bool(*array, i, 0);
+                               add_index_bool(array, i, 0);
                        }
                }
        }
@@ -344,7 +328,7 @@
    Regular expression match for multibyte string */
 PHP_FUNCTION(mb_ereg)
 {
-       php_mbereg_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
+       _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
 }
 /* }}} */
 
@@ -352,69 +336,79 @@
    Case-insensitive regular expression match for multibyte string */
 PHP_FUNCTION(mb_eregi)
 {
-       php_mbereg_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
+       _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
 }
 /* }}} */
 
 
 
-/* regex replacement */
+/* {{{ _php_mb_regex_ereg_replace_exec */
 static void
-php_mbereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, int option)
+_php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, int option)
 {
-       zval **arg_pattern, **arg_replace, **arg_string, **arg_option;
-       char *p, *string, *replace;
+       zval *arg_pattern_zval;
+
+       char *arg_pattern;
+       int arg_pattern_len;
+
+       char *replace;
+       int replace_len;
+
+       char *string;
+       int string_len;
+
+       char *p;
        mb_regex_t re;
        struct mbre_registers regs = {0, 0, 0, 0};
        struct strbuf outdev, evaldev, *pdevice;
-       int i, n, err, pos, replace_len, string_len, eval;
+       int i, n, err, pos, eval;
        char *description = NULL;
-       zval retval;
+       char pat_buf[2];
 
        eval = 0;
-       switch(ZEND_NUM_ARGS()) {
-       case 3:
-               if (zend_get_parameters_ex(3, &arg_pattern, &arg_replace, &arg_string) 
== FAILURE) {
-                       WRONG_PARAM_COUNT;
+       {
+               char *option_str = NULL;
+               int option_str_len = 0;
+
+               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zss|s",
+                                                                       
+&arg_pattern_zval,
+                                                                       &replace, 
+&replace_len,
+                                                                       &string, 
+&string_len,
+                                                                       &option_str, 
+&option_str_len) == FAILURE) {
+                       RETURN_FALSE;
                }
-               break;
 
-       case 4:
-               if (zend_get_parameters_ex(4, &arg_pattern, &arg_replace, &arg_string, 
&arg_option) == FAILURE) {
-                       WRONG_PARAM_COUNT;
+               if (option_str != NULL) {
+                       _php_mb_regex_init_options(option_str, option_str_len, 
+&option, &eval);
+               } else {
+                       option |= MBSTRG(regex_default_options);
                }
-               convert_to_string_ex(arg_option);
-               option = 0;
-               php_mbregex_init_option(Z_STRVAL_PP(arg_option), 
Z_STRLEN_PP(arg_option), &option, &eval);
-               break;
-
-       default:
-               WRONG_PARAM_COUNT;
-               break;
        }
+       if (Z_TYPE_P(arg_pattern_zval) == IS_STRING) {
+               arg_pattern = Z_STRVAL_P(arg_pattern_zval);
+               arg_pattern_len = Z_STRLEN_P(arg_pattern_zval);
+       } else {
+               /* FIXME: this code is not multibyte aware! */
+               convert_to_long_ex(&arg_pattern_zval);
+               pat_buf[0] = (char)Z_LVAL_P(arg_pattern_zval);  
+               pat_buf[1] = '\0';
 
-       convert_to_string_ex(arg_pattern);
+               arg_pattern = pat_buf;
+               arg_pattern_len = 1;    
+       }
        /* create regex pattern buffer */
        err = php_mbregex_compile_pattern(
            &re,
-           Z_STRVAL_PP(arg_pattern),
-           Z_STRLEN_PP(arg_pattern),
+           arg_pattern,
+           arg_pattern_len,
            option, MBSTRG(current_mbctype) TSRMLS_CC);
        if (err) {
                RETURN_FALSE;
        }
 
-       convert_to_string_ex(arg_replace);
-       replace = Z_STRVAL_PP(arg_replace);
-       replace_len = Z_STRLEN_PP(arg_replace);
-
-       convert_to_string_ex(arg_string);
-       string = Z_STRVAL_PP(arg_string);
-       string_len = Z_STRLEN_PP(arg_string);
-
        /* initialize string buffer (auto reallocate buffer) */
-       php_mbregex_strbuf_init(&outdev);
-       php_mbregex_strbuf_init(&evaldev);
+       _php_mb_regex_strbuf_init(&outdev);
+       _php_mb_regex_strbuf_init(&evaldev);
        outdev.allocsz = (string_len >> 2) + 8;
 
        if (eval) {
@@ -440,8 +434,9 @@
                                break;
                        }
                        /* copy the part of the string before the match */
-                       php_mbregex_strbuf_ncat(&outdev, &string[pos], regs.beg[0] - 
pos);
+                       _php_mb_regex_strbuf_ncat(&outdev, &string[pos], regs.beg[0] - 
+pos);
                        /* copy replacement and backrefs */
+                       /* FIXME: this code (\\digit replacement) is not mbyte aware! 
+*/ 
                        i = 0;
                        p = replace;
                        while (i < replace_len) {
@@ -451,27 +446,28 @@
                                }
                                if (n >= 0 && n < regs.num_regs) {
                                        if (regs.beg[n] >= 0 && regs.beg[n] < 
regs.end[n] && regs.end[n] <= string_len) {
-                                               php_mbregex_strbuf_ncat(pdevice, 
&string[regs.beg[n]], regs.end[n] - regs.beg[n]);
+                                               _php_mb_regex_strbuf_ncat(pdevice, 
+&string[regs.beg[n]], regs.end[n] - regs.beg[n]);
                                        }
                                        p += 2;
                                        i += 2;
                                } else {
-                                       php_mbregex_strbuf_ncat(pdevice, p, 1);
+                                       _php_mb_regex_strbuf_ncat(pdevice, p, 1);
                                        p++;
                                        i++;
                                }
                        }
                        if (eval) {
+                               zval v;
                                /* null terminate buffer */
-                               php_mbregex_strbuf_ncat(&evaldev, "\0", 1);
+                               _php_mb_regex_strbuf_ncat(&evaldev, "\0", 1);
                                /* do eval */
-                               zend_eval_string(evaldev.buffer, &retval, description 
TSRMLS_CC);
+                               zend_eval_string(evaldev.buffer, &v, description 
+TSRMLS_CC);
                                /* result of eval */
-                               convert_to_string(&retval);
-                               php_mbregex_strbuf_ncat(&outdev, retval.value.str.val, 
retval.value.str.len);
+                               convert_to_string(&v);
+                               _php_mb_regex_strbuf_ncat(&outdev, Z_STRVAL(v), 
+Z_STRLEN(v));
                                /* Clean up */
                                evaldev.pos = 0;
-                               zval_dtor(&retval);
+                               zval_dtor(&v);
                        }
                        n = regs.end[0];
                        if (pos < n) {
@@ -481,7 +477,7 @@
                        }
                } else { /* nomatch */
                        /* stick that last bit of string on our output */
-                       php_mbregex_strbuf_ncat(&outdev, &string[pos], string_len - 
pos);
+                       _php_mb_regex_strbuf_ncat(&outdev, &string[pos], string_len - 
+pos);
                }
        }
 
@@ -493,7 +489,7 @@
                efree((void*)evaldev.buffer);
        }
        n = outdev.pos;
-       php_mbregex_strbuf_ncat(&outdev, "\0", 1);
+       _php_mb_regex_strbuf_ncat(&outdev, "\0", 1);
        if (err <= -2) {
                if (outdev.buffer) {
                        efree((void*)outdev.buffer);
@@ -503,12 +499,12 @@
                RETVAL_STRINGL(outdev.buffer, n, 0);
        }
 }
-
+/* }}} */
 /* {{{ proto string mb_ereg_replace(string pattern, string replacement, string string 
[, string option])
    Replace regular expression for multibyte string */
 PHP_FUNCTION(mb_ereg_replace)
 {
-       php_mbereg_replace_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 
MBRE_OPTION_EXTENDED);
+       _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
 }
 /* }}} */
 
@@ -516,7 +512,7 @@
    Case insensitive replace regular expression for multibyte string */
 PHP_FUNCTION(mb_eregi_replace)
 {
-       php_mbereg_replace_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, MBRE_OPTION_EXTENDED 
| MBRE_OPTION_IGNORECASE);
+       _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 
+MBRE_OPTION_IGNORECASE);
 }
 /* }}} */
 
@@ -525,54 +521,35 @@
    split multibyte string into array by regular expression */
 PHP_FUNCTION(mb_split)
 {
-       zval **arg_pat, **arg_str, **arg_count = NULL;
+       zval *arg_pat;
        mb_regex_t re;
        struct mbre_registers regs = {0, 0, 0, 0};
        char *string;
        int n, err, count, string_len, pos;
 
        count = -1;
-       switch (ZEND_NUM_ARGS()) {
-       case 2:
-               if (zend_get_parameters_ex(2, &arg_pat, &arg_str) == FAILURE) {
-                       WRONG_PARAM_COUNT;
-               }
-               break;
 
-       case 3:
-               if (zend_get_parameters_ex(3, &arg_pat, &arg_str, &arg_count) == 
FAILURE) {
-                       WRONG_PARAM_COUNT;
-               }
-               convert_to_long_ex(arg_count);
-               count = Z_LVAL_PP(arg_count);
-               break;
-
-       default:
-               WRONG_PARAM_COUNT;
-               break;
-       }
+       if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs|l", &arg_pat,
+                               &string, &string_len, &count) == FAILURE) {
+               RETURN_FALSE;
+       } 
 
-       if (count == 0) count = 1; 
+       if (count == 0) count = 1;
 
        if (array_init(return_value) == FAILURE) {
                RETURN_FALSE;
        }
 
-       convert_to_string_ex(arg_pat);
-       convert_to_string_ex(arg_str);
-
        /* create regex pattern buffer */
        err = php_mbregex_compile_pattern(
             &re,
-            Z_STRVAL_PP(arg_pat),
-            Z_STRLEN_PP(arg_pat),
-            MBRE_OPTION_EXTENDED, MBSTRG(current_mbctype) TSRMLS_CC);
+            Z_STRVAL_P(arg_pat),
+            Z_STRLEN_P(arg_pat),
+            MBSTRG(regex_default_options), MBSTRG(current_mbctype) TSRMLS_CC);
        if (err) {
                RETURN_FALSE;
        }
 
-       string = Z_STRVAL_PP(arg_str);
-       string_len = Z_STRLEN_PP(arg_str);
        pos = 0;
        err = 0;
        /* churn through str, generating array entries as we go */
@@ -583,11 +560,10 @@
                        break;
                }
 
-               n = regs.beg[0];
+
                /* add it to the array */
-               if (n < string_len && n >= pos) {
-                       n -= pos;
-                       add_next_index_stringl(return_value, &string[pos], n, 1);
+               if ( regs.beg[0] < string_len && regs.beg[0] >= pos) {
+                       add_next_index_stringl(return_value, &string[pos], 
+regs.beg[0]-pos, 1);
                } else {
                        err = -2;
                        break;
@@ -596,8 +572,6 @@
                n = regs.end[0];
                if (pos < n) {
                        pos = n;
-               } else {
-                       pos++;
                }
                if (count < 0) {
                        count = 0;
@@ -628,44 +602,45 @@
    Regular expression match for multibyte string */
 PHP_FUNCTION(mb_ereg_match)
 {
-       zval **arg_pattern, **arg_str, **arg_option;
+       zval *arg_pattern;
+
+       char *string;
+       int string_len;
+
        mb_regex_t re;
        int option, err;
 
-       option = MBRE_OPTION_EXTENDED;
-       switch (ZEND_NUM_ARGS()) {
-       case 2:
-               if (zend_get_parameters_ex(2, &arg_pattern, &arg_str) == FAILURE) {
-                       WRONG_PARAM_COUNT;
+       option = 0;     
+       {
+               char *option_str = NULL;
+               int option_str_len = 0;
+
+               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs|s",
+                                         &arg_pattern, &string, &string_len,
+                                         &option_str, &option_str_len)==FAILURE) {
+                       RETURN_FALSE;
                }
-               break;
-       case 3:
-               if (zend_get_parameters_ex(3, &arg_pattern, &arg_str, &arg_option) == 
FAILURE) {
-                       WRONG_PARAM_COUNT;
+
+               if (option_str != NULL) {
+                       _php_mb_regex_init_options(option_str, option_str_len, 
+&option, NULL);
+               } else {
+                       option |= MBSTRG(regex_default_options);
                }
-               convert_to_string_ex(arg_option);
-               option = 0;
-               php_mbregex_init_option(Z_STRVAL_PP(arg_option), 
Z_STRLEN_PP(arg_option), &option, NULL);
-               break;
-       default:
-               WRONG_PARAM_COUNT;
-               break;
        }
+       convert_to_string_ex(&arg_pattern);
 
-       /* create regex pattern buffer */
-       convert_to_string_ex(arg_pattern);
        err = php_mbregex_compile_pattern(
            &re,
-           Z_STRVAL_PP(arg_pattern),
-           Z_STRLEN_PP(arg_pattern),
+           Z_STRVAL_P(arg_pattern),
+           Z_STRLEN_P(arg_pattern),
            option, MBSTRG(current_mbctype) TSRMLS_CC);
+
        if (err) {
                RETURN_FALSE;
        }
 
        /* match */
-       convert_to_string_ex(arg_str);
-       err = mbre_match(&re, Z_STRVAL_PP(arg_str), Z_STRLEN_PP(arg_str), 0, NULL);
+       err = mbre_match(&re, string, string_len, 0, NULL);
        if (err >= 0) {
                RETVAL_TRUE;
        } else {
@@ -677,13 +652,13 @@
 
 /* regex search */
 static void
-php_mbereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
+_php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
 {
-       zval **arg_pattern, **arg_option;
+       zval **arg_pattern, **arg_options;
        int n, i, err, pos, len, beg, end, option;
        unsigned char *str;
 
-       option = MBRE_OPTION_EXTENDED;
+       option = MBSTRG(regex_default_options);
        switch (ZEND_NUM_ARGS()) {
        case 0:
                break;
@@ -693,12 +668,12 @@
                }
                break;
        case 2:
-               if (zend_get_parameters_ex(2, &arg_pattern, &arg_option) == FAILURE) {
+               if (zend_get_parameters_ex(2, &arg_pattern, &arg_options) == FAILURE) {
                        WRONG_PARAM_COUNT;
                }
-               convert_to_string_ex(arg_option);
+               convert_to_string_ex(arg_options);
                option = 0;
-               php_mbregex_init_option(Z_STRVAL_PP(arg_option), 
Z_STRLEN_PP(arg_option), &option, NULL);
+               _php_mb_regex_init_options(Z_STRVAL_PP(arg_options), 
+Z_STRLEN_PP(arg_options), &option, NULL);
                break;
        default:
                WRONG_PARAM_COUNT;
@@ -808,7 +783,7 @@
    Regular expression search for multibyte string */
 PHP_FUNCTION(mb_ereg_search)
 {
-       php_mbereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
+       _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
 }
 /* }}} */
 
@@ -817,7 +792,7 @@
    Regular expression search for multibyte string */
 PHP_FUNCTION(mb_ereg_search_pos)
 {
-       php_mbereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
+       _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
 }
 /* }}} */
 
@@ -826,7 +801,7 @@
    Regular expression search for multibyte string */
 PHP_FUNCTION(mb_ereg_search_regs)
 {
-       php_mbereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
+       _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
 }
 /* }}} */
 
@@ -835,10 +810,10 @@
    Initialize string and regular expression for search. */
 PHP_FUNCTION(mb_ereg_search_init)
 {
-       zval **arg_str, **arg_pattern, **arg_option;
+       zval **arg_str, **arg_pattern, **arg_options;
        int err, option;
 
-       option = MBRE_OPTION_EXTENDED;
+       option = MBSTRG(regex_default_options);
        switch (ZEND_NUM_ARGS()) {
        case 1:
                if (zend_get_parameters_ex(1, &arg_str) == FAILURE) {
@@ -851,12 +826,12 @@
                }
                break;
        case 3:
-               if (zend_get_parameters_ex(3, &arg_str, &arg_pattern, &arg_option) == 
FAILURE) {
+               if (zend_get_parameters_ex(3, &arg_str, &arg_pattern, &arg_options) == 
+FAILURE) {
                        WRONG_PARAM_COUNT;
                }
-               convert_to_string_ex(arg_option);
+               convert_to_string_ex(arg_options);
                option = 0;
-               php_mbregex_init_option(Z_STRVAL_PP(arg_option), 
Z_STRLEN_PP(arg_option), &option, NULL);
+               _php_mb_regex_init_options(Z_STRVAL_PP(arg_options), 
+Z_STRLEN_PP(arg_options), &option, NULL);
                break;
        default:
                WRONG_PARAM_COUNT;
@@ -978,6 +953,39 @@
                MBSTRG(search_pos) = n;
                RETVAL_TRUE;
        }
+}
+/* }}} */
+
+/* {{{ php_mb_regex_set_options */
+PHPAPI int php_mb_regex_set_options( int options ) 
+{
+       int prev_opt = MBSTRG(regex_default_options);
+       MBSTRG(regex_default_options) = options;
+       return prev_opt;
+}
+/* }}} */
+
+/* {{{ php_mb_regex_set_options_by_string */
+PHPAPI int php_mb_regex_set_options_by_string( const char *opt_str, int len )
+{
+       int new_opt;
+       _php_mb_regex_init_options( opt_str, len, &new_opt, NULL);
+       return php_mb_regex_set_options( new_opt );
+}
+/* }}} */
+
+/* {{{ proto bool mb_regex_set_options([string encoding])
+   Set the default options for mbregex functions */
+PHP_FUNCTION(mb_regex_set_options)
+{
+       char *string;
+       int string_len;
+       if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s",
+                                   &string, &string_len ) == FAILURE ) {
+               RETURN_FALSE;
+       }
+       php_mb_regex_set_options_by_string( (const char*)string, string_len );
+       RETURN_TRUE;
 }
 /* }}} */
 

Index: php4/ext/mbstring/php_mbregex.h
+++ php4/ext/mbstring/php_mbregex.h
/*
   +----------------------------------------------------------------------+
   | PHP Version 4                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2002 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | [EMAIL PROTECTED] so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: Moriyoshi Koizumi <[EMAIL PROTECTED]>                        |
   +----------------------------------------------------------------------+
 */

/* $Id: php_mbregex.h,v 1.1 2002/10/07 17:59:49 moriyoshi Exp $ */
 
#ifndef _PHP_MBREGEX_H
#define _PHP_MBREGEX_H

#if HAVE_MBREGEX

#include "php.h"
#include "zend.h"
#include "mbregex.h"
 
/* {{{ PHP_MBREGEX_GLOBALS */
#define PHP_MBREGEX_GLOBALS \
        int default_mbctype; \
        int current_mbctype; \
        HashTable ht_rc; \
        zval **search_str; \
        zval *search_str_val; \
        unsigned int search_pos; \
        mb_regex_t *search_re; \
        struct mbre_registers *search_regs; \
        int regex_default_options;
/* }}} */

#define PHP_MBREGEX_MAXCACHE 50

PHPAPI int php_mb_regex_name2mbctype(const char *pname);
PHPAPI int php_mb_regex_set_options(int options);
PHPAPI int php_mb_regex_set_options_by_string(const char *optstr, int len);

PHP_FUNCTION(mb_regex_encoding);
PHP_FUNCTION(mb_ereg);
PHP_FUNCTION(mb_eregi);
PHP_FUNCTION(mb_ereg_replace);
PHP_FUNCTION(mb_eregi_replace);
PHP_FUNCTION(mb_split);
PHP_FUNCTION(mb_ereg_match);
PHP_FUNCTION(mb_ereg_search);
PHP_FUNCTION(mb_ereg_search_pos);
PHP_FUNCTION(mb_ereg_search_regs);
PHP_FUNCTION(mb_ereg_search_init);
PHP_FUNCTION(mb_ereg_search_getregs);
PHP_FUNCTION(mb_ereg_search_getpos);
PHP_FUNCTION(mb_ereg_search_setpos);
PHP_FUNCTION(mb_regex_set_options);

#endif /* HAVE_MBREGEX */

#endif /* _PHP_MBREGEX_H */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */


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

Reply via email to