Skip to content
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

Use the bash native capitalizing method if available, and fallback to other functions #620

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Changes from 3 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
20 changes: 19 additions & 1 deletion src/std/text.ab
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,26 @@ 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 {
hdwalters marked this conversation as resolved.
Show resolved Hide resolved
return trust $ echo "{text}" | sed "s/^\(.\)/\U\1/" $
trust {
if len(text) == 0: return text
const bash_version = $ echo \"\$\{BASH_VERSINFO[0]}\" $ as Num
hdwalters marked this conversation as resolved.
Show resolved Hide resolved
if {
bash_version >= 4 {
return $ echo \"\$\{text^}\" $
} else {
// 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/" $
else {
let 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`.
Expand Down