]> granicus.if.org Git - php/commitdiff
-Modified PCRE library slightly to do what needs to be done
authorAndrey Hristov <andrey@php.net>
Fri, 28 May 1999 20:54:52 +0000 (20:54 +0000)
committerAndrey Hristov <andrey@php.net>
Fri, 28 May 1999 20:54:52 +0000 (20:54 +0000)
-Updated preg_replace() code to handle stuff properly
-Removed ability to specify external PCRE library source to link against
(since our bundled version is patched now)

ext/pcre/config.h.stub
ext/pcre/config.m4
ext/pcre/config0.m4
ext/pcre/pcre.c
ext/pcre/pcrelib/pcre.c
ext/pcre/pcrelib/pcre.h
ext/pcre/php_pcre.h

index f504f057dd77a6dd2b86648ddd47f4f7657c2ea1..baeac73b7530b445e68b58d61caed620849b9a79 100644 (file)
@@ -1,6 +1,3 @@
 /* define if you want to use the PCRE extension */
 #define        HAVE_PCRE 0
 
-/* define if you want to use the bundled PCRE library */
-#define HAVE_BUNDLED_PCRE 0
-
index 6c6806ed4101f32c05017c1deb661e0e2b8179bb..2f25c8680320c2f21f494d002e66b00889ae9999 100644 (file)
@@ -4,40 +4,22 @@ dnl config.m4 for extension pcre
 dnl By default we'll compile and link against the bundled PCRE library
 dnl if DIR is supplied, we'll use that for linking
 
-AC_MSG_CHECKING(whether to use PCRE library)
+AC_MSG_CHECKING(whether to include PCRE support)
 AC_ARG_WITH(pcre-regex,
-[  --with-pcre-regex[=DIR] Enable Perl Compatible Regular Expressions support],[
-  case "$withval" in
-    no)
-       AC_MSG_RESULT(no) ;;
-    yes)
-        EXTRA_LIBS="-Lext/pcre/pcrelib -lpcre $EXTRA_LIBS"
-        PCRE_SUBDIR="pcrelib"
-       AC_DEFINE(HAVE_PCRE, 1)
-        AC_DEFINE(HAVE_BUNDLED_PCRE, 1)
-       AC_MSG_RESULT(yes)
-        PHP_EXTENSION(pcre) ;;
-    *)
-        test -f $withval/pcre.h && PCRE_INCLUDE="-I$withval"
-        test -f $withval/libpcre.a && PCRE_LIB="-L$withval -lpcre"
-       
-        if test -n "$PCRE_INCLUDE" && test -n "$PCRE_LIB" ; then
-          INCLUDES="$PCRE_INCLUDE $INCLUDES"
-          EXTRA_LIBS="$PCRE_LIB $EXTRA_LIBS"
-          AC_DEFINE(HAVE_PCRE, 1)
-          AC_DEFINE(HAVE_BUNDLED_PCRE, 0)
-          PCRE_SUBDIR=
-          AC_MSG_RESULT(yes)
-          PHP_EXTENSION(pcre)
-        else
-          AC_MSG_ERROR(Unable to find pcre.h and libpcre.a in $withval)
-        fi ;;
-  esac
+[  --with-pcre-regex[=DIR] Include Perl Compatible Regular Expressions support],[
+  if test "$withval" = "yes"; then
+    EXTRA_LIBS="-Lext/pcre/pcrelib -lpcre $EXTRA_LIBS"
+    PCRE_SUBDIR="pcrelib"
+    AC_DEFINE(HAVE_PCRE, 1)
+    AC_MSG_RESULT(yes)
+    PHP_EXTENSION(pcre)
+  else
+    AC_MSG_RESULT(no)
+  fi
 ],[
   EXTRA_LIBS="-Lext/pcre/pcrelib -lpcre $EXTRA_LIBS"
   PCRE_SUBDIR="pcrelib"
   AC_DEFINE(HAVE_PCRE, 1)
-  AC_DEFINE(HAVE_BUNDLED_PCRE, 1)
   AC_MSG_RESULT(yes)
   PHP_EXTENSION(pcre)
 ]) 
index 6c6806ed4101f32c05017c1deb661e0e2b8179bb..2f25c8680320c2f21f494d002e66b00889ae9999 100644 (file)
@@ -4,40 +4,22 @@ dnl config.m4 for extension pcre
 dnl By default we'll compile and link against the bundled PCRE library
 dnl if DIR is supplied, we'll use that for linking
 
-AC_MSG_CHECKING(whether to use PCRE library)
+AC_MSG_CHECKING(whether to include PCRE support)
 AC_ARG_WITH(pcre-regex,
-[  --with-pcre-regex[=DIR] Enable Perl Compatible Regular Expressions support],[
-  case "$withval" in
-    no)
-       AC_MSG_RESULT(no) ;;
-    yes)
-        EXTRA_LIBS="-Lext/pcre/pcrelib -lpcre $EXTRA_LIBS"
-        PCRE_SUBDIR="pcrelib"
-       AC_DEFINE(HAVE_PCRE, 1)
-        AC_DEFINE(HAVE_BUNDLED_PCRE, 1)
-       AC_MSG_RESULT(yes)
-        PHP_EXTENSION(pcre) ;;
-    *)
-        test -f $withval/pcre.h && PCRE_INCLUDE="-I$withval"
-        test -f $withval/libpcre.a && PCRE_LIB="-L$withval -lpcre"
-       
-        if test -n "$PCRE_INCLUDE" && test -n "$PCRE_LIB" ; then
-          INCLUDES="$PCRE_INCLUDE $INCLUDES"
-          EXTRA_LIBS="$PCRE_LIB $EXTRA_LIBS"
-          AC_DEFINE(HAVE_PCRE, 1)
-          AC_DEFINE(HAVE_BUNDLED_PCRE, 0)
-          PCRE_SUBDIR=
-          AC_MSG_RESULT(yes)
-          PHP_EXTENSION(pcre)
-        else
-          AC_MSG_ERROR(Unable to find pcre.h and libpcre.a in $withval)
-        fi ;;
-  esac
+[  --with-pcre-regex[=DIR] Include Perl Compatible Regular Expressions support],[
+  if test "$withval" = "yes"; then
+    EXTRA_LIBS="-Lext/pcre/pcrelib -lpcre $EXTRA_LIBS"
+    PCRE_SUBDIR="pcrelib"
+    AC_DEFINE(HAVE_PCRE, 1)
+    AC_MSG_RESULT(yes)
+    PHP_EXTENSION(pcre)
+  else
+    AC_MSG_RESULT(no)
+  fi
 ],[
   EXTRA_LIBS="-Lext/pcre/pcrelib -lpcre $EXTRA_LIBS"
   PCRE_SUBDIR="pcrelib"
   AC_DEFINE(HAVE_PCRE, 1)
-  AC_DEFINE(HAVE_BUNDLED_PCRE, 1)
   AC_MSG_RESULT(yes)
   PHP_EXTENSION(pcre)
 ]) 
index 791a913545a6550c09189f6c1339960cc80a546e..30721b09e639daf7fd3e7069c0d52b24afc2fe2a 100644 (file)
@@ -29,6 +29,8 @@
 
 /* $Id$ */
 
+/* Get PCRE library from ftp://ftp.cus.cam.ac.uk/pub/software/programs/pcre/ */
+
 #include "php.h"
 
 #if HAVE_PCRE
@@ -50,7 +52,7 @@ function_entry pcre_functions[] = {
        {NULL,          NULL,           NULL}
 };
 
-php3_module_entry pcre_module_entry = {
+zend_module_entry pcre_module_entry = {
    "PCRE", pcre_functions, php_minit_pcre, php_mshutdown_pcre,
                   php_rinit_pcre, NULL,
                   php_info_pcre, STANDARD_MODULE_PROPERTIES
@@ -381,7 +383,7 @@ void _pcre_match(INTERNAL_FUNCTION_PARAMETERS, int global)
                count = pcre_exec(re, extra, &subject->value.str.val[subject_offset],
                                                  subject->value.str.len-subject_offset,
                                                  (subject_offset ? exoptions|PCRE_NOTBOL : exoptions),
-                                                 offsets, size_offsets);
+                                                 offsets, size_offsets, 0);
 
                /* Check for too many substrings condition. */  
                if (count == 0) {
@@ -508,12 +510,14 @@ char *_php_pcre_replace(char *regex, char *subject, char *replace)
        int                              alloc_len;                     /* Actual allocated length */
        int                              subject_len;           /* Length of the subject string */
        int                              result_len;            /* Current length of the result */
-       int                              subject_offset;        /* Current position in the subject string */
        int                              backref;                       /* Backreference number */
        char                    *result,                        /* Result of replacement */
                                        *new_buf,                       /* Temporary buffer for re-allocation */
                                        *walkbuf,                       /* Location of current replacement in the result */
-                                       *walk;                          /* Used to walk the replacement string */
+                                       *walk,                          /* Used to walk the replacement string */
+                                       *match,                         /* The current match */
+                                       *piece,                         /* The current piece of subject */
+                                       *subject_end;           /* Points to the end of the subject */
 
        /* Compile regex or get it from cache. */
        if ((re = _pcre_get_compiled_regex(regex, extra)) == NULL) {
@@ -535,23 +539,29 @@ char *_php_pcre_replace(char *regex, char *subject, char *replace)
                return NULL;
        }
 
-       subject_offset = 0;
+       /* Initialize */
        result[0] = '\0';
+       piece = subject;
+       subject_end = subject + subject_len;
+       match = NULL;
        
        while (count >= 0) {
                /* Execute the regular expression. */
-               count = pcre_exec(re, extra, &subject[subject_offset],
-                                                       subject_len-subject_offset,
-                                                       (subject_offset ? exoptions|PCRE_NOTBOL : exoptions),
-                                                       offsets, size_offsets);
-
-               /* Check for too many substrings condition. */  
+               count = pcre_exec(re, extra, piece,
+                                                       subject_end-piece,
+                                                       (piece==subject ? exoptions : exoptions|PCRE_NOTBOL),
+                                                       offsets, size_offsets, (piece == match));
+               
+               /* Check for too many substrings condition. */
                if (count == 0) {
                        zend_error(E_NOTICE, "Matched, but too many substrings\n");
                        count = size_offsets/3;
                }
 
                if (count > 0) {
+                       /* Set the match location in piece */
+                       match = piece + offsets[0];
+
                        new_len = strlen(result) + offsets[0]; /* part before the match */
                        walk = replace;
                        while (*walk)
@@ -574,7 +584,7 @@ char *_php_pcre_replace(char *regex, char *subject, char *replace)
                        }
                        result_len = strlen(result);
                        /* copy the part of the string before the match */
-                       strncat(result, &subject[subject_offset], offsets[0]);
+                       strncat(result, piece, match-piece);
 
                        /* copy replacement and backrefs */
                        walkbuf = &result[result_len + offsets[0]];
@@ -585,7 +595,7 @@ char *_php_pcre_replace(char *regex, char *subject, char *replace)
                                        backref < count) {
                                        result_len = offsets[(backref<<1)+1] - offsets[backref<<1];
                                        memcpy (walkbuf,
-                                                       &subject[subject_offset + offsets[backref<<1]],
+                                                       piece + offsets[backref<<1],
                                                        result_len);
                                        walkbuf += result_len;
                                        walk += (backref > 9) ? 3 : 2;
@@ -593,26 +603,10 @@ char *_php_pcre_replace(char *regex, char *subject, char *replace)
                                        *walkbuf++ = *walk++;
                        *walkbuf = '\0';
 
-                       /* and get ready to keep looking for replacements */
-                       if (offsets[0] == offsets[1]) {
-                               if (offsets[0] + subject_offset >= subject_len)
-                                       break;
-                               new_len = strlen (result) + 1;
-                               if (new_len + 1 > alloc_len) {
-                                       alloc_len = 1 + alloc_len + 2 * new_len;
-                                       new_buf = emalloc(alloc_len * sizeof(char));
-                                       strcpy(new_buf, result);
-                                       efree(result);
-                                       result = new_buf;
-                               }
-                               subject_offset += offsets[1] + 1;
-                               result [new_len-1] = subject[subject_offset-1];
-                               result [new_len] = '\0';
-                       } else {
-                               subject_offset += offsets[1];
-                       }
+                       /* Advance to the next piece */
+                       piece += offsets[1];
                } else {
-                       new_len = strlen(result) + strlen(&subject[subject_offset]);
+                       new_len = strlen(result) + subject_end-piece;
                        if (new_len + 1 > alloc_len) {
                                alloc_len = new_len + 1; /* now we know exactly how long it is */
                                new_buf = emalloc(alloc_len * sizeof(char));
@@ -621,7 +615,7 @@ char *_php_pcre_replace(char *regex, char *subject, char *replace)
                                result = new_buf;
                        }
                        /* stick that last bit of string on our output */
-                       strcat(result, &subject[subject_offset]);
+                       strcat(result, piece);
                }
        }
        
@@ -716,9 +710,8 @@ PHP_FUNCTION(preg_replace)
        zval                    *regex,
                                        *replace,
                                        *subject,
-                                       **subject_entry_ptr,
-                                       *subject_entry,
-                                       *return_entry;
+                                  **subject_entry_ptr,
+                                       *subject_entry;
        char                    *result;
        
        /* Get function parameters and do error-checking. */
@@ -805,7 +798,7 @@ PHP_FUNCTION(preg_split)
                count = pcre_exec(re, extra, &subject->value.str.val[last_offset],
                                                  subject->value.str.len-last_offset,
                                                  (last_offset ? exoptions|PCRE_NOTBOL : exoptions),
-                                                 offsets, size_offsets);
+                                                 offsets, size_offsets, 0);
 
                /* Check for too many substrings condition. */
                if (count == 0) {
index dd5852dd31ae77fb3bbf03f9bd3c80e7be510e04..caeeaa4971c8e0231e63d548a671c88b060ee9fc 100644 (file)
@@ -4125,7 +4125,7 @@ Returns:          > 0 => success; value is the number of elements filled in
 
 int
 pcre_exec(const pcre *external_re, const pcre_extra *external_extra,
-  const char *subject, int length, int options, int *offsets, int offsetcount)
+  const char *subject, int length, int options, int *offsets, int offsetcount, int minlen)
 {
 int resetcount, ocount;
 int first_char = -1;
@@ -4287,6 +4287,10 @@ do
   if (!match(start_match, re->code, 2, &match_block, ims, FALSE, start_match))
     continue;
 
+  /* Check that the match is not closer than minlen (Andrey Zmievski) */
+  if (start_match - match_block.start_subject < minlen)
+    continue;
+
   /* Copy the offset information from temporary store if necessary */
 
   if (using_temporary_offsets)
index 27204b6605a11e2d040389a99b3eb155db958820..0efc0f650215aeedc88bebeba93347f01be12252 100644 (file)
@@ -59,7 +59,7 @@ extern pcre *pcre_compile(const char *, int, const char **, int *,
   const unsigned char *);
 extern int pcre_copy_substring(const char *, int *, int, int, char *, int);
 extern int pcre_exec(const pcre *, const pcre_extra *, const char *,
-  int, int, int *, int);
+  int, int, int *, int, int);
 extern int pcre_get_substring(const char *, int *, int, int, const char **);
 extern int pcre_get_substring_list(const char *, int *, int, const char ***);
 extern int pcre_info(const pcre *, int *, int *);
index d4a8e12b300fd0c54d1d85e5e319e8709f686d38..99f7578ca9cd7789e4bc557c1b03730047a2fdb0 100644 (file)
 
 #if HAVE_PCRE
 
-#if HAVE_BUNDLED_PCRE
 #include "pcrelib/pcre.h"
-#else
-#include "pcre.h"
-#endif
 
 extern void php_info_pcre(ZEND_MODULE_INFO_FUNC_ARGS);
 extern int php_minit_pcre(INIT_FUNC_ARGS);