From f153e37e69652448031a04c94beabe3fa0a1ed1c Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sat, 9 Dec 2017 21:54:56 +0100 Subject: [PATCH] Deprecate isnumber() in favor of isnumeric() 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(). --- NEWS.md | 3 +++ base/client.jl | 2 +- base/deprecated.jl | 6 +++++- base/exports.jl | 2 +- base/precompile.jl | 2 +- base/regex.jl | 6 +++--- base/strings/utf8proc.jl | 20 +++++++++++++------- doc/src/stdlib/strings.md | 2 +- test/unicode/utf8proc.jl | 16 ++++++++-------- 9 files changed, 36 insertions(+), 23 deletions(-) diff --git a/NEWS.md b/NEWS.md index 7d1277a02dc3a..7d21d0aa31812 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 --------------------------- @@ -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 \ No newline at end of file diff --git a/base/client.jl b/base/client.jl index df6f5df0c9d3d..88914e1d49f2f 100644 --- a/base/client.jl +++ b/base/client.jl @@ -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) diff --git a/base/deprecated.jl b/base/deprecated.jl index 68960bb291104..d5098560f7489 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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 @@ -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 diff --git a/base/exports.jl b/base/exports.jl index ed3f5d2b34b49..22ef335682f8a 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -740,7 +740,7 @@ export isgraph, islower, ismatch, - isnumber, + isnumeric, isprint, ispunct, isspace, diff --git a/base/precompile.jl b/base/precompile.jl index f0be54c9270af..79c1864aca2ea 100644 --- a/base/precompile.jl +++ b/base/precompile.jl @@ -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}}}) diff --git a/base/regex.jl b/base/regex.jl index 344730007d7ec..3eada8988848c 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -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 @@ -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) diff --git a/base/strings/utf8proc.jl b/base/strings/utf8proc.jl index cf30ec5b3aa6f..16de006a51e03 100644 --- a/base/strings/utf8proc.jl +++ b/base/strings/utf8proc.jl @@ -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 @@ -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 @@ -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 diff --git a/doc/src/stdlib/strings.md b/doc/src/stdlib/strings.md index ec561b8a810a6..8c5dde8363fe8 100644 --- a/doc/src/stdlib/strings.md +++ b/doc/src/stdlib/strings.md @@ -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 diff --git a/test/unicode/utf8proc.jl b/test/unicode/utf8proc.jl index ef05772064913..147f522103e5c 100644 --- a/test/unicode/utf8proc.jl +++ b/test/unicode/utf8proc.jl @@ -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'] @@ -102,7 +102,7 @@ end @test islower(c) == false @test isupper(c) == true @test isdigit(c) == false - @test isnumber(c) == false + @test isnumeric(c) == false end nocase=['א','ﺵ'] @@ -110,7 +110,7 @@ end for c in alphas @test isalpha(c) == true - @test isnumber(c) == false + @test isnumeric(c) == false end anumber=['0', '1', '5', '9'] @@ -118,11 +118,11 @@ end 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) @@ -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,"ΣβΣβ") @@ -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")