@@ -859,15 +859,53 @@ defmodule String do
859
859
def capitalize ( string , mode \\ :default )
860
860
861
861
def capitalize ( << char , rest :: binary >> , :ascii ) do
862
- char = if char >= ?a and char <= ?z , do: char - 32 , else: char
863
- << char >> <> downcase ( rest , :ascii )
862
+ << capitalize_ascii_char ( char ) >> <> downcase ( rest , :ascii )
864
863
end
865
864
866
865
def capitalize ( string , mode ) when is_binary ( string ) do
867
866
{ char , rest } = String.Casing . titlecase_once ( string , mode )
868
867
char <> downcase ( rest , mode )
869
868
end
870
869
870
+ defp capitalize_ascii_char ( char ) when char >= ?a and char <= ?z , do: char - 32
871
+ defp capitalize_ascii_char ( char ) , do: char
872
+
873
+ @ doc """
874
+ Converts only the first character in the given string to
875
+ uppercase according to `mode`.
876
+
877
+ `mode` may be `:default`, `:ascii` or `:greek`. The `:default` mode considers
878
+ all non-conditional transformations outlined in the Unicode standard. `:ascii`
879
+ lowercases only the letters A to Z. `:greek` includes the context sensitive
880
+ mappings found in Greek.
881
+
882
+ ## Examples
883
+
884
+ iex> String.titlecase("abcd")
885
+ "Abcd"
886
+
887
+ iex> String.titlecase("ABCd")
888
+ "ABCd"
889
+
890
+ iex> String.titlecase("fin")
891
+ "Fin"
892
+
893
+ iex> String.titlecase("olá")
894
+ "Olá"
895
+
896
+ """
897
+ @ spec titlecase ( t , :default | :ascii | :greek ) :: t
898
+ def titlecase ( string , mode \\ :default )
899
+
900
+ def titlecase ( << char , rest :: binary >> , :ascii ) do
901
+ << capitalize_ascii_char ( char ) >> <> rest
902
+ end
903
+
904
+ def titlecase ( string , mode ) when is_binary ( string ) do
905
+ { char , rest } = String.Casing . titlecase_once ( string , mode )
906
+ char <> rest
907
+ end
908
+
871
909
@ doc false
872
910
@ deprecated "Use String.trim_trailing/1 instead"
873
911
defdelegate rstrip ( binary ) , to: String.Break , as: :trim_trailing
0 commit comments