Skip to content

Commit 2185a52

Browse files
committed
sizeof bugfix and Base.reverse
1 parent 4c1eaa1 commit 2185a52

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/StringViews.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Base.unsafe_convert(::Type{Ptr{Int8}}, s::DenseStringViewAndSub) = convert(Ptr{I
4747
Base.cconvert(::Type{Ptr{UInt8}}, s::SubString{<:DenseStringView}) = s
4848
Base.cconvert(::Type{Ptr{Int8}}, s::SubString{<:DenseStringView}) = s
4949

50-
Base.sizeof(s::StringView) = sizeof(s.data)
50+
Base.sizeof(s::StringView) = length(s.data)
5151
Base.ncodeunits(s::StringView) = length(s.data)
5252
Base.codeunit(s::StringView) = UInt8
5353
Base.@propagate_inbounds Base.codeunit(s::StringView, i::Integer) = s.data[i]
@@ -98,6 +98,19 @@ function Base.hash(s::DenseStringViewAndSub, h::UInt)
9898
ccall(Base.memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), s, ncodeunits(s), h % UInt32) + h
9999
end
100100

101+
# each string type must implement its own reverse because it is generally
102+
# encoding-dependent
103+
function Base.reverse(s::StringViewAndSub)::String
104+
# Read characters forwards from `s` and write backwards to `out`
105+
out = Base._string_n(sizeof(s))
106+
offs = sizeof(s) + 1
107+
for c in s
108+
offs -= ncodeunits(c)
109+
Base.__unsafe_string!(out, c, offs)
110+
end
111+
return out
112+
end
113+
101114
include("decoding.jl")
102115
include("regex.jl")
103116

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ end
9696
@test !isvalid(invalid)
9797
@test !invoke(isvalid, Tuple{StringView}, invalid)
9898

99-
for str in (s, abc, invalid, ss)
100-
@test hash(str) == hash(String(str))
99+
for str in (s, abc, invalid, ss), f in (hash, reverse)
100+
@test f(str) == f(String(str))
101101
end
102102
end

0 commit comments

Comments
 (0)