From a382cfbb17c9108f9ea68f98d5edbebaa19a9426 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 29 Jun 2024 07:04:59 +0200 Subject: [PATCH] normalize underscores in snake() --- AbstractUnicodeString.php | 2 +- ByteString.php | 2 +- Tests/AbstractAsciiTestCase.php | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/AbstractUnicodeString.php b/AbstractUnicodeString.php index 0f840f1..13ff716 100644 --- a/AbstractUnicodeString.php +++ b/AbstractUnicodeString.php @@ -367,7 +367,7 @@ public function reverse(): parent public function snake(): parent { $str = clone $this; - $str->string = str_replace(' ', '_', mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1 \2', $str->string), 'UTF-8')); + $str->string = preg_replace('/[ _]+/', '_', mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1 \2', $str->string), 'UTF-8')); return $str; } diff --git a/ByteString.php b/ByteString.php index 86887d7..e4ad7a6 100644 --- a/ByteString.php +++ b/ByteString.php @@ -367,7 +367,7 @@ public function slice(int $start = 0, ?int $length = null): parent public function snake(): parent { $str = clone $this; - $str->string = str_replace(' ', '_', strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1 \2', $str->string))); + $str->string = preg_replace('/[ _]+/', '_', strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1 \2', $str->string))); return $str; } diff --git a/Tests/AbstractAsciiTestCase.php b/Tests/AbstractAsciiTestCase.php index 132d558..42e8fda 100644 --- a/Tests/AbstractAsciiTestCase.php +++ b/Tests/AbstractAsciiTestCase.php @@ -1079,6 +1079,9 @@ public static function provideSnake() ['symfony', 'SYMFONY'], ['symfony_is_great', 'SYMFONY IS GREAT'], ['symfony_is_great', 'SYMFONY_IS_GREAT'], + ['symfony_is_great', 'symfony is great'], + ['symfony_is_great', 'SYMFONY IS GREAT'], + ['symfony_is_great', 'SYMFONY _ IS _ GREAT'], ]; }