Skip to content

Commit 47cb653

Browse files
committed
Don't strip any \0 bytes
Fix tests to handle inconsistencies with \0 termination between UTF8String, UTF16String, and UTF32String in Julia
1 parent eebb165 commit 47cb653

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

src/iconv.jl

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,7 @@ Convert an array of bytes `a` representing text in encoding `enc` to a string.
260260
function decode(a::Vector{UInt8}, enc::ASCIIString)
261261
b = IOBuffer(a)
262262
try
263-
d = readbytes(StringDecoder(b, enc, "UTF-8"))
264-
# Skip final null bytes if needed
265-
# FIXME: find a better solution?
266-
i = length(d)
267-
while i >= 1
268-
d[i] != 0 && break
269-
i -= 1
270-
end
271-
UTF8String(d[1:i])
263+
UTF8String(readbytes(StringDecoder(b, enc, "UTF-8")))
272264
finally
273265
close(b)
274266
end

test/runtests.jl

Lines changed: 6 additions & 3 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

0 commit comments

Comments
 (0)