Skip to content

Commit

Permalink
Add StringViews extension
Browse files Browse the repository at this point in the history
This allows `MemoryView(::StringView)` to work.
  • Loading branch information
jakobnissen committed Oct 8, 2024
1 parent 081943f commit bab27c0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ uuid = "a791c907-b98b-4e44-8f4d-e4c2362c6b2f"
version = "0.2.1"
authors = ["Jakob Nybo Nissen <[email protected]>"]

[weakdeps]
StringViews = "354b36f9-a18e-4713-926e-db85100087ba"

[compat]
Aqua = "0.8.7"
StringViews = "1"
Test = "1.11"
julia = "1.11"

[extensions]
StringViewsExt = "StringViews"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
StringViews = "354b36f9-a18e-4713-926e-db85100087ba"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "Test"]
test = ["Aqua", "StringViews", "Test"]
9 changes: 9 additions & 0 deletions ext/StringViewsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module StringViewsExt

using StringViews: StringView
import MemoryViews: MemoryView, MemoryKind

MemoryView(s::StringView) = MemoryView(codeunits(s))
MemoryKind(::Type{StringView{A}}) where {A} = MemoryKind(A)

end # module
20 changes: 20 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Test
using MemoryViews
using Aqua
using StringViews: StringView

MemoryViews.MemoryView(s::GenericString) = MemoryView(s.string)

Expand Down Expand Up @@ -435,4 +436,23 @@ end
@test_throws Exception inner(NotMemory())
end

@testset "StringViews" begin
# Backed by mutable array
s = StringView([0x01, 0x02])
@test MemoryView(s) isa MutableMemoryView{UInt8}
@test MemoryView(s) == [0x01, 0x02]
@test MemoryKind(typeof(s)) == IsMemory{MutableMemoryView{UInt8}}()

# Backed by immutable string data
s = StringView(view(codeunits("abcd"), 2:4))
@test MemoryView(s) isa ImmutableMemoryView{UInt8}
@test MemoryView(s) == codeunits("bcd")
@test MemoryKind(typeof(s)) == IsMemory{ImmutableMemoryView{UInt8}}()

# Not backed by memory
s = StringView(view(0x61:0x65, 2:4))
@test_throws MethodError MemoryView(s)
@test MemoryKind(typeof(s)) == NotMemory()
end

Aqua.test_all(MemoryViews)

0 comments on commit bab27c0

Please sign in to comment.