Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7444825

Browse files
Remove iconv_error, modulename, clean up show, per review
1 parent 7776b4d commit 7444825

File tree

1 file changed

+29
-52
lines changed

1 file changed

+29
-52
lines changed

‎src/iconv.jl‎

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,52 @@ module iconv
44
import Base: close, eof, flush, read, readall, write, show
55
import Base.Libc: errno, strerror, E2BIG, EINVAL, EILSEQ
66
export StringEncoder, StringDecoder, encode, decode
7-
export StringEncodingError, GeneralError
8-
export InvalidEncodingError, InvalidSequenceError, IncompleteSequenceError, IConvError
7+
export StringEncodingError, OutputBufferError, IConvError
8+
export InvalidEncodingError, InvalidSequenceError, IncompleteSequenceError
99

1010

1111
abstract StringEncodingError
1212

13-
# For now, don't display module name, maybe want it if this changes to StringEncoders
14-
const modulename = ""
15-
1613
# specified encodings are not supported
1714
type InvalidEncodingError <: StringEncodingError
18-
mod::ByteString
19-
msg::ByteString
20-
args
21-
InvalidEncodingError(from, to) = new(modulename, "Conversion from <<1>> to <<2>> not supported by iconv implementation, check that specified encodings are correct", (from, to))
15+
args::Tuple{ASCIIString, ASCIIString}
16+
InvalidEncodingError(from, to) = new((ascii(from), ascii(to)))
2217
end
18+
message(::Type{InvalidEncodingError}) = "Conversion from <<1>> to <<2>> not supported by iconv implementation, check that specified encodings are correct"
2319

2420
# Encountered invalid byte sequence
2521
type InvalidSequenceError <: StringEncodingError
26-
mod::ByteString
27-
msg::ByteString
28-
args
29-
InvalidSequenceError(seq) = new(modulename, "Byte sequence 0x<<1>> is invalid in source encoding or cannot be represented in target encoding", (seq,))
30-
end
31-
32-
# Input ended with incomplete byte sequence
33-
type IncompleteSequenceError <: StringEncodingError
34-
mod::ByteString
35-
msg::ByteString
36-
args
37-
IncompleteSequenceError() =
38-
new(modulename, "Incomplete byte sequence at end of input", ())
22+
args::Tuple{ASCIIString}
23+
InvalidSequenceError(seq) = new((bytes2hex(seq),))
3924
end
25+
message(::Type{InvalidSequenceError}) = "Byte sequence 0x<<1>> is invalid in source encoding or cannot be represented in target encoding"
4026

4127
type IConvError <: StringEncodingError
42-
mod::ByteString
43-
msg::ByteString
44-
args
45-
IConvError(func) = new(func, "<<1>> (<<2>>)", (errno(), strerror(errno())))
28+
args::Tuple{ASCIIString, Int, ASCIIString}
29+
IConvError(func) = new((func, errno(), strerror(errno())))
4630
end
31+
message(::Type{IConvError}) = "<<1>>: <<2>> (<<3>>)"
4732

48-
type OutputBufferError <: StringEncodingError
49-
mod::ByteString
50-
msg::ByteString
51-
args
52-
OutputBufferError(func) =
53-
new(modulename, "Ran out of space in the output buffer", ())
54-
end
33+
# Input ended with incomplete byte sequence
34+
type IncompleteSequenceError <: StringEncodingError ; end
35+
message(::Type{IncompleteSequenceError}) = "Incomplete byte sequence at end of input"
5536

56-
type InvalidBytesError <: StringEncodingError
57-
mod::ByteString
58-
msg::ByteString
59-
args
60-
InvalidBytesError() =
61-
new(modulename, "Invalid byte sequence in input", ())
62-
end
37+
type OutputBufferError <: StringEncodingError ; end
38+
message(::Type{OutputBufferError}) = "Ran out of space in the output buffer"
6339

64-
iconv_error(func) = throw(IConvError(func))
40+
type InvalidBytesError <: StringEncodingError ; end
41+
message(::Type{InvalidBytesError}) = "Invalid byte sequence in input"
6542

66-
function show{T <: StringEncodingError}(io::IO, exc::T)
67-
str = exc.mod == "" ? exc.msg : string(exc.mod, ": ", exc.msg)
68-
if contains(exc.msg, "<<")
69-
for i = 1:length(exc.args)
70-
str = replace(str, "<<$i>>", exc.args[i])
71-
end
43+
function show(io::IO, exc::StringEncodingError)
44+
str = message(typeof(exc))
45+
for i = 1:length(exc.args)
46+
str = replace(str, "<<$i>>", exc.args[i])
7247
end
7348
print(io, str)
7449
end
7550

51+
show{T<:Union{IncompleteSequenceError,OutputBufferError,InvalidBytesError}}(io::IO, exc::T) = print(io, message(T))
52+
7653
depsjl = joinpath(dirname(@__FILE__), "..", "deps", "deps.jl")
7754
isfile(depsjl) ? include(depsjl) : error("libiconv not properly installed. Please run\nPkg.build(\"iconv\")")
7855

@@ -82,7 +59,7 @@ isfile(depsjl) ? include(depsjl) : error("libiconv not properly installed. Pleas
8259
function iconv_close(cd::Ptr{Void})
8360
if cd != C_NULL
8461
ccall((:iconv_close, libiconv), Cint, (Ptr{Void},), cd) == 0 ||
85-
iconv_error("iconv_close")
62+
throw(IConvError("iconv_close"))
8663
end
8764
end
8865

@@ -93,7 +70,7 @@ function iconv_open(tocode, fromcode)
9370
elseif errno() == EINVAL
9471
throw(InvalidEncodingError(fromcode, tocode))
9572
else
96-
iconv_error("iconv_open")
73+
throw(IConvError("iconv_open"))
9774
end
9875
end
9976

@@ -158,10 +135,10 @@ function iconv!(cd::Ptr{Void}, inbuf::Vector{UInt8}, outbuf::Vector{UInt8},
158135
elseif err == E2BIG || err == EINVAL
159136
copy!(inbuf, 1, inbuf, inbytesleft_orig-inbytesleft[]+1, inbytesleft[])
160137
elseif err == EILSEQ
161-
seq = bytes2hex(inbuf[(inbytesleft_orig-inbytesleft[]+1):inbytesleft_orig])
138+
seq = inbuf[(inbytesleft_orig-inbytesleft[]+1):inbytesleft_orig]
162139
throw(InvalidSequenceError(seq))
163140
else
164-
iconv_error("iconv!")
141+
throw(IConvError("iconv!"))
165142
end
166143
end
167144

@@ -188,7 +165,7 @@ function iconv_reset!(s::Union{StringEncoder, StringDecoder})
188165
elseif err == EILSEQ
189166
throw(InvalidBytesError())
190167
else
191-
iconv_error("iconv_reset!")
168+
throw(IConvError("iconv_reset!"))
192169
end
193170
end
194171

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /