Skip to content

Commit

Permalink
Accommodating Slash with preg_quote - Text Functions (#3582)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oleibman authored May 27, 2023
1 parent 9a13f52 commit 4f6d1f7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Calculation/TextData/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -270,7 +270,7 @@ function ($delimiter) {
return '(' . $delimiters . ')';
}

return '(' . preg_quote($delimiter ?? '') . ')';
return '(' . preg_quote($delimiter ?? '', '/') . ')';
}

private static function matchFlags(int $matchMode): string
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/TextData/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Calculation/TextData/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/data/Calculation/TextData/NUMBERVALUE.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
],
'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', '/', ','],
'issue 3574 null string treated as 0' => [0, '', ',', ' '],
'issue 3574 one or more spaces treated as 0' => [0, ' ', ',', ' '],
'issue 3574 non-blank numeric string okay' => [2, ' 2 ', ',', ' '],
Expand Down
7 changes: 7 additions & 0 deletions tests/data/Calculation/TextData/TEXTAFTER.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,11 @@
1,
],
],
'slash delimiter' => [
'about/that',
[
'How/about/that',
'/',
],
],
];
7 changes: 7 additions & 0 deletions tests/data/Calculation/TextData/TEXTBEFORE.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,11 @@
1,
],
],
'slash delimiter' => [
'How',
[
'How/about/that',
'/',
],
],
];
16 changes: 16 additions & 0 deletions tests/data/Calculation/TextData/TEXTSPLIT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
'.',
'/',
],
],
];

0 comments on commit 4f6d1f7

Please sign in to comment.