]> granicus.if.org Git - php/commitdiff
Implement request #53659 (Implementing RegexIterator::getRegex() method). Patch
authorAdam Harvey <aharvey@php.net>
Thu, 6 Jan 2011 02:32:02 +0000 (02:32 +0000)
committerAdam Harvey <aharvey@php.net>
Thu, 6 Jan 2011 02:32:02 +0000 (02:32 +0000)
by Joshua Thijssen.

NEWS
UPGRADING
ext/spl/internal/regexiterator.inc
ext/spl/spl_iterators.c
ext/spl/spl_iterators.h
ext/spl/tests/regexiterator_getregex.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 6f63542f9767bdcb578e861940218162ff2a430d..c8d186dd61326a4815a94751ca4a4ec68637f270 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -182,6 +182,7 @@ PHP                                                                        NEWS
     either is present at compile time. (Rasmus)
 
 - Improved SPL extension:
+  . Added RegexIterator::getRegex() method. (Joshua Thijssen)
   . Added SplObjectStorage::getHash() hook. (Etienne)
   . Added SplFileInfo::getExtension(). FR #48767. (Peter Cowburn)
 
index 8787b4018fdbca92807e2c660397faaf65d7d1cc..b590324764cb608ab8af0219321d5a47054d3933 100755 (executable)
--- a/UPGRADING
+++ b/UPGRADING
@@ -353,6 +353,9 @@ UPGRADE NOTES - PHP X.Y
          - ReflectionClass::getTraitAliases()
          - ReflectionParameter::canBePassedByValue()
        
+       - RegexIterator
+         - RegexIterator::getRegex()
+
        - PDO_dblib
          - PDO::newRowset()
 
index 8b4ffe9dad8c627e61f01f7a363537ce52613c4a..4eef7ddcf598f0d2726ac42fd11cfd5d43ae8ab4 100755 (executable)
@@ -158,6 +158,13 @@ class RegexIterator extends FilterIterator
        {
                $this->preg_flags = $preg_flags;
        }
+
+       /** @return current regular expression
+       */
+       function getRegex()
+       {
+               return $this->regex;
+       }
 }
 
 ?>
index c10f59f7e07d61016b1c3600a8e18b01525905df..e2ad8f3b2c28955e723c38a026ff8fe89f6baa11 100755 (executable)
@@ -1465,6 +1465,7 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
                        }
                        intern->u.regex.mode = mode;
                        intern->u.regex.regex = estrndup(regex, regex_len);
+                       intern->u.regex.regex_len = regex_len;
                        intern->u.regex.pce = pcre_get_compiled_regex_cache(regex, regex_len TSRMLS_CC);
                        if (intern->u.regex.pce == NULL) {
                                /* pcre_get_compiled_regex_cache has already sent error */
@@ -1941,6 +1942,19 @@ SPL_METHOD(RegexIterator, accept)
        }
 } /* }}} */
 
+/* {{{ proto string RegexIterator::getRegex()
+   Returns current regular expression */
+SPL_METHOD(RegexIterator, getRegex)
+{
+       spl_dual_it_object *intern = (spl_dual_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+
+       if (zend_parse_parameters_none() == FAILURE) {
+               return;
+       }
+
+       RETURN_STRINGL(intern->u.regex.regex, intern->u.regex.regex_len, 1);
+} /* }}} */
+
 /* {{{ proto bool RegexIterator::getMode()
    Returns current operation mode */
 SPL_METHOD(RegexIterator, getMode)
@@ -2205,6 +2219,7 @@ static const zend_function_entry spl_funcs_RegexIterator[] = {
        SPL_ME(RegexIterator,   setFlags,         arginfo_regex_it_set_flags,      ZEND_ACC_PUBLIC)
        SPL_ME(RegexIterator,   getPregFlags,     arginfo_recursive_it_void,       ZEND_ACC_PUBLIC)
        SPL_ME(RegexIterator,   setPregFlags,     arginfo_regex_it_set_preg_flags, ZEND_ACC_PUBLIC)
+       SPL_ME(RegexIterator,   getRegex,         arginfo_recursive_it_void,       ZEND_ACC_PUBLIC)
        {NULL, NULL, NULL}
 };
 
index d9f57d8b78c6a853407d7d4c0b9543238fe6e989..ef59fcc6ea8bbb4810c53cce58275241d06d081d 100755 (executable)
@@ -154,6 +154,7 @@ typedef struct _spl_dual_it_object {
                        long             preg_flags;
                        pcre_cache_entry *pce;
                        char             *regex;
+                       uint             regex_len;
                } regex;
 #endif
        } u;
diff --git a/ext/spl/tests/regexiterator_getregex.phpt b/ext/spl/tests/regexiterator_getregex.phpt
new file mode 100644 (file)
index 0000000..d3113a5
--- /dev/null
@@ -0,0 +1,29 @@
+--TEST--
+SPL: RegexIterator::getRegex() basic tests
+--CREDITS--
+Joshua Thijssen <jthijssen@noxlogic.nl>
+--FILE--
+<?php
+
+$array = array('cat', 'hat', 'sat');
+$iterator = new ArrayIterator($array);
+
+# Simple regex
+$regexIterator = new RegexIterator($iterator, '/.at/');
+var_dump($regexIterator->getRegex());
+
+# Empty regular expression
+$regexIterator = new RegexIterator($iterator, '//');
+var_dump($regexIterator->getRegex());
+
+# "Complex" email regular expression
+$regexIterator = new RegexIterator($iterator, '|\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b|');
+var_dump($regexIterator->getRegex());
+
+
+
+?>
+--EXPECT--
+string(5) "/.at/"
+string(2) "//"
+string(43) "|\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b|"