Skip to content

Commit 4cb98a3

Browse files
committed
Merge pull request #6 from ScottPJones/spj/handlenul
Don't strip any \0 bytes
2 parents 861063c + f79b5cc commit 4cb98a3

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

src/iconv.jl

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,7 @@ in the input data without raising an error.
266266
function decode(a::Vector{UInt8}, enc::ASCIIString)
267267
b = IOBuffer(a)
268268
try
269-
d = readbytes(StringDecoder(b, enc, "UTF-8"))
270-
# Skip final null bytes if needed
271-
# FIXME: find a better solution?
272-
i = length(d)
273-
while i >= 1
274-
d[i] != 0 && break
275-
i -= 1
276-
end
277-
UTF8String(d[1:i])
269+
UTF8String(readbytes(StringDecoder(b, enc, "UTF-8")))
278270
finally
279271
close(b)
280272
end

test/runtests.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using Base.Test
22
using iconv
33

4-
for s in ("", "a", "café crème",
4+
for s in ("", "\0", "a", "café crème",
55
"a"^(iconv.BUFSIZE-1) * "€ with an incomplete codepoint between two input buffer fills",
6-
"a string € チャネルパートナーの選択")
6+
"a string € チャネルパートナーの選択",
7+
"a string \0€ チャネルパ\0\0トナーの選択 with embedded and trailing nuls\0")
78
# Test round-trip to Unicode formats, checking against pure-Julia implementation
8-
for T in (UTF8String, UTF16String, UTF32String)
9+
for (T, nullen) in ((UTF8String, 0), (UTF16String, 2), (UTF32String, 4))
910
enc = iconv.encoding_string(T)
1011
a = reinterpret(UInt8, T(s).data)
12+
# Adjust for explicit \0 only for .data on UTF16String/UTF32String
13+
a = a[1:end - nullen]
1114
@test decode(a, enc) == s
1215
@test decode(encode(s, enc), enc) == s
1316
end
@@ -58,9 +61,9 @@ end
5861
# win_iconv currently does not throw an error on bytes >= 0x80 in ASCII sources
5962
# https://github.com/win-iconv/win-iconv/pull/26
6063
if OS_NAME != :Windows
61-
@test_throws ErrorException decode("qwertyé€".data, "ASCII")
64+
@test_throws ErrorException decode(b"qwertyé€", "ASCII")
6265
try
63-
decode("qwertyé€".data, "ASCII")
66+
decode(b"qwertyé€", "ASCII")
6467
catch err
6568
io = IOBuffer()
6669
showerror(io, err)

0 commit comments

Comments
 (0)