From b49914ba6b0711d04581a2bcf7d32a1b7c969064 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Tue, 31 Jan 2017 11:18:56 -0500 Subject: [PATCH] deprecate string methods of character predicates, e.g. `isnumber("")` fixes #14156 --- base/deprecated.jl | 6 ++++ base/strings/basic.jl | 9 +++-- base/strings/utf8proc.jl | 74 +++++++++++++++------------------------- test/unicode/utf8proc.jl | 51 ++++++++++++++------------- 4 files changed, 63 insertions(+), 77 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 5e966bd834efc2..26383d1b48a007 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1849,4 +1849,10 @@ end) @deprecate FloatRange{T}(start::T, step, len, den) Base.floatrange(T, start, step, len, den) +for name in ("alnum", "alpha", "cntrl", "digit", "number", "graph", + "lower", "print", "punct", "space", "upper", "isxdigit") + f = Symbol("is",name) + @eval @deprecate ($f)(s::AbstractString) all($f, s) +end + # End deprecations scheduled for 0.6 diff --git a/base/strings/basic.jl b/base/strings/basic.jl index d5da3559f7f652..2d6c253e773a7b 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -280,20 +280,19 @@ isascii(s::AbstractString) = all(isascii, s) promote_rule{S<:AbstractString,T<:AbstractString}(::Type{S}, ::Type{T}) = String """ - isxdigit(c::Union{Char,AbstractString}) -> Bool + isxdigit(c::Char) -> Bool -Tests whether a character is a valid hexadecimal digit, or whether this is true for all elements of a string. +Tests whether a character is a valid hexadecimal digit. ```jldoctest -julia> isxdigit("abc") +julia> isxdigit('a') true -julia> isxdigit("0x9") +julia> isxdigit('z') false ``` """ isxdigit(c::Char) = '0'<=c<='9' || 'a'<=c<='f' || 'A'<=c<='F' -isxdigit(s::AbstractString) = all(isxdigit, s) ## uppercase, lowercase, and titlecase transformations ## diff --git a/base/strings/utf8proc.jl b/base/strings/utf8proc.jl index 1ad2e37bf4cf25..27683d1d4defb9 100644 --- a/base/strings/utf8proc.jl +++ b/base/strings/utf8proc.jl @@ -219,10 +219,10 @@ is_assigned_char(c) = category_code(c) != UTF8PROC_CATEGORY_CN ## libc character class predicates ## """ - islower(c::Union{Char,AbstractString}) -> Bool + islower(c::Char) -> Bool -Tests whether a character is a lowercase letter, or whether this is true for all elements of -a string. A character is classified as lowercase if it belongs to Unicode category Ll, +Tests whether a character is a lowercase letter. +A character is classified as lowercase if it belongs to Unicode category Ll, Letter: Lowercase. """ islower(c::Char) = (category_code(c) == UTF8PROC_CATEGORY_LL) @@ -230,10 +230,10 @@ islower(c::Char) = (category_code(c) == UTF8PROC_CATEGORY_LL) # true for Unicode upper and mixed case """ - isupper(c::Union{Char,AbstractString}) -> Bool + isupper(c::Char) -> Bool -Tests whether a character is an uppercase letter, or whether this is true for all elements -of a string. A character is classified as uppercase if it belongs to Unicode category Lu, +Tests whether a character is an uppercase letter. +A character is classified as uppercase if it belongs to Unicode category Lu, Letter: Uppercase, or Lt, Letter: Titlecase. """ function isupper(c::Char) @@ -242,36 +242,35 @@ function isupper(c::Char) end """ - isdigit(c::Union{Char,AbstractString}) -> Bool + isdigit(c::Char) -> Bool -Tests whether a character is a numeric digit (0-9), or whether this is true for all elements -of a string. +Tests whether a character is a numeric digit (0-9). """ isdigit(c::Char) = ('0' <= c <= '9') """ - isalpha(c::Union{Char,AbstractString}) -> Bool + isalpha(c::Char) -> Bool -Tests whether a character is alphabetic, or whether this is true for all elements of a -string. A character is classified as alphabetic if it belongs to the Unicode general +Tests whether a character is alphabetic. +A character is classified as alphabetic if it belongs to the Unicode general category Letter, i.e. a character whose category code begins with 'L'. """ isalpha(c::Char) = (UTF8PROC_CATEGORY_LU <= category_code(c) <= UTF8PROC_CATEGORY_LO) """ - isnumber(c::Union{Char,AbstractString}) -> Bool + isnumber(c::Char) -> Bool -Tests whether a character is numeric, or whether this is true for all elements of a string. +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'. """ isnumber(c::Char) = (UTF8PROC_CATEGORY_ND <= category_code(c) <= UTF8PROC_CATEGORY_NO) """ - isalnum(c::Union{Char,AbstractString}) -> Bool + isalnum(c::Char) -> Bool -Tests whether a character is alphanumeric, or whether this is true for all elements of a -string. A character is classified as alphabetic if it belongs to the Unicode general +Tests whether a character is alphanumeric. +A character is classified as alphabetic if it belongs to the Unicode general category Letter or Number, i.e. a character whose category code begins with 'L' or 'N'. """ function isalnum(c::Char) @@ -283,67 +282,50 @@ end # following C++ only control characters from the Latin-1 subset return true """ - iscntrl(c::Union{Char,AbstractString}) -> Bool + iscntrl(c::Char) -> Bool -Tests whether a character is a control character, or whether this is true for all elements -of a string. Control characters are the non-printing characters of the Latin-1 subset of Unicode. +Tests whether a character is a control character. +Control characters are the non-printing characters of the Latin-1 subset of Unicode. """ iscntrl(c::Char) = (c <= Char(0x1f) || Char(0x7f) <= c <= Char(0x9f)) """ - ispunct(c::Union{Char,AbstractString}) -> Bool + ispunct(c::Char) -> Bool Tests whether a character belongs to the Unicode general category Punctuation, i.e. a -character whose category code begins with 'P'. For strings, tests whether this is true for -all elements of the string. +character whose category code begins with 'P'. """ ispunct(c::Char) = (UTF8PROC_CATEGORY_PC <= category_code(c) <= UTF8PROC_CATEGORY_PO) # \u85 is the Unicode Next Line (NEL) character """ - isspace(c::Union{Char,AbstractString}) -> Bool + isspace(c::Char) -> Bool Tests whether a character is any whitespace character. Includes ASCII characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' ', Latin-1 character U+0085, and characters in Unicode -category Zs. For strings, tests whether this is true for all elements of the string. +category Zs. """ @inline isspace(c::Char) = c == ' ' || '\t' <= c <='\r' || c == '\u85' || '\ua0' <= c && category_code(c) == UTF8PROC_CATEGORY_ZS """ - isprint(c::Union{Char,AbstractString}) -> Bool + isprint(c::Char) -> Bool -Tests whether a character is printable, including spaces, but not a control character. For -strings, tests whether this is true for all elements of the string. +Tests whether a character is printable, including spaces, but not a control character. """ isprint(c::Char) = (UTF8PROC_CATEGORY_LU <= category_code(c) <= UTF8PROC_CATEGORY_ZS) # true in principal if a printer would use ink """ - isgraph(c::Union{Char,AbstractString}) -> Bool + isgraph(c::Char) -> Bool -Tests whether a character is printable, and not a space, or whether this is true for all -elements of a string. Any character that would cause a printer to use ink should be +Tests whether a character is printable, and not a space. +Any character that would cause a printer to use ink should be classified with `isgraph(c)==true`. """ isgraph(c::Char) = (UTF8PROC_CATEGORY_LU <= category_code(c) <= UTF8PROC_CATEGORY_SO) -for name = ("alnum", "alpha", "cntrl", "digit", "number", "graph", - "lower", "print", "punct", "space", "upper") - f = Symbol("is",name) - @eval begin - function $f(s::AbstractString) - for c in s - if !$f(c) - return false - end - end - return true - end - end -end - ############################################################################ # iterators for grapheme segmentation diff --git a/test/unicode/utf8proc.jl b/test/unicode/utf8proc.jl index c88cc61632fd3c..d8b05f458a1d93 100644 --- a/test/unicode/utf8proc.jl +++ b/test/unicode/utf8proc.jl @@ -187,34 +187,33 @@ let end -@test isspace(" \t \n \r ")==true -@test isgraph(" \t \n \r ")==false -@test isprint(" \t \n \r ")==false -@test isalpha(" \t \n \r ")==false -@test isnumber(" \t \n \r ")==false -@test ispunct(" \t \n \r ")==false - -@test isspace("ΣβΣβ")==false -@test isalpha("ΣβΣβ")==true -@test isgraph("ΣβΣβ")==true -@test isprint("ΣβΣβ")==true -@test isupper("ΣβΣβ")==false -@test islower("ΣβΣβ")==false -@test isnumber("ΣβΣβ")==false -@test iscntrl("ΣβΣβ")==false -@test ispunct("ΣβΣβ")==false - -@test isnumber("23435")==true -@test isdigit("23435")==true -@test isalnum("23435")==true -@test isalpha("23435")==false -@test iscntrl( string(Char(0x0080))) == true -@test ispunct( "‡؟჻") ==true +@test all(isspace," \t \n \r ")==true +@test all(isgraph," \t \n \r ")==false +@test all(isprint," \t \n \r ")==false +@test all(isalpha," \t \n \r ")==false +@test all(isnumber," \t \n \r ")==false +@test all(ispunct," \t \n \r ")==false + +@test all(isspace,"ΣβΣβ")==false +@test all(isalpha,"ΣβΣβ")==true +@test all(isgraph,"ΣβΣβ")==true +@test all(isprint,"ΣβΣβ")==true +@test all(isupper,"ΣβΣβ")==false +@test all(islower,"ΣβΣβ")==false +@test all(isnumber,"ΣβΣβ")==false +@test all(iscntrl,"ΣβΣβ")==false +@test all(ispunct,"ΣβΣβ")==false + +@test all(isnumber,"23435")==true +@test all(isdigit,"23435")==true +@test all(isalnum,"23435")==true +@test all(isalpha,"23435")==false +@test all(iscntrl,string(Char(0x0080))) == true +@test all(ispunct, "‡؟჻") ==true @test isxdigit('0') == true -@test isxdigit("0") == true -@test isxdigit("a") == true -@test isxdigit("g") == false +@test isxdigit('a') == true +@test isxdigit('g') == false # check utf8proc handling of CN category constants let c_ll = 'β', c_cn = '\u038B'