Skip to content

Commit

Permalink
Add Compat.Unicode for standard library Unicode (#432)
Browse files Browse the repository at this point in the history
* Add Compat.Unicode for standard library Unicode

* Define titlecase only on Julia 0.5

* Remove normalize and isassigned from exports

* Add textwidth deprecation
  • Loading branch information
omus authored and ararslan committed Dec 21, 2017
1 parent 1aa2b90 commit 7695d5c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ Currently, the `@compat` macro supports the following syntaxes:
`CartesianRange` now has two type parameters, so using them as
fields in other `struct`s requires manual intervention.

## Module Aliases

* In 0.6, some 0.5 iterator functions have been moved to the `Base.Iterators`
module. Code can be written to work on both 0.5 and 0.6 by `import`ing or
`using` the `Compat.Iterators` module instead. ([#18839])

* `using Compat.Test`, `using Compat.SharedArrays`, `using Compat.Mmap`, and `using
Compat.DelimitedFiles` are provided on versions older than 0.7, where these are not yet
part of the standard library. ([#23931])
Expand All @@ -89,11 +95,8 @@ Currently, the `@compat` macro supports the following syntaxes:
* `using Compat.Dates` is provided on versions older than 0.7, where this library is not
yet a part of the standard library. ([#24459])

## Module Aliases

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

## New functions, macros, and methods

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

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

* `strwidth` and `charwidth` are now merged into `textwidth` ([#23667]).

* `Complex32`, `Complex64`, and `Complex128` are now `ComplexF16`, `ComplexF32`, and
`ComplexF64`, respectively ([#24647]).

Expand Down Expand Up @@ -378,7 +379,6 @@ includes this fix. Find the minimum version from there.
[#23427]: https://github.com/JuliaLang/julia/issues/23427
[#23570]: https://github.com/JuliaLang/julia/issues/23570
[#23666]: https://github.com/JuliaLang/julia/issues/23666
[#23667]: https://github.com/JuliaLang/julia/issues/23667
[#23757]: https://github.com/JuliaLang/julia/issues/23757
[#23812]: https://github.com/JuliaLang/julia/issues/23812
[#23931]: https://github.com/JuliaLang/julia/issues/23931
Expand All @@ -392,3 +392,4 @@ includes this fix. Find the minimum version from there.
[#24652]: https://github.com/JuliaLang/julia/issues/24652
[#24657]: https://github.com/JuliaLang/julia/issues/24657
[#24785]: https://github.com/JuliaLang/julia/issues/24785
[#25021]: https://github.com/JuliaLang/julia/issues/25021
48 changes: 41 additions & 7 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -797,13 +797,6 @@ end
export BitSet
end

# 0.7.0-DEV.1930
@static if !isdefined(Base, :textwidth)
textwidth(c::Char) = charwidth(c)
textwidth(c::AbstractString) = strwidth(c)
export textwidth
end

# 0.7.0-DEV.2116
@static if VERSION < v"0.7.0-DEV.2116"
import Base: spdiagm
Expand Down Expand Up @@ -919,6 +912,47 @@ end
export ComplexF64
end

# 0.7.0-DEV.2915
module Unicode
export graphemes, textwidth, isvalid,
islower, isupper, isalpha, isdigit, isxdigit, isnumeric, isalnum,
iscntrl, ispunct, isspace, isprint, isgraph,
lowercase, uppercase, titlecase, lcfirst, ucfirst

if VERSION < v"0.7.0-DEV.2915"
# 0.7.0-DEV.1930
if !isdefined(Base, :textwidth)
textwidth(c::Char) = charwidth(c)
textwidth(c::AbstractString) = strwidth(c)
end

isnumeric(c::Char) = isnumber(c)

# 0.6.0-dev.1404 (https://github.com/JuliaLang/julia/pull/19469)
if !isdefined(Base, :titlecase)
titlecase(c::Char) = isascii(c) ? ('a' <= c <= 'z' ? c - 0x20 : c) :
Char(ccall(:utf8proc_totitle, UInt32, (UInt32,), c))

function titlecase(s::AbstractString)
startword = true
b = IOBuffer()
for c in s
if isspace(c)
print(b, c)
startword = true
else
print(b, startword ? titlecase(c) : c)
startword = false
end
end
return String(take!(b))
end
end
else
using Unicode
end
end

include("deprecated.jl")

end # module Compat
4 changes: 4 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,7 @@ else
import Base.@irrational
import Base.LinAlg.BLAS.@blasfunc
end

if VERSION < v"0.7.0-DEV.2915"
Base.@deprecate textwidth Compat.Unicode.textwidth
end
22 changes: 17 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,10 @@ let s = "Koala test: 🐨"
end

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

# julia#19950, tests from Base (#20028)
Expand Down Expand Up @@ -919,8 +919,8 @@ end
@test 1 in BitSet(1:10)

# 0.7.0-DEV.1930
@test textwidth("A") == 1
@test textwidth('A') == 1
@test Compat.Unicode.textwidth("A") == 1
@test Compat.Unicode.textwidth('A') == 1

# 0.7
@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]
Expand Down Expand Up @@ -985,6 +985,18 @@ end
@test ComplexF32 === Complex{Float32}
@test ComplexF64 === Complex{Float64}

# 0.7.0-DEV.2915
module Test25021
using Compat
using Compat.Test
using Compat.Unicode
@test isdefined(@__MODULE__, :Unicode)

@test !isnumeric('a')
@test isnumeric('1')
@test titlecase("firstname lastname") == "Firstname Lastname"
end

if VERSION < v"0.6.0"
include("deprecated.jl")
end
Expand Down

0 comments on commit 7695d5c

Please sign in to comment.