From 57536d2f9db577924b8ae10d5d4f3cae7f7b07ed Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 27 Oct 2021 00:25:02 +0200 Subject: [PATCH] Strings, Arrays: $flags replaced with parameters --- src/Utils/Arrays.php | 5 ++-- src/Utils/Strings.php | 45 +++++++++++++++++++++-------- tests/Utils/Arrays.grep().phpt | 5 ++++ tests/Utils/Strings.match().phpt | 1 + tests/Utils/Strings.matchAll().phpt | 14 +++++++++ tests/Utils/Strings.split().phpt | 16 ++++++++++ 6 files changed, 72 insertions(+), 14 deletions(-) diff --git a/src/Utils/Arrays.php b/src/Utils/Arrays.php index 81d030936..ef15c5145 100644 --- a/src/Utils/Arrays.php +++ b/src/Utils/Arrays.php @@ -186,12 +186,13 @@ public static function renameKey(array &$array, string|int $oldKey, string|int $ /** - * Returns only those array items, which matches a regular expression $pattern. + * Returns only those array items, which matches a regular expression $pattern. Parameter $flags is deprecated. * @param string[] $array * @return string[] */ - public static function grep(array $array, string $pattern, int $flags = 0): array + public static function grep(array $array, string $pattern, int $flags = 0, bool $invert = false): array { + $flags |= $invert ? PREG_GREP_INVERT : 0; return Strings::pcre('preg_grep', [$pattern, $array, $flags]); } diff --git a/src/Utils/Strings.php b/src/Utils/Strings.php index 353d85fc1..ef3529de2 100644 --- a/src/Utils/Strings.php +++ b/src/Utils/Strings.php @@ -468,20 +468,33 @@ private static function pos(string $haystack, string $needle, int $nth = 1): ?in /** * Splits a string into array by the regular expression. Parenthesized expression in the delimiter are captured. - * Argument $flags can be any combination of PREG_SPLIT_NO_EMPTY and PREG_OFFSET_CAPTURE flags. + * Parameter $flags is deprecated. */ - public static function split(string $subject, string $pattern, int $flags = 0): array - { - return self::pcre('preg_split', [$pattern, $subject, -1, $flags | PREG_SPLIT_DELIM_CAPTURE]); + public static function split( + string $subject, + string $pattern, + int $flags = 0, + bool $captureOffset = false, + bool $noEmpty = false, + ): array { + $flags |= ($captureOffset ? PREG_SPLIT_OFFSET_CAPTURE : 0) | ($noEmpty ? PREG_SPLIT_NO_EMPTY : 0) | PREG_SPLIT_DELIM_CAPTURE; + return self::pcre('preg_split', [$pattern, $subject, -1, $flags]); } /** * Checks if given string matches a regular expression pattern and returns an array with first found match and each subpattern. - * Argument $flags can be any combination of PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags. + * Parameter $flags is deprecated. */ - public static function match(string $subject, string $pattern, int $flags = 0, int $offset = 0): ?array - { + public static function match( + string $subject, + string $pattern, + int $flags = 0, + int $offset = 0, + bool $captureOffset = false, + bool $unmatchedAsNull = false, + ): ?array { + $flags |= ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0); if ($offset > strlen($subject)) { return null; } @@ -492,11 +505,19 @@ public static function match(string $subject, string $pattern, int $flags = 0, i /** - * Finds all occurrences matching regular expression pattern and returns a two-dimensional array. Result is array of matches (ie uses by default PREG_SET_ORDER). - * Argument $flags can be any combination of PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL and PREG_PATTERN_ORDER flags. + * Finds all occurrences matching regular expression pattern and returns a two-dimensional array. + * Result is array of matches (ie uses by default PREG_SET_ORDER). Parameter $flags is deprecated. */ - public static function matchAll(string $subject, string $pattern, int $flags = 0, int $offset = 0): array - { + public static function matchAll( + string $subject, + string $pattern, + int $flags = 0, + int $offset = 0, + bool $captureOffset = false, + bool $unmatchedAsNull = false, + bool $patternOrder = false, + ): array { + $flags |= ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0) | ($patternOrder ? PREG_PATTERN_ORDER : 0); if ($offset > strlen($subject)) { return []; } @@ -524,7 +545,7 @@ public static function replace( if (!is_callable($replacement, false, $textual)) { throw new Nette\InvalidStateException("Callback '$textual' is not callable."); } - $flags = ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0); + $flags = ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0); return self::pcre('preg_replace_callback', [$pattern, $replacement, $subject, $limit, 0, $flags]); } elseif (is_array($pattern) && is_string(key($pattern))) { diff --git a/tests/Utils/Arrays.grep().phpt b/tests/Utils/Arrays.grep().phpt index f356405d3..f8613b8ae 100644 --- a/tests/Utils/Arrays.grep().phpt +++ b/tests/Utils/Arrays.grep().phpt @@ -21,3 +21,8 @@ Assert::same([ 0 => 'a', 2 => 'c', ], Arrays::grep(['a', '1', 'c'], '#\d#', PREG_GREP_INVERT)); + +Assert::same([ + 0 => 'a', + 2 => 'c', +], Arrays::grep(['a', '1', 'c'], '#\d#', invert: true)); diff --git a/tests/Utils/Strings.match().phpt b/tests/Utils/Strings.match().phpt index 5814b5753..314472c01 100644 --- a/tests/Utils/Strings.match().phpt +++ b/tests/Utils/Strings.match().phpt @@ -20,6 +20,7 @@ Assert::same(['hell', 'l'], Strings::match('hello world!', '#([e-l])+#')); Assert::same(['hell'], Strings::match('hello world!', '#[e-l]+#')); Assert::same([['hell', 0]], Strings::match('hello world!', '#[e-l]+#', PREG_OFFSET_CAPTURE)); +Assert::same([['hell', 0]], Strings::match('hello world!', '#[e-l]+#', captureOffset: true)); Assert::same(['ll'], Strings::match('hello world!', '#[e-l]+#', 0, 2)); diff --git a/tests/Utils/Strings.matchAll().phpt b/tests/Utils/Strings.matchAll().phpt index 384bbc18a..bc81c4bee 100644 --- a/tests/Utils/Strings.matchAll().phpt +++ b/tests/Utils/Strings.matchAll().phpt @@ -32,14 +32,28 @@ Assert::same([ [['k', 14], ['k', 14], ['', 15]], ], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', PREG_OFFSET_CAPTURE)); +Assert::same([ + [['lu', 2], ['l', 2], ['u', 3]], + [['ou', 6], ['o', 6], ['u', 7]], + [['k', 10], ['k', 10], ['', 11]], + [['k', 14], ['k', 14], ['', 15]], +], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', captureOffset: true)); + Assert::same([ [['lu', 2], ['ou', 6], ['k', 10], ['k', 14]], [['l', 2], ['o', 6], ['k', 10], ['k', 14]], [['u', 3], ['u', 7], ['', 11], ['', 15]], ], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER)); +Assert::same([ + [['lu', 2], ['ou', 6], ['k', 10], ['k', 14]], + [['l', 2], ['o', 6], ['k', 10], ['k', 14]], + [['u', 3], ['u', 7], ['', 11], ['', 15]], +], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', captureOffset: true, patternOrder: true)); + Assert::same([['l'], ['k'], ['k']], Strings::matchAll('žluťoučký kůň', '#[e-l]+#u', 0, 2)); Assert::same([['ll', 'l']], Strings::matchAll('hello world!', '#[e-l]+#', PREG_PATTERN_ORDER, 2)); +Assert::same([['ll', 'l']], Strings::matchAll('hello world!', '#[e-l]+#', patternOrder: true, offset: 2)); Assert::same([], Strings::matchAll('hello world!', '', 0, 50)); diff --git a/tests/Utils/Strings.split().phpt b/tests/Utils/Strings.split().phpt index b291e9613..c621c85f9 100644 --- a/tests/Utils/Strings.split().phpt +++ b/tests/Utils/Strings.split().phpt @@ -29,6 +29,14 @@ Assert::same([ 'c', ], Strings::split('a, b, c', '#(,)\s*#', PREG_SPLIT_NO_EMPTY)); +Assert::same([ + 'a', + ',', + 'b', + ',', + 'c', +], Strings::split('a, b, c', '#(,)\s*#', noEmpty: true)); + Assert::same([ ['a', 0], [',', 1], @@ -36,3 +44,11 @@ Assert::same([ [',', 4], ['c', 6], ], Strings::split('a, b, c', '#(,)\s*#', PREG_SPLIT_OFFSET_CAPTURE)); + +Assert::same([ + ['a', 0], + [',', 1], + ['b', 3], + [',', 4], + ['c', 6], +], Strings::split('a, b, c', '#(,)\s*#', captureOffset: true));