Skip to content

Commit

Permalink
Use the bash native capitalizing method if available, and fallback to…
Browse files Browse the repository at this point in the history
… other functions (#620)

Co-authored-by: hdwalters <[email protected]>
  • Loading branch information
lens0021 and hdwalters authored Nov 28, 2024
1 parent cba1671 commit 05caef9
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/std/text.ab
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit 05caef9

Please sign in to comment.