Skip to content

Commit

Permalink
titlecase: chars not starting a word can be converted to lowercase
Browse files Browse the repository at this point in the history
* A second argument `strict` is added to `titlecase` to control
  whether to convert those chars to lowercase.
  This is useful e.g. for #23379.
* The one-arg version is deprecated, and will be equivalent to
  the new behavior (`strict=true`) in the future.
  This is to be compatible with the `istitle` function, so that
  `istitle(titlecase(s)) == true` when `s` has at least 1 letter.
  This is also how some languages (e.g. python) implement it, and
  is compatible with http://www.unicode.org/L2/L1999/99190.htm.
  • Loading branch information
rfourquet committed Aug 22, 2017
1 parent 1db7b8f commit 637c623
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,14 @@ Deprecated or removed
* `diagm(A::SparseMatrixCSC)` has been deprecated in favor of
`spdiagm(sparsevec(A))` ([#23341]).

* `titlecase(s)` is deprecated in favor of `titlecase(s, #=strict=# false)`,
which means to preserve uppercase characters; in the future, the second
argument will default to true, i.e. every character not starting a word
will be converted to lowercase.
This is to be consistent with the function `istitle` (so that
`istitle(titlecase(s))` is always true if `s` contains at least one letter),
and with other languages.

Command-line option changes
---------------------------

Expand Down
3 changes: 3 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,9 @@ export hex2num
# PR 23341
@deprecate diagm(A::SparseMatrixCSC) spdiagm(sparsevec(A))

# PR #23393
@deprecate titlecase(s::AbstractString) titlecase(s, false)

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
15 changes: 10 additions & 5 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -406,27 +406,32 @@ julia> lowercase("STRINGS AND THINGS")
lowercase(s::AbstractString) = map(lowercase, s)

"""
titlecase(s::AbstractString)
titlecase(s::AbstractString, strict::Bool)
Capitalizes the first character of each word in `s`.
Capitalizes the first character of each word in `s`;
if `strict` is true, every other character is
converted to lowercase, otherwise they are left unchanged.
See also [`ucfirst`](@ref) to capitalize only the first
character in `s`.
# Examples
```jldoctest
julia> titlecase("the julia programming language")
julia> titlecase("the JULIA programming language", true)
"The Julia Programming Language"
julia> titlecase("ISS - international space station", false)
"ISS - International Space Station"
```
"""
function titlecase(s::AbstractString)
function titlecase(s::AbstractString, strict::Bool)
startword = true
b = IOBuffer()
for c in s
if isspace(c)
print(b, c)
startword = true
else
print(b, startword ? titlecase(c) : c)
print(b, startword ? titlecase(c) : strict ? lowercase(c) : c)
startword = false
end
end
Expand Down
10 changes: 6 additions & 4 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,12 @@ end
@test ucfirst(GenericString("A")) == "A"

# titlecase
@test titlecase('lj') == 'Lj'
@test titlecase("ljubljana") == "Ljubljana"
@test titlecase("aBc ABC") == "ABc ABC"
@test titlecase("abcD EFG\n\thij") == "AbcD EFG\n\tHij"
@test titlecase('lj', true) == 'Lj'
@test titlecase("ljubljana", true) == "Ljubljana"
@test titlecase("aBc ABC", true) == "ABc ABC"
@test titlecase("aBc ABC", false) == "Abc Abc"
@test titlecase("abcD EFG\n\thij", true) == "AbcD EFG\n\tHij"
@test titlecase("abcD EFG\n\thij", false) == "Abcd Efg\n\tHij"

# issue # 11464: uppercase/lowercase of GenericString becomes a String
str = "abcdef\uff\uffff\u10ffffABCDEF"
Expand Down

0 comments on commit 637c623

Please sign in to comment.