Skip to content

Commit 292a9cc

Browse files
mnemnionstevengj
andauthored
Implement Base.one for StringView (#24)
* Implement Base.one for StringView I have a lot of `S where S<:AbstractString` code which uses `one(S)` for the empty string to compare against. I didn't realize until I went to add this that `typemin` also works, but hey, that makes the implementation that much easier, and StringViews gives me a zero-copy drop-in replacement for a string buffer with this addition. * Update runtests.jl And a test for identity equality * Union type signature for Base.one Co-authored-by: Steven G. Johnson <[email protected]> * Update test/runtests.jl Co-authored-by: Steven G. Johnson <[email protected]> * Adds Base.oneunit Also defined for Strings * Test for oneunit * Adds convert for data type of StringView As well as a custom typemin for StringView(Base.CodeUnits{etc}}. This makes the Base implementation of typemin function for both s and su in the test suite, and I would consider returning the data field for an attempt to convert a StringView to its underlying data to be legitimate. * Remove settings.json * remove settings.json entirely :/ * Removes Manifest.toml * Remove convert, add StringViews{S} constructor * Remove Manifest.toml --------- Co-authored-by: Steven G. Johnson <[email protected]>
1 parent f94b09e commit 292a9cc

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/StringViews.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Base.Vector{UInt8}(s::StringViewAndSub) = Vector{UInt8}(codeunits(s))
3939
Base.Array{UInt8}(s::StringViewAndSub) = Vector{UInt8}(s)
4040
Base.String(s::StringViewAndSub) = String(copyto!(Base.StringVector(ncodeunits(s)), codeunits(s)))
4141
StringView(s::StringView) = s
42+
StringView{S}(s::StringView{S}) where {S<:AbstractVector{UInt8}} = s
4243
StringView(s::String) = StringView(codeunits(s))
4344

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

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

9093
if VERSION < v"1.10.0-DEV.1007" # JuliaLang/julia#47880
9194
Base.isvalid(s::DenseStringViewAndSub) = ccall(:u8_isvalid, Int32, (Ptr{UInt8}, Int), s, sizeof(s)) 0

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ end
157157

158158
@test Base.typemin(s) isa StringView{Vector{UInt8}}
159159
@test Base.typemin(s) == ""
160+
@test one(s) == one(typeof(s)) == typemin(s) == ""
161+
@test oneunit(s) == oneunit(typeof(s)) == one(s) == ""
162+
@test one(su) == one(typeof(su)) == typemin(su) == ""
163+
@test oneunit(su) == oneunit(typeof(su)) == one(su) == ""
160164

161165
@test isascii(s)
162166
@test !isascii(StringView("fööbār"))

0 commit comments

Comments
 (0)