Skip to content

Commit

Permalink
titlecase: all non-letters are considered word-separators
Browse files Browse the repository at this point in the history
This is to be consistent with `istitle`.
  • Loading branch information
rfourquet committed Aug 22, 2017
1 parent 637c623 commit d12bf25
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,7 @@ export hex2num
@deprecate diagm(A::SparseMatrixCSC) spdiagm(sparsevec(A))

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

# END 0.7 deprecations

Expand Down
16 changes: 11 additions & 5 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -423,17 +423,23 @@ julia> titlecase("ISS - international space station", false)
"ISS - International Space Station"
```
"""
function titlecase(s::AbstractString, strict::Bool)
function titlecase(s::AbstractString, strict::Bool, compat=false)
startword = true
b = IOBuffer()
for c in s
if isspace(c)
if compat
if isspace(c)
print(b, c)
startword = true
continue
end
elseif !iscased(c)
print(b, c)
startword = true
else
print(b, startword ? titlecase(c) : strict ? lowercase(c) : c)
startword = false
continue
end
print(b, startword ? titlecase(c) : strict ? lowercase(c) : c)
startword = false
end
return String(take!(b))
end
Expand Down

0 comments on commit d12bf25

Please sign in to comment.