Skip to content

Commit 7695d5c

Browse files
omusararslan
authored andcommitted
Add Compat.Unicode for standard library Unicode (#432)
* Add Compat.Unicode for standard library Unicode * Define titlecase only on Julia 0.5 * Remove normalize and isassigned from exports * Add textwidth deprecation
1 parent 1aa2b90 commit 7695d5c

File tree

4 files changed

+71
-20
lines changed

4 files changed

+71
-20
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ Currently, the `@compat` macro supports the following syntaxes:
7979
`CartesianRange` now has two type parameters, so using them as
8080
fields in other `struct`s requires manual intervention.
8181

82+
## Module Aliases
83+
84+
* In 0.6, some 0.5 iterator functions have been moved to the `Base.Iterators`
85+
module. Code can be written to work on both 0.5 and 0.6 by `import`ing or
86+
`using` the `Compat.Iterators` module instead. ([#18839])
87+
8288
* `using Compat.Test`, `using Compat.SharedArrays`, `using Compat.Mmap`, and `using
8389
Compat.DelimitedFiles` are provided on versions older than 0.7, where these are not yet
8490
part of the standard library. ([#23931])
@@ -89,11 +95,8 @@ Currently, the `@compat` macro supports the following syntaxes:
8995
* `using Compat.Dates` is provided on versions older than 0.7, where this library is not
9096
yet a part of the standard library. ([#24459])
9197

92-
## Module Aliases
93-
94-
* In 0.6, some 0.5 iterator functions have been moved to the `Base.Iterators`
95-
module. Code can be written to work on both 0.5 and 0.6 by `import`ing or
96-
`using` the `Compat.Iterators` module instead. ([#18839])
98+
* `using Compat.Unicode` is provided on versions older than 0.7, where this library is not
99+
yet a part of the standard library. ([#25021])
97100

98101
## New functions, macros, and methods
99102

@@ -238,8 +241,6 @@ Currently, the `@compat` macro supports the following syntaxes:
238241

239242
* `IntSet` is now `BitSet` ([#24282])
240243

241-
* `strwidth` and `charwidth` are now merged into `textwidth` ([#23667]).
242-
243244
* `Complex32`, `Complex64`, and `Complex128` are now `ComplexF16`, `ComplexF32`, and
244245
`ComplexF64`, respectively ([#24647]).
245246

@@ -378,7 +379,6 @@ includes this fix. Find the minimum version from there.
378379
[#23427]: https://github.com/JuliaLang/julia/issues/23427
379380
[#23570]: https://github.com/JuliaLang/julia/issues/23570
380381
[#23666]: https://github.com/JuliaLang/julia/issues/23666
381-
[#23667]: https://github.com/JuliaLang/julia/issues/23667
382382
[#23757]: https://github.com/JuliaLang/julia/issues/23757
383383
[#23812]: https://github.com/JuliaLang/julia/issues/23812
384384
[#23931]: https://github.com/JuliaLang/julia/issues/23931
@@ -392,3 +392,4 @@ includes this fix. Find the minimum version from there.
392392
[#24652]: https://github.com/JuliaLang/julia/issues/24652
393393
[#24657]: https://github.com/JuliaLang/julia/issues/24657
394394
[#24785]: https://github.com/JuliaLang/julia/issues/24785
395+
[#25021]: https://github.com/JuliaLang/julia/issues/25021

src/Compat.jl

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -797,13 +797,6 @@ end
797797
export BitSet
798798
end
799799

800-
# 0.7.0-DEV.1930
801-
@static if !isdefined(Base, :textwidth)
802-
textwidth(c::Char) = charwidth(c)
803-
textwidth(c::AbstractString) = strwidth(c)
804-
export textwidth
805-
end
806-
807800
# 0.7.0-DEV.2116
808801
@static if VERSION < v"0.7.0-DEV.2116"
809802
import Base: spdiagm
@@ -919,6 +912,47 @@ end
919912
export ComplexF64
920913
end
921914

915+
# 0.7.0-DEV.2915
916+
module Unicode
917+
export graphemes, textwidth, isvalid,
918+
islower, isupper, isalpha, isdigit, isxdigit, isnumeric, isalnum,
919+
iscntrl, ispunct, isspace, isprint, isgraph,
920+
lowercase, uppercase, titlecase, lcfirst, ucfirst
921+
922+
if VERSION < v"0.7.0-DEV.2915"
923+
# 0.7.0-DEV.1930
924+
if !isdefined(Base, :textwidth)
925+
textwidth(c::Char) = charwidth(c)
926+
textwidth(c::AbstractString) = strwidth(c)
927+
end
928+
929+
isnumeric(c::Char) = isnumber(c)
930+
931+
# 0.6.0-dev.1404 (https://github.com/JuliaLang/julia/pull/19469)
932+
if !isdefined(Base, :titlecase)
933+
titlecase(c::Char) = isascii(c) ? ('a' <= c <= 'z' ? c - 0x20 : c) :
934+
Char(ccall(:utf8proc_totitle, UInt32, (UInt32,), c))
935+
936+
function titlecase(s::AbstractString)
937+
startword = true
938+
b = IOBuffer()
939+
for c in s
940+
if isspace(c)
941+
print(b, c)
942+
startword = true
943+
else
944+
print(b, startword ? titlecase(c) : c)
945+
startword = false
946+
end
947+
end
948+
return String(take!(b))
949+
end
950+
end
951+
else
952+
using Unicode
953+
end
954+
end
955+
922956
include("deprecated.jl")
923957

924958
end # module Compat

src/deprecated.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,7 @@ else
188188
import Base.@irrational
189189
import Base.LinAlg.BLAS.@blasfunc
190190
end
191+
192+
if VERSION < v"0.7.0-DEV.2915"
193+
Base.@deprecate textwidth Compat.Unicode.textwidth
194+
end

test/runtests.jl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,10 @@ let s = "Koala test: 🐨"
320320
end
321321

322322
# julia#17155, tests from Base Julia
323-
@test (uppercasehex)(239487) == "3A77F"
323+
@test (Compat.Unicode.uppercasehex)(239487) == "3A77F"
324324
let str = randstring(20)
325-
@test filter(!isupper, str) == replace(str, r"[A-Z]", "")
326-
@test filter(!islower, str) == replace(str, r"[a-z]", "")
325+
@test filter(!Compat.Unicode.isupper, str) == replace(str, r"[A-Z]", "")
326+
@test filter(!Compat.Unicode.islower, str) == replace(str, r"[a-z]", "")
327327
end
328328

329329
# julia#19950, tests from Base (#20028)
@@ -919,8 +919,8 @@ end
919919
@test 1 in BitSet(1:10)
920920

921921
# 0.7.0-DEV.1930
922-
@test textwidth("A") == 1
923-
@test textwidth('A') == 1
922+
@test Compat.Unicode.textwidth("A") == 1
923+
@test Compat.Unicode.textwidth('A') == 1
924924

925925
# 0.7
926926
@test diagm(0 => ones(2), -1 => ones(2)) == [1.0 0.0 0.0; 1.0 1.0 0.0; 0.0 1.0 0.0]
@@ -985,6 +985,18 @@ end
985985
@test ComplexF32 === Complex{Float32}
986986
@test ComplexF64 === Complex{Float64}
987987

988+
# 0.7.0-DEV.2915
989+
module Test25021
990+
using Compat
991+
using Compat.Test
992+
using Compat.Unicode
993+
@test isdefined(@__MODULE__, :Unicode)
994+
995+
@test !isnumeric('a')
996+
@test isnumeric('1')
997+
@test titlecase("firstname lastname") == "Firstname Lastname"
998+
end
999+
9881000
if VERSION < v"0.6.0"
9891001
include("deprecated.jl")
9901002
end

0 commit comments

Comments
 (0)