From dbd800e7b80a485c2eaad9fea3c804403fce306a Mon Sep 17 00:00:00 2001 From: James Lucas Date: Thu, 2 Dec 2021 22:24:53 +1100 Subject: [PATCH] Fixes #2430 * Handle a wildcard match that contains a forward slash in the pattern by adding / to the delimiter list of preg_quote * Fix SUMIF doing a wildcard match on empty cells (NULL) * Fix compare logic to return false when value is an empty string or NULL (Verified against LibreOffice SUMIF and MATCH handling of empty cells) --- .../Calculation/Internal/WildcardMatch.php | 8 ++++---- tests/data/Calculation/LookupRef/MATCH.php | 6 ++++++ tests/data/Calculation/MathTrig/SUMIF.php | 12 ++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php b/src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php index 5731569f7a..371ad8b3d1 100644 --- a/src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php +++ b/src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php @@ -25,13 +25,13 @@ class WildcardMatch public static function wildcard(string $wildcard): string { // Preg Escape the wildcard, but protecting the Excel * and ? search characters - return str_replace(self::SEARCH_SET, self::REPLACEMENT_SET, preg_quote($wildcard)); + return str_replace(self::SEARCH_SET, self::REPLACEMENT_SET, preg_quote($wildcard, '/')); } - public static function compare(string $value, string $wildcard): bool + public static function compare(?string $value, string $wildcard): bool { - if ($value === '') { - return true; + if ($value === '' || $value === null) { + return false; } return (bool) preg_match("/^{$wildcard}\$/mui", $value); diff --git a/tests/data/Calculation/LookupRef/MATCH.php b/tests/data/Calculation/LookupRef/MATCH.php index 47b925285f..b424f28e6f 100644 --- a/tests/data/Calculation/LookupRef/MATCH.php +++ b/tests/data/Calculation/LookupRef/MATCH.php @@ -352,4 +352,10 @@ ['aAAAAA', 'a123456*c', 'abc~xyz', 'alembic'], 0, ], + [ + 2, // Expected + 'abc/123*', // wildcard search contains a forward slash + ['abc123fff', 'abc/123fff'], + 0, + ], ]; diff --git a/tests/data/Calculation/MathTrig/SUMIF.php b/tests/data/Calculation/MathTrig/SUMIF.php index a427624f30..3daf6bfdbc 100644 --- a/tests/data/Calculation/MathTrig/SUMIF.php +++ b/tests/data/Calculation/MathTrig/SUMIF.php @@ -168,4 +168,16 @@ 'n', [[1], [2], [3], [4], [5], [6], [7], [8], [9]], ], + [ + 2, + [ + [null], + ['abc123fff'], + ], + 'abc123*', + [ + [1], + [2], + ], + ], ];