From 3f723a2a74fcd91c988f26e428106b3b4a08358e Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 16 Jan 2017 16:26:20 +0100 Subject: [PATCH] Add BC for PHP 7.2, see https://bugs.php.net/bug.php?id=73947 --- ChangeLog.md | 6 ++++++ src/main/php/text/regex/MatchResult.class.php | 10 +++++++++- src/test/php/text/regex/unittest/PatternTest.class.php | 8 ++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index c7fa78d..a42b916 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,12 @@ Pattern matching changelog ## ?.?.? / ????-??-?? +## 7.1.1 / 2017-01-16 + +* Added BC for PHP 7.2: Empty optional patterns should not be NULLed + See https://bugs.php.net/bug.php?id=73947 + (@thekid) + ## 7.1.0 / 2016-08-29 * Added version compatibility with XP 8 - @thekid diff --git a/src/main/php/text/regex/MatchResult.class.php b/src/main/php/text/regex/MatchResult.class.php index 82f85de..7ad6218 100755 --- a/src/main/php/text/regex/MatchResult.class.php +++ b/src/main/php/text/regex/MatchResult.class.php @@ -25,7 +25,15 @@ static function __static() { */ public function __construct($length, $matches) { $this->length= $length; - $this->matches= $matches; + + // Ensure empty patterns are not NULLed to ensure BC. + // See https://bugs.php.net/bug.php?id=73947 + foreach ($matches as $group => $match) { + $this->matches[$group]= []; + foreach ($match as $i => $segment) { + $this->matches[$group][$i]= (string)$segment; + } + } } /** diff --git a/src/test/php/text/regex/unittest/PatternTest.class.php b/src/test/php/text/regex/unittest/PatternTest.class.php index 876a41b..0adde5c 100644 --- a/src/test/php/text/regex/unittest/PatternTest.class.php +++ b/src/test/php/text/regex/unittest/PatternTest.class.php @@ -202,4 +202,12 @@ public function stringCastWithFlag() { public function stringCastWithFlags() { $this->assertEquals('/end$/iU', (string)new Pattern('end$', Pattern::CASE_INSENSITIVE | Pattern::UNGREEDY)); } + + #[@test] + public function php_bug_73947() { + $this->assertEquals( + ['http://domain', 'http', '', 'domain'], + Pattern::compile('([a-z]+)://([^@]+@)?([a-z]+)')->match('http://domain')->group(0) + ); + } }