[Str][Byte] simplify reverse implementation (#160)

This commit is contained in:
Nat Zimmermann 2021-03-21 01:22:54 +00:00 committed by GitHub
parent 3830bc0d26
commit 5482786dac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 11 deletions

View File

@ -39,7 +39,7 @@
- [replace_ci](./../../src/Psl/Str/Byte/replace_ci.php#L15) - [replace_ci](./../../src/Psl/Str/Byte/replace_ci.php#L15)
- [replace_every](./../../src/Psl/Str/Byte/replace_every.php#L17) - [replace_every](./../../src/Psl/Str/Byte/replace_every.php#L17)
- [replace_every_ci](./../../src/Psl/Str/Byte/replace_every_ci.php#L17) - [replace_every_ci](./../../src/Psl/Str/Byte/replace_every_ci.php#L17)
- [reverse](./../../src/Psl/Str/Byte/reverse.php#L10) - [reverse](./../../src/Psl/Str/Byte/reverse.php#L14)
- [rot13](./../../src/Psl/Str/Byte/rot13.php#L14) - [rot13](./../../src/Psl/Str/Byte/rot13.php#L14)
- [search](./../../src/Psl/Str/Byte/search.php#L23) - [search](./../../src/Psl/Str/Byte/search.php#L23)
- [search_ci](./../../src/Psl/Str/Byte/search_ci.php#L23) - [search_ci](./../../src/Psl/Str/Byte/search_ci.php#L23)

View File

@ -4,19 +4,14 @@ declare(strict_types=1);
namespace Psl\Str\Byte; namespace Psl\Str\Byte;
use function strrev;
/** /**
* Reverses the string.
*
* @pure * @pure
*/ */
function reverse(string $string): string function reverse(string $string): string
{ {
$lo = 0; return strrev($string);
$hi = namespace\length($string) - 1;
for (; $lo < $hi; $lo++, $hi--) {
$temp = $string[$lo];
$string[$lo] = $string[$hi];
$string[$hi] = $temp;
}
return $string;
} }