Skip to content

Commit

Permalink
Fix capitalize for single codepoint (#13268)
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 19, 2024
1 parent c35edd1 commit ad778a6
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/elixir/lib/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ defmodule String do

def capitalize(string, mode) when is_binary(string) do
case :unicode_util.gc(string) do
[gc] -> grapheme_to_binary(:string.titlecase([gc]))
[gc, rest] -> grapheme_to_binary(:string.titlecase([gc])) <> downcase(rest, mode)
[gc | rest] -> grapheme_to_binary(:string.titlecase([gc])) <> downcase(rest, mode)
[] -> ""
Expand Down
1 change: 1 addition & 0 deletions lib/elixir/test/elixir/string_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ defmodule StringTest do

test "capitalize/1" do
assert String.capitalize("") == ""
assert String.capitalize("1") == "1"
assert String.capitalize("abc") == "Abc"
assert String.capitalize("ABC") == "Abc"
assert String.capitalize("c b a") == "C b a"
Expand Down

0 comments on commit ad778a6

Please sign in to comment.