Skip to content

Commit eebb165

Browse files
committed
Merge pull request #2 from ScottPJones/spj/fixtests
Fix tests
2 parents c5c6f84 + bb85900 commit eebb165

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/iconv.jl

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function iconv_open(tocode, fromcode)
2525
elseif errno() == EINVAL
2626
error("conversion from $fromcode to $tocode not supported by iconv implementation, check that specified encodings are correct")
2727
else
28-
error("iconv_open error $(errno()): $(strerror(errno()))")
28+
error("iconv_open error $(errno()): $(strerror(errno()))")
2929
end
3030
end
3131

@@ -57,6 +57,15 @@ type StringDecoder{S<:IO} <: IO
5757
skip::Int
5858
end
5959

60+
# This is called during GC, just make sure C memory is returned, don't throw errors
61+
function finalize(s::Union{StringEncoder, StringDecoder})
62+
if s.cd != C_NULL
63+
iconv_close(s.cd)
64+
s.cd = C_NULL
65+
end
66+
nothing
67+
end
68+
6069
function iconv!(cd::Ptr{Void}, inbuf::Vector{UInt8}, outbuf::Vector{UInt8},
6170
inbufptr::Ref{Ptr{UInt8}}, outbufptr::Ref{Ptr{UInt8}},
6271
inbytesleft::Ref{Csize_t}, outbytesleft::Ref{Csize_t})
@@ -70,7 +79,7 @@ function iconv!(cd::Ptr{Void}, inbuf::Vector{UInt8}, outbuf::Vector{UInt8},
7079
(Ptr{Void}, Ptr{Ptr{UInt8}}, Ref{Csize_t}, Ptr{Ptr{UInt8}}, Ref{Csize_t}),
7180
cd, inbufptr, inbytesleft, outbufptr, outbytesleft)
7281

73-
if ret == reinterpret(Csize_t, -1)
82+
if ret == -1 % Csize_t
7483
err = errno()
7584

7685
# Should never happen unless a very small buffer is used
@@ -102,7 +111,7 @@ function iconv_reset!(s::Union{StringEncoder, StringDecoder})
102111
(Ptr{Void}, Ptr{Ptr{UInt8}}, Ref{Csize_t}, Ptr{Ptr{UInt8}}, Ref{Csize_t}),
103112
s.cd, C_NULL, C_NULL, s.outbufptr, s.outbytesleft)
104113

105-
if ret == reinterpret(Csize_t, -1)
114+
if ret == -1 % Csize_t
106115
err = errno()
107116
if err == EINVAL
108117
error("iconv error: incomplete byte sequence at end of input")
@@ -135,7 +144,7 @@ function StringEncoder(ostream::IO, to::ASCIIString, from::ASCIIString="UTF-8")
135144
s = StringEncoder(ostream, cd, inbuf, outbuf,
136145
Ref{Ptr{UInt8}}(pointer(inbuf)), Ref{Ptr{UInt8}}(pointer(outbuf)),
137146
Ref{Csize_t}(0), Ref{Csize_t}(BUFSIZE))
138-
finalizer(s, close)
147+
finalizer(s, finalize)
139148
s
140149
end
141150

@@ -157,11 +166,10 @@ function flush(s::StringEncoder)
157166
end
158167

159168
function close(s::StringEncoder)
160-
s.cd == C_NULL && return s
161169
flush(s)
162170
iconv_reset!(s)
163-
iconv_close(s.cd)
164-
s.cd = C_NULL
171+
# Make sure C memory/resources are returned
172+
finalize(s)
165173
# flush() wasn't able to empty input buffer, which cannot happen with correct data
166174
s.inbytesleft[] == 0 || error("iconv error: incomplete byte sequence at end of input")
167175
end
@@ -188,7 +196,7 @@ function StringDecoder(istream::IO, from::ASCIIString, to::ASCIIString="UTF-8")
188196
s = StringDecoder(istream, cd, inbuf, outbuf,
189197
Ref{Ptr{UInt8}}(pointer(inbuf)), Ref{Ptr{UInt8}}(pointer(outbuf)),
190198
Ref{Csize_t}(0), Ref{Csize_t}(BUFSIZE), 0)
191-
finalizer(s, close)
199+
finalizer(s, finalize)
192200
s
193201
end
194202

@@ -221,10 +229,10 @@ function eof(s::StringDecoder)
221229
end
222230

223231
function close(s::StringDecoder)
224-
s.cd == C_NULL && return s
225-
iconv_close(s.cd)
226-
s.cd = C_NULL
227-
# fill_buffer!() wasn't able to empty input buffer, which cannot happen with correct data
232+
iconv_reset!(s)
233+
# Make sure C memory/resources are returned
234+
finalize(s)
235+
# iconv_reset!() wasn't able to empty input buffer, which cannot happen with correct data
228236
s.inbytesleft[] == 0 || error("iconv error: incomplete byte sequence at end of input")
229237
end
230238

@@ -240,9 +248,9 @@ end
240248
## Functions to encode/decode strings
241249

242250
encoding_string(::Type{ASCIIString}) = "ASCII"
243-
encoding_string(::Type{UTF8String}) = "UTF-8"
244-
encoding_string(::Type{UTF16String}) = "UTF-16LE"
245-
encoding_string(::Type{UTF32String}) = "UTF-32LE"
251+
encoding_string(::Type{UTF8String}) = "UTF-8"
252+
encoding_string(::Type{UTF16String}) = (ENDIAN_BOM == 0x04030201) ? "UTF-16LE" : "UTF-16BE"
253+
encoding_string(::Type{UTF32String}) = (ENDIAN_BOM == 0x04030201) ? "UTF-32LE" : "UTF-32BE"
246254

247255
"""
248256
decode(a::Vector{UInt8}, enc::ASCIIString)

test/runtests.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ for s in ("", "a", "café crème",
1414
end
1515

1616
# Test a few non-Unicode encodings
17-
for (s, enc) in (("noël", "ISO-8859-1"), ("noël €", "ISO-8859-15", "CP1252"),
18-
("Код Обмена Информацией, 8 бит", "KOI8-R"), ("国家标准", "GB18030"))
17+
for (s, enc) in (("noël", "ISO-8859-1"),
18+
("noël €", "ISO-8859-15", "CP1252"),
19+
("Код Обмена Информацией, 8 бит", "KOI8-R"),
20+
("国家标准", "GB18030"))
1921
@test decode(encode(s, enc), enc) == s
2022
end
2123

@@ -76,3 +78,5 @@ end
7678

7779
@test_throws ErrorException p = StringEncoder(IOBuffer(), "nonexistent_encoding")
7880
@test_throws ErrorException p = StringDecoder(IOBuffer(), "nonexistent_encoding")
81+
82+
nothing

0 commit comments

Comments
 (0)