Skip to content

Commit

Permalink
Merge branch '3.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Mar 27, 2024
2 parents 9fc04f4 + 9f9d248 commit 2f5ccb5
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 32 deletions.
22 changes: 0 additions & 22 deletions src/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,4 @@ public static function ucsplit($string)
return $substrings;
}

/**
* Extracts a section of a string and returns it as a new string, without
* modifying the original string. If $end is omitted, the function extracts
* the remaining string. If $end is negative, it is computed from the end
* of the string.
*
* @param int $start Initial index from which to begin extraction.
* @param int|null $end Index at which to end extraction.
* @return string
*/
public static function str_slice(string $str, int $start, ?int $end = null)
{
$length = $end - $start;

if ($end === null) {
$length = \mb_strlen($str);
} elseif ($end >= 0 && $end <= $start) {
return '';
}

return \mb_substr($str, $start, $length);
}
}
10 changes: 0 additions & 10 deletions tests/StringSupportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,4 @@ public function testUcsplitMethod()
$this->assertSame(['foo', 'B', 'A', 'R', 'Baz'], Str::ucsplit('fooBARBaz'));
$this->assertSame(['Foo-ba', 'R-baz'], Str::ucsplit('Foo-baR-baz'));
}

public function testStrSliceMethod()
{
$str = 'The quick brown fox jumps over the lazy dog.';

$this->assertSame('the lazy dog.', Str::str_slice($str, 31));
$this->assertSame('quick brown fox', Str::str_slice($str, 4, 19));
$this->assertSame('dog.', Str::str_slice($str, -4));
$this->assertSame('lazy', Str::str_slice($str, -9, -5));
}
}

0 comments on commit 2f5ccb5

Please sign in to comment.