Skip to content

Commit

Permalink
Move back isascii() from Unicode to Base (#25076)
Browse files Browse the repository at this point in the history
This function is not really Unicode-related and its definition does not depend
on the Unicode standard version. This fixes a bug introduced when moving
isascii() to Base.Unicode, as it was not exported from Unicode stdlib module
as it should have been.
  • Loading branch information
nalimilan committed Dec 16, 2017
1 parent c640381 commit 9c1faae
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ Deprecated or removed

* Unicode-related string functions have been moved to the new `Unicode` standard
library module ([#25021]). This applies to `normalize_string`, `graphemes`,
`is_assigned_char`, `textwidth`, `isascii`, `islower`, `isupper`, `isalpha`,
`is_assigned_char`, `textwidth`, `islower`, `isupper`, `isalpha`,
`isdigit`, `isxdigit`, `isnumber`, `isalnum`, `iscntrl`, `ispunct`, `isspace`,
`isprint`, `isgraph`, `lowercase`, `uppercase`, `titlecase`, `lcfirst` and `ucfirst`.

Expand Down
2 changes: 1 addition & 1 deletion base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function show(io::IO, ::MIME"text/plain", c::Char)
isoverlong(c) && print(io, "[overlong] ")
u = UInt32(c)
h = hex(u, u 0xffff ? 4 : 6)
print(io, (Unicode.isascii(c) ? "ASCII/" : ""), "Unicode U+", h)
print(io, (isascii(c) ? "ASCII/" : ""), "Unicode U+", h)
else
print(io, ": Malformed UTF-8")
end
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ export
hex2bytes,
hex2bytes!,
info,
isascii,
ismatch,
isvalid,
join,
Expand Down
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ julia> countlines(io, '.')
```
"""
function countlines(io::IO, eol::Char='\n')
Unicode.isascii(eol) || throw(ArgumentError("only ASCII line terminators are supported"))
isascii(eol) || throw(ArgumentError("only ASCII line terminators are supported"))
aeol = UInt8(eol)
a = Vector{UInt8}(uninitialized, 8192)
nl = 0
Expand Down
2 changes: 1 addition & 1 deletion base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ elseif Sys.isapple()

# If there is no match, it's possible that the file does exist but HFS+
# performed unicode normalization. See https://developer.apple.com/library/mac/qa/qa1235/_index.html.
Unicode.isascii(path_basename) && return false
isascii(path_basename) && return false
Vector{UInt8}(Unicode.normalize(path_basename, :NFD)) == casepreserved_basename
end
else
Expand Down
2 changes: 1 addition & 1 deletion base/repl/latex_symbols.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ open(fname) do f
split(replace(L, r"[{}\"]+", "\t"), "\t"))
c = Char(parse(Int, x[2], 16))
if (Base.is_id_char(c) || Base.isoperator(Symbol(c))) &&
string(c) ∉ latex_strings && !Unicode.isascii(c)
string(c) ∉ latex_strings && !isascii(c)
tabcomname = escape_string(x[3])
if startswith(tabcomname, "\\\\math")
tabcomname = string("\\\\", tabcomname[7:end])
Expand Down
2 changes: 1 addition & 1 deletion base/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ Gets all of the IP addresses of the `host`.
Uses the operating system's underlying getaddrinfo implementation, which may do a DNS lookup.
"""
function getalladdrinfo(host::String)
Unicode.isascii(host) || error("non-ASCII hostname: $host")
isascii(host) || error("non-ASCII hostname: $host")
req = Libc.malloc(_sizeof_uv_getaddrinfo)
uv_req_set_data(req, C_NULL) # in case we get interrupted before arriving at the wait call
status = ccall(:jl_getaddrinfo, Int32, (Ptr{Void}, Ptr{Void}, Cstring, Ptr{Void}, Ptr{Void}),
Expand Down
24 changes: 24 additions & 0 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,30 @@ next(e::EachStringIndex, state) = (state, nextind(e.s, state))
done(e::EachStringIndex, state) = done(e.s, state)
eltype(::Type{EachStringIndex}) = Int

"""
isascii(c::Union{Char,AbstractString}) -> Bool
Test whether a character belongs to the ASCII character set, or whether this is true for
all elements of a string.
# Examples
```jldoctest
julia> isascii('a')
true
julia> isascii('α')
false
julia> isascii("abc")
true
julia> isascii("αβγ")
false
```
"""
isascii(c::Char) = bswap(reinterpret(UInt32, c)) < 0x80
isascii(s::AbstractString) = all(isascii, s)

## string map, filter, has ##

function map(f, s::AbstractString)
Expand Down
4 changes: 2 additions & 2 deletions base/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function search(a::Union{String,ByteArray}, b::Union{Int8,UInt8}, i::Integer = 1
end

function search(a::ByteArray, b::Char, i::Integer = 1)
if Unicode.isascii(b)
if isascii(b)
search(a,UInt8(b),i)
else
search(a,Vector{UInt8}(string(b)),i).start
Expand Down Expand Up @@ -59,7 +59,7 @@ function rsearch(a::Union{String,ByteArray}, b::Union{Int8,UInt8}, i::Integer =
end

function rsearch(a::ByteArray, b::Char, i::Integer = length(a))
if Unicode.isascii(b)
if isascii(b)
rsearch(a,UInt8(b),i)
else
rsearch(a,Vector{UInt8}(string(b)),i).start
Expand Down
26 changes: 0 additions & 26 deletions base/strings/unicode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -597,32 +597,6 @@ true
"""
isgraph(c::Char) = UTF8PROC_CATEGORY_LU <= category_code(c) <= UTF8PROC_CATEGORY_SO

"""
isascii(c::Union{Char,AbstractString}) -> Bool
Test whether a character belongs to the ASCII character set, or whether this is true for
all elements of a string.
# Examples
```jldoctest
julia> using Unicode
julia> isascii('a')
true
julia> isascii('α')
false
julia> isascii("abc")
true
julia> isascii("αβγ")
false
```
"""
isascii(c::Char) = bswap(reinterpret(UInt32, c)) < 0x80
isascii(s::AbstractString) = all(isascii, s)

"""
isxdigit(c::Char) -> Bool
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Distributed/src/Distributed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using Base: Process, Semaphore, JLOptions, AnyDict, buffer_writes, wait_connecte
binding_module, notify_error, atexit, julia_exename, julia_cmd,
AsyncGenerator, acquire, release, invokelatest,
shell_escape_posixly, uv_error, coalesce, notnothing
using Base.Unicode: isascii, isdigit, isnumeric
using Base.Unicode: isdigit, isnumeric

# NOTE: clusterserialize.jl imports additional symbols from Base.Serializer for use

Expand Down
2 changes: 1 addition & 1 deletion stdlib/SuiteSparse/src/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ end

### cholmod_check.h ###
function print_sparse(A::Sparse{Tv}, name::String) where Tv<:VTypes
Unicode.isascii(name) || error("non-ASCII name: $name")
isascii(name) || error("non-ASCII name: $name")
set_print_level(common_struct, 3)
@isok ccall((@cholmod_name("print_sparse", SuiteSparse_long),:libcholmod), Cint,
(Ptr{C_Sparse{Tv}}, Ptr{UInt8}, Ptr{UInt8}),
Expand Down
2 changes: 1 addition & 1 deletion test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ end
s = "julia"
@test find(c -> c == 'l', s) == [3]
g = Base.Unicode.graphemes("日本語")
@test find(Base.Unicode.isascii, g) == Int[]
@test find(isascii, g) == Int[]
@test find(!iszero, (i % 2 for i in 1:10)) == collect(1:2:9)
end
@testset "findn" begin
Expand Down
2 changes: 1 addition & 1 deletion test/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
cp, ch, st = cx[i,:]
@test cp == convert(UInt32, ch)
@test string(ch) == unescape_string(st)
if Base.Unicode.isascii(ch) || !Base.Unicode.isprint(ch)
if isascii(ch) || !Base.Unicode.isprint(ch)
@test st == escape_string(string(ch))
end
for j = 1:size(cx,1)
Expand Down

0 comments on commit 9c1faae

Please sign in to comment.