-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add str_increment and str_decrement functions
- Loading branch information
Showing
3 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
namespace Symfony\Polyfill\Php83; | ||
|
||
/** | ||
* @author Ion Bazan <[email protected]> | ||
* @author Ion Bazan <[email protected]>, Pierre Ambroise <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
|
@@ -82,4 +82,42 @@ public static function mb_str_pad(string $string, int $length, string $pad_strin | |
return mb_substr(str_repeat($pad_string, $leftPaddingLength), 0, $leftPaddingLength, $encoding).$string.mb_substr(str_repeat($pad_string, $rightPaddingLength), 0, $rightPaddingLength, $encoding); | ||
} | ||
} | ||
|
||
public static function str_increment(string $string): string | ||
{ | ||
if ('' === $string) { | ||
throw new \ValueError('str_increment(): Argument #1 ($string) cannot be empty'); | ||
} | ||
|
||
if (1 === preg_match('/[^\x00-\x7F]/', $string)) { | ||
throw new \ValueError('str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters'); | ||
} | ||
|
||
return ++$string; | ||
} | ||
|
||
public static function str_decrement(string $string): string | ||
{ | ||
if ('' === $string) { | ||
throw new \ValueError('str_decrement(): Argument #1 ($string) cannot be empty'); | ||
} | ||
|
||
if (1 === preg_match('/[^\x00-\x7F]/', $string)) { | ||
throw new \ValueError('str_decrement(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters'); | ||
} | ||
|
||
if (1 === preg_match('/^A|0+$/', $string)) { | ||
throw new \ValueError('str_decrement(): Argument #1 ($string) "A" is out of decrement range'); | ||
} | ||
|
||
if (!in_array(substr($string, -1), ['A', '0'])){ | ||
return join('', array_slice(str_split($string), 0, -1)) . chr(ord(substr($string, -1)) - 1); | ||
} | ||
|
||
if (strlen($string) === 1) { | ||
return ''; | ||
} | ||
|
||
return self::str_decrement(substr($string, 0, -1)) . 'Z'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters