Skip to content

Commit

Permalink
Deprecate isnumber() in favor of isnumeric()
Browse files Browse the repository at this point in the history
Consistent with Python and Rust (but not Go), and less easy to confuse
with isdigit(). Improve documentation to make confusion less easy.

Also fix a few uses where isdigit() is more appropriate than isnumber().
  • Loading branch information
nalimilan committed Dec 9, 2017
1 parent ff045af commit f153e37
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 23 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ Deprecated or removed
* The `sum_kbn` and `cumsum_kbn` functions have been moved to the
[KahanSummation](https://github.com/JuliaMath/KahanSummation.jl) package ([#24869]).

* `isnumber` has been deprecated in favor of `isnumeric` ([#25008])

Command-line option changes
---------------------------

Expand Down Expand Up @@ -1711,3 +1713,4 @@ Command-line option changes
[#24413]: https://github.com/JuliaLang/julia/issues/24413
[#24653]: https://github.com/JuliaLang/julia/issues/24653
[#24869]: https://github.com/JuliaLang/julia/issues/24869
[#25008]: https://github.com/JuliaLang/julia/issues/25008
2 changes: 1 addition & 1 deletion base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ function load_machine_file(path::AbstractString)
s = split(line, '*'; keep = false)
map!(strip, s, s)
if length(s) > 1
cnt = isnumber(s[1]) ? parse(Int,s[1]) : Symbol(s[1])
cnt = all(isdigit, s[1]) ? parse(Int,s[1]) : Symbol(s[1])
push!(machines,(s[2], cnt))
else
push!(machines,line)
Expand Down
6 changes: 5 additions & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1072,12 +1072,13 @@ function Matrix()
return Matrix(uninitialized, 0, 0)
end

for name in ("alnum", "alpha", "cntrl", "digit", "number", "graph",
for name in ("alnum", "alpha", "cntrl", "digit", "graph",
"lower", "print", "punct", "space", "upper", "xdigit")
f = Symbol("is",name)
@eval import .UTF8proc: $f
@eval @deprecate ($f)(s::AbstractString) all($f, s)
end
@deprecate isnumber(s::AbstractString) all(isnumeric, s)

# TODO: remove warning for using `_` in parse_input_line in base/client.jl

Expand Down Expand Up @@ -2186,6 +2187,9 @@ end
@deprecate_moved sum_kbn "KahanSummation"
@deprecate_moved cumsum_kbn "KahanSummation"

# PR #25008
@deprecate isnumber(c::Char) isnumeric(c)

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ export
isgraph,
islower,
ismatch,
isnumber,
isnumeric,
isprint,
ispunct,
isspace,
Expand Down
2 changes: 1 addition & 1 deletion base/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ precompile(Tuple{typeof(Base.lstrip), Base.SubString{String}, Array{Char, 1}})
precompile(Tuple{getfield(Base, Symbol("#kw##split")), Array{Any, 1}, typeof(Base.split), String, Char})
precompile(Tuple{getfield(Base, Symbol("#kw##split")), Array{Any, 1}, typeof(Base.split), Base.SubString{String}, Char})
precompile(Tuple{typeof(Base.map!), typeof(Base.strip), Array{Base.SubString{String}, 1}, Array{Base.SubString{String}, 1}})
precompile(Tuple{typeof(Base.UTF8proc.isnumber), Base.SubString{String}})
precompile(Tuple{typeof(Base.UTF8proc.isnumeric), Base.SubString{String}})
precompile(Tuple{Type{Core.Inference.Generator{I, F} where F where I}, Type{Core.Inference.Const}, Tuple{Tuple{Base.DevNullStream, Base.DevNullStream, Base.DevNullStream}}})
precompile(Tuple{Type{Core.Inference.Generator{Tuple{Tuple{Base.DevNullStream, Base.DevNullStream, Base.DevNullStream}}, Type{Core.Inference.Const}}}, Type{Core.Inference.Const}, Tuple{Tuple{Base.DevNullStream, Base.DevNullStream, Base.DevNullStream}}})
precompile(Tuple{typeof(Core.Inference.convert), Type{Tuple{Tuple{Base.DevNullStream, Base.DevNullStream, Base.DevNullStream}}}, Tuple{Tuple{Base.DevNullStream, Base.DevNullStream, Base.DevNullStream}}})
Expand Down
6 changes: 3 additions & 3 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,11 @@ function _replace(io, repl_s::SubstitutionString, str, r, re)
if repl[next_i] == SUB_CHAR
write(io, SUB_CHAR)
i = nextind(repl, next_i)
elseif isnumber(repl[next_i])
elseif isdigit(repl[next_i])
group = parse(Int, repl[next_i])
i = nextind(repl, next_i)
while i <= e
if isnumber(repl[i])
if isdigit(repl[i])
group = 10group + parse(Int, repl[i])
i = nextind(repl, i)
else
Expand All @@ -364,7 +364,7 @@ function _replace(io, repl_s::SubstitutionString, str, r, re)
end
# TODO: avoid this allocation
groupname = SubString(repl, groupstart, prevind(repl, i))
if all(isnumber,groupname)
if all(isdigit, groupname)
_write_capture(io, re, parse(Int, groupname))
else
group = PCRE.substring_number_from_name(re.regex, groupname)
Expand Down
20 changes: 13 additions & 7 deletions base/strings/utf8proc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export isgraphemebreak, category_code, category_abbrev, category_string

# also exported by Base:
export normalize_string, graphemes, is_assigned_char, textwidth, isvalid,
islower, isupper, isalpha, isdigit, isnumber, isalnum,
islower, isupper, isalpha, isdigit, isnumeric, isalnum,
iscntrl, ispunct, isspace, isprint, isgraph

# whether codepoints are valid Unicode scalar values, i.e. 0-0xd7ff, 0xe000-0x10ffff
Expand Down Expand Up @@ -349,7 +349,7 @@ end
"""
isdigit(c::Char) -> Bool
Tests whether a character is a numeric digit (0-9).
Tests whether a character is a decimal digit (0-9).
# Examples
```jldoctest
Expand Down Expand Up @@ -387,25 +387,31 @@ false
isalpha(c::Char) = (UTF8PROC_CATEGORY_LU <= category_code(c) <= UTF8PROC_CATEGORY_LO)

"""
isnumber(c::Char) -> Bool
isnumeric(c::Char) -> Bool
Tests whether a character is numeric.
A character is classified as numeric if it belongs to the Unicode general category Number,
i.e. a character whose category code begins with 'N'.
Note that this broad category includes characters such as ¾ and ௰.
Use [`isdigit`](@ref) to check whether a character a decimal digit between 0 and 9.
# Examples
```jldoctest
julia> isnumber('9')
julia> isnumeric('௰')
true
julia> isnumeric('9')
true
julia> isnumber('α')
julia> isnumeric('α')
false
julia> isnumber('❤')
julia> isnumeric('❤')
false
```
"""
isnumber(c::Char) = (UTF8PROC_CATEGORY_ND <= category_code(c) <= UTF8PROC_CATEGORY_NO)
isnumeric(c::Char) = (UTF8PROC_CATEGORY_ND <= category_code(c) <= UTF8PROC_CATEGORY_NO)

"""
isalnum(c::Char) -> Bool
Expand Down
2 changes: 1 addition & 1 deletion doc/src/stdlib/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Base.UTF8proc.iscntrl
Base.UTF8proc.isdigit
Base.UTF8proc.isgraph
Base.UTF8proc.islower
Base.UTF8proc.isnumber
Base.UTF8proc.isnumeric
Base.UTF8proc.isprint
Base.UTF8proc.ispunct
Base.UTF8proc.isspace
Expand Down
16 changes: 8 additions & 8 deletions test/unicode/utf8proc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ end
@test islower(c) == true
@test isupper(c) == false
@test isdigit(c) == false
@test isnumber(c) == false
@test isnumeric(c) == false
end

aupper=['A', 'D', 'J', 'Y', 'Z']
Expand All @@ -102,27 +102,27 @@ end
@test islower(c) == false
@test isupper(c) == true
@test isdigit(c) == false
@test isnumber(c) == false
@test isnumeric(c) == false
end

nocase=['א','']
alphas=vcat(alower,ulower,aupper,uupper,nocase)

for c in alphas
@test isalpha(c) == true
@test isnumber(c) == false
@test isnumeric(c) == false
end

anumber=['0', '1', '5', '9']
unumber=['٣', '٥', '٨', '¹', '' ]

for c in anumber
@test isdigit(c) == true
@test isnumber(c) == true
@test isnumeric(c) == true
end
for c in unumber
@test isdigit(c) == false
@test isnumber(c) == true
@test isnumeric(c) == true
end

alnums=vcat(alphas,anumber,unumber)
Expand Down Expand Up @@ -197,7 +197,7 @@ end
@test !all(isgraph," \t \n \r ")
@test !all(isprint," \t \n \r ")
@test !all(isalpha," \t \n \r ")
@test !all(isnumber," \t \n \r ")
@test !all(isnumeric," \t \n \r ")
@test !all(ispunct," \t \n \r ")

@test !all(isspace,"ΣβΣβ")
Expand All @@ -206,11 +206,11 @@ end
@test all(isprint,"ΣβΣβ")
@test !all(isupper,"ΣβΣβ")
@test !all(islower,"ΣβΣβ")
@test !all(isnumber,"ΣβΣβ")
@test !all(isnumeric,"ΣβΣβ")
@test !all(iscntrl,"ΣβΣβ")
@test !all(ispunct,"ΣβΣβ")

@test all(isnumber,"23435")
@test all(isnumeric,"23435")
@test all(isdigit,"23435")
@test all(isalnum,"23435")
@test !all(isalpha,"23435")
Expand Down

0 comments on commit f153e37

Please sign in to comment.