Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autofilter Part 2 #2162

Merged
merged 5 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use WildcardMatch
Per suggestion from @MarkBaker.

WildcardMatch did not handle double tilde correctly. It has been changed to do so and its logic simplified (and commented).

Existing AutoFilter test covered this situation, but I added a test for MATCH as well.
  • Loading branch information
oleibman committed Jun 16, 2021
commit 75615ba444ddf7504da41594837fbe9948924be3
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -755,16 +755,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Calculation/Internal/MakeMatrix.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Internal\\\\WildcardMatch\\:\\:wildcard\\(\\) should return string but returns string\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Internal\\\\WildcardMatch\\:\\:compare\\(\\) has parameter \\$value with no typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php

-
message: "#^Call to function is_string\\(\\) with null will always evaluate to false\\.$#"
count: 3
Expand Down
28 changes: 14 additions & 14 deletions src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
class WildcardMatch
{
private const SEARCH_SET = [
'/(?<!~)\*/ui',
'/~\*/ui',
'/(?<!~)\?/ui',
'/~\?/ui',
'~~', // convert double tilde to unprintable value
'~\\*', // convert tilde backslash asterisk to [*] (matches literal asterisk in regexp)
'\\*', // convert backslash asterisk to .* (matches string of any length in regexp)
'~\\?', // convert tilde backslash question to [?] (matches literal question mark in regexp)
'\\?', // convert backslash question to . (matches one character in regexp)
"\x1c", // convert original double tilde to single tilde
];

private const REPLACEMENT_SET = [
'${1}.*',
'\*',
'${1}.',
'\?',
"\x1c",
'[*]',
'.*',
'[?]',
'.',
'~',
];

public static function wildcard(string $wildcard): string
{
// Preg Escape the wildcard, but protecting the Excel * and ? search characters
$wildcard = str_replace(['*', '?'], [0x1A, 0x1B], $wildcard);
$wildcard = preg_quote($wildcard);
$wildcard = str_replace([0x1A, 0x1B], ['*', '?'], $wildcard);

return preg_replace(self::SEARCH_SET, self::REPLACEMENT_SET, $wildcard);
return str_replace(self::SEARCH_SET, self::REPLACEMENT_SET, preg_quote($wildcard));
}

public static function compare($value, string $wildcard): bool
public static function compare(string $value, string $wildcard): bool
{
if ($value === '') {
return true;
Expand Down
11 changes: 2 additions & 9 deletions src/PhpSpreadsheet/Worksheet/AutoFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DateTimeZone;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Internal\WildcardMatch;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Shared\Date;
Expand Down Expand Up @@ -452,13 +453,6 @@ private static function filterTestInPeriodDateSet($cellValue, $monthSet)
return false;
}

/**
* Search/Replace arrays to convert Excel wildcard syntax to a regexp syntax for preg_matching.
*/
private const FROM_REPLACE = ['~~', '~\\*', '\\*', '~\\?', '\\?', "\x1c"];

private const TO_REPLACE = ["\x1c", '[*]', '.*', '[?]', '.', '~'];

private static function makeDateObject(int $year, int $month, int $day, int $hour = 0, int $minute = 0, int $second = 0): DateTime
{
$baseDate = new DateTime();
Expand Down Expand Up @@ -853,8 +847,7 @@ public function showHideRows()
$ruleValue = $rule->getValue();
if (!is_array($ruleValue) && !is_numeric($ruleValue)) {
// Convert to a regexp allowing for regexp reserved characters, wildcards and escaped wildcards
$ruleValue = preg_quote("$ruleValue");
$ruleValue = str_replace(self::FROM_REPLACE, self::TO_REPLACE, $ruleValue);
$ruleValue = WildcardMatch::wildcard($ruleValue);
if (trim($ruleValue) == '') {
$customRuleForBlanks = true;
$ruleValue = trim($ruleValue);
Expand Down
6 changes: 6 additions & 0 deletions tests/data/Calculation/LookupRef/MATCH.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,10 @@
['Obtuse', 'Amuse', 'Obverse', 'Inverse', 'Assurance', 'Amplitude', 'Adverse', 'Apartment'],
0,
],
[
3, // Expected
'*~~*', // contains a tilde
['aAAAAA', 'a123456*c', 'abc~xyz', 'alembic'],
0,
],
];