Skip to content

Commit

Permalink
Strings::before(), after(), indexOf() and pos() return NULL instead o…
Browse files Browse the repository at this point in the history
…f FALSE if the needle was not found (BC break)
  • Loading branch information
dg committed Feb 1, 2017
1 parent fcad715 commit 29ba4cb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
24 changes: 12 additions & 12 deletions src/Utils/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,51 +353,51 @@ public static function reverse(string $s): string

/**
* Returns part of $haystack before $nth occurence of $needle (negative value means searching from the end).
* @return string|FALSE returns FALSE if the needle was not found
* @return string|NULL returns NULL if the needle was not found
*/
public static function before(string $haystack, string $needle, int $nth = 1)
{
$pos = self::pos($haystack, $needle, $nth);
return $pos === FALSE
? FALSE
return $pos === NULL
? NULL
: substr($haystack, 0, $pos);
}


/**
* Returns part of $haystack after $nth occurence of $needle (negative value means searching from the end).
* @return string|FALSE returns FALSE if the needle was not found
* @return string|NULL returns NULL if the needle was not found
*/
public static function after(string $haystack, string $needle, int $nth = 1)
{
$pos = self::pos($haystack, $needle, $nth);
return $pos === FALSE
? FALSE
return $pos === NULL
? NULL
: substr($haystack, $pos + strlen($needle));
}


/**
* Returns position of $nth occurence of $needle in $haystack (negative value means searching from the end).
* @return int|FALSE offset in characters or FALSE if the needle was not found
* @return int|NULL offset in characters or NULL if the needle was not found
*/
public static function indexOf(string $haystack, string $needle, int $nth = 1)
{
$pos = self::pos($haystack, $needle, $nth);
return $pos === FALSE
? FALSE
return $pos === NULL
? NULL
: self::length(substr($haystack, 0, $pos));
}


/**
* Returns position of $nth occurence of $needle in $haystack.
* @return int|FALSE offset in bytes or FALSE if the needle was not found
* @return int|NULL offset in bytes or NULL if the needle was not found
*/
private static function pos(string $haystack, string $needle, int $nth = 1)
{
if (!$nth) {
return FALSE;
return NULL;
} elseif ($nth > 0) {
if (strlen($needle) === 0) {
return 0;
Expand All @@ -416,7 +416,7 @@ private static function pos(string $haystack, string $needle, int $nth = 1)
$pos--;
}
}
return $pos;
return $pos === FALSE ? NULL : $pos;
}


Expand Down
8 changes: 4 additions & 4 deletions tests/Utils/Strings.after().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ test(function () {
Assert::same('c', Strings::after($foo, '789', 3));
Assert::same('a123456789b123456789c', Strings::after($foo, '9', -3));
Assert::same('a123456789b123456789c', Strings::after($foo, '789', -3));
Assert::false(Strings::after($foo, '9', 0));
Assert::false(Strings::after($foo, 'not-in-string'));
Assert::false(Strings::after($foo, 'b', -2));
Assert::false(Strings::after($foo, 'b', 2));
Assert::null(Strings::after($foo, '9', 0));
Assert::null(Strings::after($foo, 'not-in-string'));
Assert::null(Strings::after($foo, 'b', -2));
Assert::null(Strings::after($foo, 'b', 2));
});


Expand Down
8 changes: 4 additions & 4 deletions tests/Utils/Strings.before().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ test(function () {
Assert::same('0123456789a123456789b123456', Strings::before($foo, '789', 3));
Assert::same('012345678', Strings::before($foo, '9', -3));
Assert::same('0123456', Strings::before($foo, '789', -3));
Assert::false(Strings::before($foo, '9', 0));
Assert::false(Strings::before($foo, 'not-in-string'));
Assert::false(Strings::before($foo, 'b', -2));
Assert::false(Strings::before($foo, 'b', 2));
Assert::null(Strings::before($foo, '9', 0));
Assert::null(Strings::before($foo, 'not-in-string'));
Assert::null(Strings::before($foo, 'b', -2));
Assert::null(Strings::before($foo, 'b', 2));
});


Expand Down
8 changes: 4 additions & 4 deletions tests/Utils/Strings.indexOf().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ test(function () {
Assert::same(27, Strings::indexOf($foo, '789', 3));
Assert::same(9, Strings::indexOf($foo, '9', -3));
Assert::same(7, Strings::indexOf($foo, '789', -3));
Assert::false(Strings::indexOf($foo, '9', 0));
Assert::false(Strings::indexOf($foo, 'not-in-string'));
Assert::false(Strings::indexOf($foo, 'b', -2));
Assert::false(Strings::indexOf($foo, 'b', 2));
Assert::null(Strings::indexOf($foo, '9', 0));
Assert::null(Strings::indexOf($foo, 'not-in-string'));
Assert::null(Strings::indexOf($foo, 'b', -2));
Assert::null(Strings::indexOf($foo, 'b', 2));
});


Expand Down

0 comments on commit 29ba4cb

Please sign in to comment.