Skip to content

Commit 7ff1b18

Browse files
committed
titlecase: all non-letters are considered word-separators
This is to be consistent with `istitle`.
1 parent 637c623 commit 7ff1b18

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

base/deprecated.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ export hex2num
17091709
@deprecate diagm(A::SparseMatrixCSC) spdiagm(sparsevec(A))
17101710

17111711
# PR #23393
1712-
@deprecate titlecase(s::AbstractString) titlecase(s, false)
1712+
@deprecate titlecase(s::AbstractString) titlecase(s, false, true)
17131713

17141714
# END 0.7 deprecations
17151715

base/strings/basic.jl

+11-5
Original file line numberDiff line numberDiff line change
@@ -423,17 +423,23 @@ julia> titlecase("ISS - international space station", false)
423423
"ISS - International Space Station"
424424
```
425425
"""
426-
function titlecase(s::AbstractString, strict::Bool)
426+
function titlecase(s::AbstractString, strict::Bool, compat=false)
427427
startword = true
428428
b = IOBuffer()
429429
for c in s
430-
if isspace(c)
430+
if compat
431+
if isspace(c)
432+
print(b, c)
433+
startword = true
434+
continue
435+
end
436+
elseif !iscased(c)
431437
print(b, c)
432438
startword = true
433-
else
434-
print(b, startword ? titlecase(c) : strict ? lowercase(c) : c)
435-
startword = false
439+
continue
436440
end
441+
print(b, startword ? titlecase(c) : strict ? lowercase(c) : c)
442+
startword = false
437443
end
438444
return String(take!(b))
439445
end

base/strings/utf8proc.jl

+14-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module UTF8proc
55

66
import Base: show, ==, hash, string, Symbol, isless, length, eltype, start, next, done, convert, isvalid, lowercase, uppercase, titlecase
77

8-
export isgraphemebreak, category_code, category_abbrev, category_string
8+
export isgraphemebreak, category_code, category_abbrev, category_string, iscased
99

1010
# also exported by Base:
1111
export normalize_string, graphemes, is_assigned_char, charwidth, isvalid,
@@ -333,6 +333,19 @@ false
333333
"""
334334
isalpha(c::Char) = (UTF8PROC_CATEGORY_LU <= category_code(c) <= UTF8PROC_CATEGORY_LO)
335335

336+
"""
337+
iscased(c::Char) -> Bool
338+
339+
Tests whether a character is cased, i.e. is lower-, upper- or title-cased.
340+
"""
341+
function iscased(c::Char)
342+
ccode = category_code(c)
343+
return ccode == UTF8PROC_CATEGORY_LU ||
344+
ccode == UTF8PROC_CATEGORY_LT ||
345+
ccode == UTF8PROC_CATEGORY_LL
346+
end
347+
348+
336349
"""
337350
isnumber(c::Char) -> Bool
338351

0 commit comments

Comments
 (0)