Skip to content

Commit

Permalink
Strings, Arrays: $flags replaced with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 26, 2021
1 parent c330f89 commit 57536d2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/Utils/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
45 changes: 33 additions & 12 deletions src/Utils/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 [];
}
Expand Down Expand Up @@ -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))) {
Expand Down
5 changes: 5 additions & 0 deletions tests/Utils/Arrays.grep().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));
1 change: 1 addition & 0 deletions tests/Utils/Strings.match().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
14 changes: 14 additions & 0 deletions tests/Utils/Strings.matchAll().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));
16 changes: 16 additions & 0 deletions tests/Utils/Strings.split().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,26 @@ 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],
['b', 3],
[',', 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));

0 comments on commit 57536d2

Please sign in to comment.