8000 [String] Normalize underscores in snake() by xabbuh · Pull Request #57594 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[String] Normalize underscores in snake() #57594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
2 changes: 1 addition & 1 deletion src/Symfony/Component/String/AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/String/ByteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add a test case like
['symfony_is_great', 'SYMFONY _ IS _ GREAT'],

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, and this also revealed a bug in my patch

['symfony_is_great', 'SYMFONY _ IS _ GREAT'],
];
}

Expand Down
0