Skip to content

Commit d231108

Browse files
authored
Fix handling of Shift_JISx0213 (#50)
For stateful encodings, we need to copy the contents of the output buffer to the stream after calling `iconv_reset!`. Otherwise the bytes that may be written remain in the buffer. We als oneed to `close` from `encode`, which incidentally ensures that objects can be freed immediately.
1 parent d102671 commit d231108

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StringEncodings"
22
uuid = "69024149-9ee7-55f6-a4c4-859efe599b68"
3-
version = "0.3.4"
3+
version = "0.3.5"
44

55
[deps]
66
Libiconv_jll = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"

src/StringEncodings.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ end
236236
function close(s::StringEncoder)
237237
flush(s)
238238
iconv_reset!(s)
239+
write(s.stream, view(s.outbuf, 1:(BUFSIZE - Int(s.outbytesleft[]))))
239240
# Make sure C memory/resources are returned
240241
finalize(s)
241242
if s.closestream
@@ -518,7 +519,7 @@ function encode(s::AbstractString, enc::Encoding)
518519
b = IOBuffer()
519520
p = StringEncoder(b, enc, encoding(typeof(s)))
520521
write(p, s)
521-
flush(p)
522+
close(p)
522523
take!(b)
523524
end
524525

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,18 @@ mktemp() do path, io
234234
@test_throws ArgumentError open(path, enc"ISO-2022-JP", true, false, false, false, true)
235235
end
236236

237+
@testset "Bytes written only when closing with Shift JIS (issue #49)" begin
238+
for enc in ("SHIFT_JIS", "SHIFT_JISX0213")
239+
@test encode("", enc) == [0x83, 0x4e]
240+
241+
b = IOBuffer()
242+
s = StringEncoder(b, enc)
243+
write(s, "")
244+
close(s)
245+
@test take!(b) == [0x83, 0x4e]
246+
end
247+
end
248+
237249

238250
## Test encodings support
239251
b = IOBuffer()

0 commit comments

Comments
 (0)