Skip to content

Commit 2d53794

Browse files
committed
iteration tests
1 parent b0cea0b commit 2d53794

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/decoding.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ Base.getindex(s::StringView, r::UnitRange{<:Integer}) = s[Int(first(r)):Int(last
7676
@inbounds isvalid(s, j) || string_index_err(s, j)
7777
end
7878
j = nextind(s, j) - 1
79-
n = j - i + 1
80-
ss = Base._string_n(n)
81-
GC.@preserve s ss unsafe_copyto!(pointer(ss), pointer(s, i), n)
82-
return ss
79+
return StringView(@view s.data[i:j])
8380
end
8481

8582
Base.length(s::StringView) = length_continued(s, 1, ncodeunits(s), ncodeunits(s))

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ s = StringView(b)
55
ss = SubString(s, 2, 5) # "ooba"
66
abc = StringView(0x61:0x63)
77
invalid = StringView([0x8b, 0x52, 0x9b, 0x8d])
8+
su = StringView("föôẞαr")
89

910
@testset "construction/conversion" begin
1011
@test StringView(s) === s
@@ -52,6 +53,19 @@ end
5253
@test isvalid(ss)
5354
end
5455

56+
@testset "iteration" begin
57+
for str in (s, ss, abc, invalid, su)
58+
sS = String(str)
59+
@test sS == str
60+
@test length(sS) == length(str)
61+
@test collect(sS) == collect(str) ==
62+
getindex.(sS, eachindex(sS)) == getindex.(str, eachindex(sS))
63+
@test collect(eachindex(sS)) == collect(eachindex(str))
64+
@test sS[1:end] == str[1:end]
65+
@test sS[nextind(sS,1):prevind(sS,end)] == str[nextind(str,1):prevind(str,end)]
66+
end
67+
end
68+
5569
@testset "regular expressions" begin
5670
for str in (s,ss)
5771
@test [m.match for m in collect(eachmatch(r"[aeiou]+", str))] == ["oo", "a"]

0 commit comments

Comments
 (0)