From ad778a6f2990ebbc7f1c1a2326524135a6170c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 19 Jan 2024 19:42:55 +0100 Subject: [PATCH] Fix capitalize for single codepoint (#13268) --- lib/elixir/lib/string.ex | 1 + lib/elixir/test/elixir/string_test.exs | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/elixir/lib/string.ex b/lib/elixir/lib/string.ex index 1966c533409..3945f681fbe 100644 --- a/lib/elixir/lib/string.ex +++ b/lib/elixir/lib/string.ex @@ -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) [] -> "" diff --git a/lib/elixir/test/elixir/string_test.exs b/lib/elixir/test/elixir/string_test.exs index 8a411eb78d1..6aa5f622595 100644 --- a/lib/elixir/test/elixir/string_test.exs +++ b/lib/elixir/test/elixir/string_test.exs @@ -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"