Skip to content

Commit fb60bb9

Browse files
committed
Add show() for StringEncoder/StringDecoder
1 parent a87aaa9 commit fb60bb9

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/StringEncodings.jl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ end
8181

8282
const BUFSIZE = 100
8383

84-
type StringEncoder{S<:IO} <: IO
84+
type StringEncoder{F<:Encoding, T<:Encoding, S<:IO} <: IO
8585
ostream::S
8686
closestream::Bool
8787
cd::Ptr{Void}
@@ -93,7 +93,7 @@ type StringEncoder{S<:IO} <: IO
9393
outbytesleft::Ref{Csize_t}
9494
end
9595

96-
type StringDecoder{S<:IO} <: IO
96+
type StringDecoder{F<:Encoding, T<:Encoding, S<:IO} <: IO
9797
istream::S
9898
closestream::Bool
9999
cd::Ptr{Void}
@@ -190,7 +190,8 @@ function StringEncoder(ostream::IO, to::Encoding, from::Encoding=enc"UTF-8")
190190
cd = iconv_open(ASCIIString(to), ASCIIString(from))
191191
inbuf = Vector{UInt8}(BUFSIZE)
192192
outbuf = Vector{UInt8}(BUFSIZE)
193-
s = StringEncoder(ostream, false, cd, inbuf, outbuf,
193+
s = StringEncoder{typeof(from), typeof(to), typeof(ostream)}(ostream, false,
194+
cd, inbuf, outbuf,
194195
Ref{Ptr{UInt8}}(pointer(inbuf)), Ref{Ptr{UInt8}}(pointer(outbuf)),
195196
Ref{Csize_t}(0), Ref{Csize_t}(BUFSIZE))
196197
finalizer(s, finalize)
@@ -202,6 +203,12 @@ StringEncoder(ostream::IO, to::AbstractString, from::Encoding=enc"UTF-8") =
202203
StringEncoder(ostream::IO, to::AbstractString, from::AbstractString) =
203204
StringEncoder(ostream, Encoding(to), Encoding(from))
204205

206+
function show{F, T, S}(io::IO, s::StringEncoder{F, T, S})
207+
from = F()
208+
to = T()
209+
print(io, "StringEncoder{$from, $to}($(s.ostream))")
210+
end
211+
205212
# Flush input buffer and convert it into output buffer
206213
# Returns the number of bytes written to output buffer
207214
function flush(s::StringEncoder)
@@ -256,7 +263,8 @@ function StringDecoder(istream::IO, from::Encoding, to::Encoding=enc"UTF-8")
256263
cd = iconv_open(ASCIIString(to), ASCIIString(from))
257264
inbuf = Vector{UInt8}(BUFSIZE)
258265
outbuf = Vector{UInt8}(BUFSIZE)
259-
s = StringDecoder(istream, false, cd, inbuf, outbuf,
266+
s = StringDecoder{typeof(from), typeof(to), typeof(istream)}(istream, false,
267+
cd, inbuf, outbuf,
260268
Ref{Ptr{UInt8}}(pointer(inbuf)), Ref{Ptr{UInt8}}(pointer(outbuf)),
261269
Ref{Csize_t}(0), Ref{Csize_t}(BUFSIZE), 0)
262270
finalizer(s, finalize)
@@ -268,6 +276,12 @@ StringDecoder(istream::IO, from::AbstractString, to::Encoding=enc"UTF-8") =
268276
StringDecoder(istream::IO, from::AbstractString, to::AbstractString) =
269277
StringDecoder(istream, Encoding(from), Encoding(to))
270278

279+
function show{F, T, S}(io::IO, s::StringDecoder{F, T, S})
280+
from = F()
281+
to = T()
282+
print(io, "StringDecoder{$from, $to}($(s.istream))")
283+
end
284+
271285
# Fill input buffer and convert it into output buffer
272286
# Returns the number of bytes written to output buffer
273287
function fill_buffer!(s::StringDecoder)

test/runtests.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ let s = "a string チャネルパートナーの選択"
3434
write(p, s.data)
3535
close(p)
3636

37-
p = StringEncoder(IOBuffer(), "UTF-16LE")
37+
b = IOBuffer()
38+
p = StringEncoder(b, "UTF-16LE")
39+
@test string(p) == "StringEncoder{UTF-8, UTF-16LE}($(string(b)))"
3840
write(p, s.data[1:10])
3941
@test_throws IncompleteSequenceError close(p)
4042

@@ -50,7 +52,9 @@ let s = "a string チャネルパートナーの選択"
5052
@test takebuf_string(io) == "Incomplete byte sequence at end of input"
5153
end
5254

53-
p = StringDecoder(IOBuffer(encode(s, "UTF-16LE")[1:19]), "UTF-16LE")
55+
b = IOBuffer(encode(s, "UTF-16LE")[1:19])
56+
p = StringDecoder(b, "UTF-16LE")
57+
@test string(p) == "StringDecoder{UTF-16LE, UTF-8}($(string(b)))"
5458
@test readstring(p) == s[1:9]
5559
@test_throws IncompleteSequenceError close(p)
5660

@@ -59,7 +63,8 @@ let s = "a string チャネルパートナーの選択"
5963
x = encode(s, "ISO-2022-JP")
6064
@test decode(x, "ISO-2022-JP") == s
6165

62-
p = StringDecoder(IOBuffer(x), "ISO-2022-JP", "UTF-8")
66+
p = StringDecoder(b, "ISO-2022-JP", "UTF-8")
67+
b = IOBuffer(x)
6368
# Test that closed pipe behaves correctly
6469
close(p)
6570
@test eof(p)

0 commit comments

Comments
 (0)