Skip to content

Commit

Permalink
[Str] add after and before helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Jan 17, 2021
1 parent 1f567ab commit f2b0fba
Show file tree
Hide file tree
Showing 39 changed files with 1,262 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Psl/Internal/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ final class Loader
'Psl\Str\Byte\search',
'Psl\Str\Byte\search_ci',
'Psl\Str\Byte\search_last',
'Psl\Str\Byte\search_last_ci',
'Psl\Str\Byte\shuffle',
'Psl\Str\Byte\slice',
'Psl\Str\Byte\splice',
Expand All @@ -229,6 +230,14 @@ final class Loader
'Psl\Str\Byte\uppercase',
'Psl\Str\Byte\words',
'Psl\Str\Byte\wrap',
'Psl\Str\Byte\after',
'Psl\Str\Byte\after_ci',
'Psl\Str\Byte\after_last',
'Psl\Str\Byte\after_last_ci',
'Psl\Str\Byte\before',
'Psl\Str\Byte\before_ci',
'Psl\Str\Byte\before_last',
'Psl\Str\Byte\before_last_ci',
'Psl\Str\capitalize',
'Psl\Str\capitalize_words',
'Psl\Str\chr',
Expand Down Expand Up @@ -278,6 +287,14 @@ final class Loader
'Psl\Str\uppercase',
'Psl\Str\width',
'Psl\Str\wrap',
'Psl\Str\after',
'Psl\Str\after_ci',
'Psl\Str\after_last',
'Psl\Str\after_last_ci',
'Psl\Str\before',
'Psl\Str\before_ci',
'Psl\Str\before_last',
'Psl\Str\before_last_ci',
'Psl\invariant',
'Psl\invariant_violation',
'Psl\sequence',
Expand Down Expand Up @@ -353,6 +370,14 @@ final class Loader
'Psl\Str\Grapheme\starts_with_ci',
'Psl\Str\Grapheme\strip_prefix',
'Psl\Str\Grapheme\strip_suffix',
'Psl\Str\Grapheme\after',
'Psl\Str\Grapheme\after_ci',
'Psl\Str\Grapheme\after_last',
'Psl\Str\Grapheme\after_last_ci',
'Psl\Str\Grapheme\before',
'Psl\Str\Grapheme\before_ci',
'Psl\Str\Grapheme\before_last',
'Psl\Str\Grapheme\before_last_ci',
];

public const INTERFACES = [
Expand Down
24 changes: 24 additions & 0 deletions src/Psl/Str/Byte/after.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Byte;

use Psl;

/**
* @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds.
*
* @psalm-pure
*/
function after(string $haystack, string $needle, int $offset = 0): ?string
{
$offset = search($haystack, $needle, $offset);
if (null === $offset) {
return null;
}

$offset += length($needle);

return slice($haystack, $offset, null);
}
27 changes: 27 additions & 0 deletions src/Psl/Str/Byte/after_ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Byte;

use Psl;

/**
* @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds.
*
* @psalm-pure
*/
function after_ci(
string $haystack,
string $needle,
int $offset = 0
): ?string {
$offset = search_ci($haystack, $needle, $offset);
if (null === $offset) {
return null;
}

$offset += length($needle);

return slice($haystack, $offset, null);
}
28 changes: 28 additions & 0 deletions src/Psl/Str/Byte/after_last.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Byte;

use Psl;

/**
* @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds.
* @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided.
*
* @psalm-pure
*/
function after_last(
string $haystack,
string $needle,
int $offset = 0
): ?string {
$position = search_last($haystack, $needle, $offset);
if (null === $position) {
return null;
}

$position += length($needle);

return slice($haystack, $position);
}
27 changes: 27 additions & 0 deletions src/Psl/Str/Byte/after_last_ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Byte;

use Psl;

/**
* @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds.
*
* @psalm-pure
*/
function after_last_ci(
string $haystack,
string $needle,
int $offset = 0
): ?string {
$position = search_last_ci($haystack, $needle, $offset);
if (null === $position) {
return null;
}

$position += length($needle);

return slice($haystack, $position);
}
27 changes: 27 additions & 0 deletions src/Psl/Str/Byte/before.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Byte;

use Psl;

/**
* @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds.
*
* @psalm-pure
*/
function before(
string $haystack,
string $needle,
int $offset = 0
): ?string {
$length = search($haystack, $needle, $offset);
if (null === $length) {
return null;
}

$length += length($needle);

return slice($haystack, 0, $length);
}
27 changes: 27 additions & 0 deletions src/Psl/Str/Byte/before_ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Byte;

use Psl;

/**
* @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds.
*
* @psalm-pure
*/
function before_ci(
string $haystack,
string $needle,
int $offset = 0
): ?string {
$length = search_ci($haystack, $needle, $offset);
if (null === $length) {
return null;
}

$length += length($needle);

return slice($haystack, 0, $length);
}
27 changes: 27 additions & 0 deletions src/Psl/Str/Byte/before_last.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Byte;

use Psl;

/**
* @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds.
*
* @psalm-pure
*/
function before_last(
string $haystack,
string $needle,
int $offset = 0
): ?string {
$length = search_last($haystack, $needle, $offset);
if (null === $length) {
return null;
}

$length += length($needle);

return slice($haystack, 0, $length);
}
27 changes: 27 additions & 0 deletions src/Psl/Str/Byte/before_last_ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Byte;

use Psl;

/**
* @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds.
*
* @psalm-pure
*/
function before_last_ci(
string $haystack,
string $needle,
int $offset = 0
): ?string {
$length = search_last_ci($haystack, $needle, $offset);
if (null === $length) {
return null;
}

$length += length($needle);

return slice($haystack, 0, $length);
}
4 changes: 4 additions & 0 deletions src/Psl/Str/Byte/search_last.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
*/
function search_last(string $haystack, string $needle, int $offset = 0): ?int
{
if ('' === $needle) {
return null;
}

$haystack_length = length($haystack);
Psl\invariant($offset >= -$haystack_length && $offset <= $haystack_length, 'Offset is out-of-bounds.');

Expand Down
32 changes: 32 additions & 0 deletions src/Psl/Str/Byte/search_last_ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Byte;

use Psl;

/**
* Returns the last position of the 'needle' string in the 'haystack' string,
* or null if it isn't found (case-insensitive).
*
* An optional offset determines where in the haystack (from the beginning) the
* search begins. If the offset is negative, the search will begin that many
* characters from the end of the string and go backwards. If the offset is
* out-of-bounds, an InvariantViolationException will be thrown.
*
* @psalm-pure
*
* @throws Psl\Exception\InvariantViolationException If $offset is out-of-bounds.
*/
function search_last_ci(string $haystack, string $needle, int $offset = 0): ?int
{
if ('' === $needle) {
return null;
}

$haystack_length = length($haystack);
Psl\invariant($offset >= -$haystack_length && $offset <= $haystack_length, 'Offset is out-of-bounds.');

return false === ($pos = strripos($haystack, $needle, $offset)) ? null : $pos;
}
24 changes: 24 additions & 0 deletions src/Psl/Str/Grapheme/after.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Grapheme;

use Psl;

/**
* @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds.
*
* @psalm-pure
*/
function after(string $haystack, string $needle, int $offset = 0): ?string
{
$offset = search($haystack, $needle, $offset);
if (null === $offset) {
return null;
}

$offset += length($needle);

return slice($haystack, $offset, null);
}
27 changes: 27 additions & 0 deletions src/Psl/Str/Grapheme/after_ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Grapheme;

use Psl;

/**
* @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds.
*
* @psalm-pure
*/
function after_ci(
string $haystack,
string $needle,
int $offset = 0
): ?string {
$offset = search_ci($haystack, $needle, $offset);
if (null === $offset) {
return null;
}

$offset += length($needle);

return slice($haystack, $offset, null);
}
Loading

0 comments on commit f2b0fba

Please sign in to comment.