Skip to content

Commit 907da7b

Browse files
committed
add fallback isvalid
1 parent 49820f0 commit 907da7b

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/StringViews.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Base.typemin(::Type{StringView{Vector{UInt8}}}) = StringView(Vector{UInt8}(undef
6767
Base.typemin(::T) where {T<:StringView} = typemin(T)
6868

6969
Base.isvalid(s::DenseStringView) = ccall(:u8_isvalid, Int32, (Ptr{UInt8}, Int), s, sizeof(s)) 0
70+
Base.isvalid(s::StringView) = all(isvalid, s)
7071
Base.isvalid(::Type{String}, s::StringView) = isvalid(s)
7172

7273
function Base.isascii(s::StringView)

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using StringViews, Test
33
b = Vector{UInt8}("foobar")
44
s = StringView(b)
55
abc = StringView(0x61:0x63)
6+
invalid = StringView([0x8b, 0x52, 0x9b, 0x8d])
67

78
@testset "Construction/conversion" begin
89
@test StringView(s) === s
@@ -41,6 +42,11 @@ abc = StringView(0x61:0x63)
4142

4243
@test Base.print_to_string(s) == "foobar"
4344
@test Base.print_to_string(abc) == "abc"
45+
46+
@test isvalid(s)
47+
@test isvalid(abc)
48+
@test !isvalid(invalid)
49+
@test !invoke(isvalid, Tuple{StringView}, invalid)
4450
end
4551

4652
@testset "regular expressions" begin

0 commit comments

Comments
 (0)