Skip to content

Commit 6cb0088

Browse files
authored
ensure proper writing of codeunits (#55)
1 parent 4117410 commit 6cb0088

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "InlineStrings"
22
uuid = "842dd82b-1e85-43dc-bf29-5d0ee9dffc48"
33
authors = ["Jacob Quinn <quinn.jacobd@gmail.com> and contributors"]
4-
version = "1.3.0"
4+
version = "1.3.1"
55

66
[deps]
77
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"

src/InlineStrings.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,15 @@ function Base.write(io::IO, x::T) where {T <: InlineString}
337337
end
338338
end
339339

340+
# without this method the fallback will try to write more than the codeunits
341+
function Base.write(io::IO, x::Base.CodeUnits{UInt8,<:InlineString})
342+
s = 0
343+
for j 1:ncodeunits(x.s)
344+
s += write(io, codeunit(x.s, j))
345+
end
346+
s
347+
end
348+
340349
function Base.read(s::IO, ::Type{T}) where {T <: InlineString}
341350
return read!(s, Ref{T}())[]::T
342351
end

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ S = InlineString15
127127
@test_throws ArgumentError chop(S("∀ϵ∃Δ"), head=3, tail=-3)
128128
@test_throws ArgumentError chop(S("∀ϵ∃Δ"), head=-3, tail=-3)
129129

130+
# check that writing code units works as expected
131+
io = IOBuffer()
132+
cu = codeunits(InlineString("hello"))
133+
@test write(io, cu) == 5
134+
@test take!(io) == codeunits("hello")
135+
130136
if isdefined(Base, :chopprefix)
131137
@test chopprefix(abc, "a") === InlineString3("bc")
132138
@test chopprefix(abc, "bc") === abc

0 commit comments

Comments
 (0)