From f0f8e1107d146c84e4dac59d3b51583bfe4f5ae8 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Tue, 23 May 2023 18:36:20 -0700 Subject: [PATCH] Accomodating Slash with preg_quote - Text Functions PR #3513, developed by @SaidkhojaIftikhor, has been stuck for some time awaiting tests. This is the first of three PRs to replace that one. This accomodates the use of slash as a delimiter in functions TEXTAFTER, TEXTBEFORE, TEXTSPLIT, and NUMBERVALUE. The source changes are very simple. Additional tests exercise all the source changes. --- .../Calculation/TextData/Extract.php | 4 ++-- .../Calculation/TextData/Format.php | 2 +- src/PhpSpreadsheet/Calculation/TextData/Text.php | 4 ++-- tests/data/Calculation/TextData/NUMBERVALUE.php | 2 ++ tests/data/Calculation/TextData/TEXTAFTER.php | 7 +++++++ tests/data/Calculation/TextData/TEXTBEFORE.php | 7 +++++++ tests/data/Calculation/TextData/TEXTSPLIT.php | 16 ++++++++++++++++ 7 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/TextData/Extract.php b/src/PhpSpreadsheet/Calculation/TextData/Extract.php index 519607c080..24ddff2ec5 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Extract.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Extract.php @@ -261,7 +261,7 @@ private static function buildDelimiter($delimiter): string $delimiter = Functions::flattenArray($delimiter); $quotedDelimiters = array_map( function ($delimiter) { - return preg_quote($delimiter ?? ''); + return preg_quote($delimiter ?? '', '/'); }, $delimiter ); @@ -270,7 +270,7 @@ function ($delimiter) { return '(' . $delimiters . ')'; } - return '(' . preg_quote($delimiter ?? '') . ')'; + return '(' . preg_quote($delimiter ?? '', '/') . ')'; } private static function matchFlags(int $matchMode): string diff --git a/src/PhpSpreadsheet/Calculation/TextData/Format.php b/src/PhpSpreadsheet/Calculation/TextData/Format.php index 5e0762fd69..4f6453719e 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Format.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Format.php @@ -285,7 +285,7 @@ public static function NUMBERVALUE($value = '', $decimalSeparator = null, $group } if (!is_numeric($value)) { - $decimalPositions = preg_match_all('/' . preg_quote($decimalSeparator) . '/', $value, $matches, PREG_OFFSET_CAPTURE); + $decimalPositions = preg_match_all('/' . preg_quote($decimalSeparator, '/') . '/', $value, $matches, PREG_OFFSET_CAPTURE); if ($decimalPositions > 1) { return ExcelError::VALUE(); } diff --git a/src/PhpSpreadsheet/Calculation/TextData/Text.php b/src/PhpSpreadsheet/Calculation/TextData/Text.php index 8e6a575a29..b8a730767c 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Text.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Text.php @@ -193,7 +193,7 @@ private static function buildDelimiter($delimiter): string if (is_array($delimiter) && count($valueSet) > 1) { $quotedDelimiters = array_map( function ($delimiter) { - return preg_quote($delimiter ?? ''); + return preg_quote($delimiter ?? '', '/'); }, $valueSet ); @@ -202,7 +202,7 @@ function ($delimiter) { return '(' . $delimiters . ')'; } - return '(' . preg_quote(/** @scrutinizer ignore-type */ Functions::flattenSingleValue($delimiter)) . ')'; + return '(' . preg_quote(/** @scrutinizer ignore-type */ Functions::flattenSingleValue($delimiter), '/') . ')'; } private static function matchFlags(bool $matchMode): string diff --git a/tests/data/Calculation/TextData/NUMBERVALUE.php b/tests/data/Calculation/TextData/NUMBERVALUE.php index 791e61cf85..a8a1368e50 100644 --- a/tests/data/Calculation/TextData/NUMBERVALUE.php +++ b/tests/data/Calculation/TextData/NUMBERVALUE.php @@ -51,4 +51,6 @@ ], 'no arguments' => ['exception'], 'boolean argument' => ['#VALUE!', true], + 'slash as group separator' => [1234567.1, '1/234/567.1', '.', '/'], + 'slash as decimal separator' => [1234567.1, '1,234,567/1', '/', ','], ]; diff --git a/tests/data/Calculation/TextData/TEXTAFTER.php b/tests/data/Calculation/TextData/TEXTAFTER.php index ebcfecb946..c0e3e3d51f 100644 --- a/tests/data/Calculation/TextData/TEXTAFTER.php +++ b/tests/data/Calculation/TextData/TEXTAFTER.php @@ -248,4 +248,11 @@ 1, ], ], + 'slash delimiter' => [ + 'about/that', + [ + 'How/about/that', + '/', + ], + ], ]; diff --git a/tests/data/Calculation/TextData/TEXTBEFORE.php b/tests/data/Calculation/TextData/TEXTBEFORE.php index 1929354ce5..a79d37b0fd 100644 --- a/tests/data/Calculation/TextData/TEXTBEFORE.php +++ b/tests/data/Calculation/TextData/TEXTBEFORE.php @@ -240,4 +240,11 @@ 1, ], ], + 'slash delimiter' => [ + 'How', + [ + 'How/about/that', + '/', + ], + ], ]; diff --git a/tests/data/Calculation/TextData/TEXTSPLIT.php b/tests/data/Calculation/TextData/TEXTSPLIT.php index 64016ca7a7..a10b26f6e8 100644 --- a/tests/data/Calculation/TextData/TEXTSPLIT.php +++ b/tests/data/Calculation/TextData/TEXTSPLIT.php @@ -104,4 +104,20 @@ '', ], ], + 'slash as column delimiter' => [ + [['Hello', 'World']], + [ + 'Hello/World', + '/', + '', + ], + ], + 'slash as row delimiter' => [ + [['ho', 'w'], ['about', '#N/A'], ['t', 'hat']], + [ + 'ho.w/about/t.hat', + '.', + '/', + ], + ], ];