Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Base.one for StringView #24

Merged
merged 12 commits into from
Apr 25, 2024
7 changes: 7 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file is machine-generated - editing it directly is not advised

julia_version = "1.10.2"
manifest_format = "2.0"
project_hash = "b49e9b1483a70ece1f89d014cdbe39fe7ec7e18e"

[deps]
3 changes: 3 additions & 0 deletions src/StringViews.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Base.Vector{UInt8}(s::StringViewAndSub) = Vector{UInt8}(codeunits(s))
Base.Array{UInt8}(s::StringViewAndSub) = Vector{UInt8}(s)
Base.String(s::StringViewAndSub) = String(copyto!(Base.StringVector(ncodeunits(s)), codeunits(s)))
StringView(s::StringView) = s
StringView{S}(s::StringView{S}) where {S<:AbstractVector{UInt8}} = s
StringView(s::String) = StringView(codeunits(s))

# iobuffer constructor (note that buf.data is always 1-based)
Expand Down Expand Up @@ -85,7 +86,9 @@ end
Base.:(==)(s1::StringViewAndSub, s2::StringAndSub) = s2 == s1

Base.typemin(::Type{StringView{Vector{UInt8}}}) = StringView(Vector{UInt8}(undef,0))
Base.typemin(::Type{StringView{Base.CodeUnits{UInt8, String}}}) = StringView("")
Base.typemin(::T) where {T<:StringView} = typemin(T)
Base.one(::Union{T,Type{T}}) where {T<:StringView} = typemin(T)

if VERSION < v"1.10.0-DEV.1007" # JuliaLang/julia#47880
Base.isvalid(s::DenseStringViewAndSub) = ccall(:u8_isvalid, Int32, (Ptr{UInt8}, Int), s, sizeof(s)) ≠ 0
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ end

@test Base.typemin(s) isa StringView{Vector{UInt8}}
@test Base.typemin(s) == ""
@test one(s) == one(typeof(s)) == typemin(s) == ""
@test oneunit(s) == oneunit(typeof(s)) == one(s) == ""
@test one(su) == one(typeof(su)) == typemin(su) == ""
@test oneunit(su) == oneunit(typeof(su)) == one(su) == ""

@test isascii(s)
@test !isascii(StringView("fööbār"))
Expand Down
Loading