Skip to content

Commit fa102c1

Browse files
authored
support contiguous subarrays in isvalid(String, ...) (#36047)
1 parent 287215f commit fa102c1

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

base/strings/string.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ end
163163

164164
## checking UTF-8 & ACSII validity ##
165165

166-
byte_string_classify(s::Union{String,Vector{UInt8}}) =
166+
byte_string_classify(s::Union{String,Vector{UInt8},FastContiguousSubArray{UInt8,1,Vector{UInt8}}}) =
167167
ccall(:u8_isvalid, Int32, (Ptr{UInt8}, Int), s, sizeof(s))
168168
# 0: neither valid ASCII nor UTF-8
169169
# 1: valid ASCII
170170
# 2: valid UTF-8
171171

172-
isvalid(::Type{String}, s::Union{Vector{UInt8},String}) = byte_string_classify(s) 0
172+
isvalid(::Type{String}, s::Union{Vector{UInt8},FastContiguousSubArray{UInt8,1,Vector{UInt8}},String}) = byte_string_classify(s) 0
173173
isvalid(s::String) = isvalid(String, s)
174174

175175
is_valid_continuation(c) = c & 0xc0 == 0x80

base/strings/unicode.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ isvalid(value)
3333
3434
Returns `true` if the given value is valid for that type. Types currently can
3535
be either `AbstractChar` or `String`. Values for `AbstractChar` can be of type `AbstractChar` or [`UInt32`](@ref).
36-
Values for `String` can be of that type, or `Vector{UInt8}` or `SubString{String}`.
36+
Values for `String` can be of that type, `SubString{String}`, `Vector{UInt8}`,
37+
or a contiguous subarray thereof.
3738
3839
# Examples
3940
```jldoctest
@@ -46,6 +47,9 @@ true
4647
julia> isvalid(Char, 0xd799)
4748
true
4849
```
50+
51+
!!! compat "Julia 1.6"
52+
Support for subarray values was added in Julia 1.6.
4953
"""
5054
isvalid(T,value)
5155

test/strings/basic.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,9 @@ end
575575
for (rng, flg) in ((0x00:0x9f, false), (0xa0:0xbf, true), (0xc0:0xff, false))
576576
for cont in rng
577577
@test isvalid(String, UInt8[0xe0, cont]) == false
578-
@test isvalid(String, UInt8[0xe0, cont, 0x80]) == flg
578+
bytes = UInt8[0xe0, cont, 0x80]
579+
@test isvalid(String, bytes) == flg
580+
@test isvalid(String, @view(bytes[1:end])) == flg # contiguous subarray support
579581
end
580582
end
581583
# Check three-byte sequences

0 commit comments

Comments
 (0)