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 Nov 8, 2020
1 parent 2bef63d commit e4148a2
Show file tree
Hide file tree
Showing 28 changed files with 893 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
26 changes: 26 additions & 0 deletions src/Psl/Str/Byte/after.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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, bool $include_needle = false, int $offset = 0): string
{
$offset = search($haystack, $needle, $offset);
if (null === $offset) {
return $haystack;
}

if (!$include_needle) {
$offset += length($needle);
}

return slice($haystack, $offset, null);
}
30 changes: 30 additions & 0 deletions src/Psl/Str/Byte/after_ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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,
bool $include_needle = false,
int $offset = 0
): string {
$offset = search_ci($haystack, $needle, $offset);
if (null === $offset) {
return $haystack;
}

if (!$include_needle) {
$offset += length($needle);
}

return slice($haystack, $offset, null);
}
31 changes: 31 additions & 0 deletions src/Psl/Str/Byte/after_last.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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,
bool $include_needle = false,
int $offset = 0
): string {
$offset = search_last($haystack, $needle, $offset);
if (null === $offset) {
return $haystack;
}

if (!$include_needle) {
$offset += length($needle);
}

return slice($haystack, $offset, null);
}
30 changes: 30 additions & 0 deletions src/Psl/Str/Byte/after_last_ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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,
bool $include_needle = false,
int $offset = 0
): string {
$offset = search_last_ci($haystack, $needle, $offset);
if (null === $offset) {
return $haystack;
}

if (!$include_needle) {
$offset += length($needle);
}

return slice($haystack, $offset, null);
}
30 changes: 30 additions & 0 deletions src/Psl/Str/Byte/before.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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,
bool $include_needle = false,
int $offset = 0
): string {
$length = search($haystack, $needle, $offset);
if (null === $length) {
return $haystack;
}

if (!$include_needle) {
$length += length($needle);
}

return slice($haystack, 0, $length);
}
30 changes: 30 additions & 0 deletions src/Psl/Str/Byte/before_ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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,
bool $include_needle = false,
int $offset = 0
): string {
$length = search_ci($haystack, $needle, $offset);
if (null === $length) {
return $haystack;
}

if (!$include_needle) {
$length += length($needle);
}

return slice($haystack, 0, $length);
}
30 changes: 30 additions & 0 deletions src/Psl/Str/Byte/before_last.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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,
bool $include_needle = false,
int $offset = 0
): string {
$length = search_last($haystack, $needle, $offset);
if (null === $length) {
return $haystack;
}

if (!$include_needle) {
$length += length($needle);
}

return slice($haystack, 0, $length);
}
30 changes: 30 additions & 0 deletions src/Psl/Str/Byte/before_last_ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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,
bool $include_needle = false,
int $offset = 0
): string {
$length = search_last_ci($haystack, $needle, $offset);
if (null === $length) {
return $haystack;
}

if (!$include_needle) {
$length += length($needle);
}

return slice($haystack, 0, $length);
}
28 changes: 28 additions & 0 deletions src/Psl/Str/Byte/search_last_ci.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;

/**
* 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
{
$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;
}
26 changes: 26 additions & 0 deletions src/Psl/Str/Grapheme/after.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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, bool $include_needle = false, int $offset = 0): string
{
$offset = search($haystack, $needle, $offset);
if (null === $offset) {
return $haystack;
}

if (!$include_needle) {
$offset += length($needle);
}

return slice($haystack, $offset, null);
}
30 changes: 30 additions & 0 deletions src/Psl/Str/Grapheme/after_ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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,
bool $include_needle = false,
int $offset = 0
): string {
$offset = search_ci($haystack, $needle, $offset);
if (null === $offset) {
return $haystack;
}

if (!$include_needle) {
$offset += length($needle);
}

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

0 comments on commit e4148a2

Please sign in to comment.