diff --git a/src/std/text.ab b/src/std/text.ab index 0ad3bbba..6a94a4e9 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -142,8 +142,24 @@ pub fun char_at(text: Text, index: Num): Text { } /// Capitalize the first letter of the given `text`. +#[allow_absurd_cast] pub fun capitalize(text: Text): Text { - return trust $ echo "{text}" | sed "s/^\(.\)/\U\1/" $ + trust { + if len(text) == 0 { + return text + } + const bash_version = $ echo \"\$\{BASH_VERSINFO[0]}\" $ as Num + if bash_version >= 4 { + return $ echo \"\$\{text^}\" $ + } + // GNU sed supports \U + $ re='\bCopyright\b.+\bFree Software Foundation\b'; [[ \$(sed --version 2>/dev/null) =~ \$re ]] $ + if status == 0 { + return $ echo "{text}" | sed "s/^\(.\)/\U\1/" $ + } + const first_letter = upper(char_at(text, 0)) + return first_letter + slice(text, 1) + } } /// Pads `text` with the specified `pad` character on left until it reaches the desired `length`.