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 6f85c20

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

File tree

1 file changed

+31
-53
lines changed

1 file changed

+31
-53
lines changed

‎src/iconv.jl‎

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,53 @@ 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-
16-
# specified encodings are not supported
13+
# Specified encodings or the combination are not supported by iconv
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}
2216
end
17+
InvalidEncodingError(from, to) = InvalidEncodingError((from, to))
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}
3923
end
24+
InvalidSequenceError(seq) = InvalidSequenceError((bytes2hex(seq),))
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}
4629
end
30+
IConvError(func) = IConvError((func, errno(), strerror(errno())))
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(io::IO, exc::Union{IncompleteSequenceError,OutputBufferError,InvalidBytesError}) =
52+
print(io, message(typeof(esc)))
53+
7654
depsjl = joinpath(dirname(@__FILE__), "..", "deps", "deps.jl")
7755
isfile(depsjl) ? include(depsjl) : error("libiconv not properly installed. Please run\nPkg.build(\"iconv\")")
7856

@@ -82,7 +60,7 @@ isfile(depsjl) ? include(depsjl) : error("libiconv not properly installed. Pleas
8260
function iconv_close(cd::Ptr{Void})
8361
if cd != C_NULL
8462
ccall((:iconv_close, libiconv), Cint, (Ptr{Void},), cd) == 0 ||
85-
iconv_error("iconv_close")
63+
throw(IConvError("iconv_close"))
8664
end
8765
end
8866

@@ -93,7 +71,7 @@ function iconv_open(tocode, fromcode)
9371
elseif errno() == EINVAL
9472
throw(InvalidEncodingError(fromcode, tocode))
9573
else
96-
iconv_error("iconv_open")
74+
throw(IConvError("iconv_open"))
9775
end
9876
end
9977

@@ -158,10 +136,10 @@ function iconv!(cd::Ptr{Void}, inbuf::Vector{UInt8}, outbuf::Vector{UInt8},
158136
elseif err == E2BIG || err == EINVAL
159137
copy!(inbuf, 1, inbuf, inbytesleft_orig-inbytesleft[]+1, inbytesleft[])
160138
elseif err == EILSEQ
161-
seq = bytes2hex(inbuf[(inbytesleft_orig-inbytesleft[]+1):inbytesleft_orig])
139+
seq = inbuf[(inbytesleft_orig-inbytesleft[]+1):inbytesleft_orig]
162140
throw(InvalidSequenceError(seq))
163141
else
164-
iconv_error("iconv!")
142+
throw(IConvError("iconv!"))
165143
end
166144
end
167145

@@ -188,7 +166,7 @@ function iconv_reset!(s::Union{StringEncoder, StringDecoder})
188166
elseif err == EILSEQ
189167
throw(InvalidBytesError())
190168
else
191-
iconv_error("iconv_reset!")
169+
throw(IConvError("iconv_reset!"))
192170
end
193171
end
194172

0 commit comments

Comments
(0)

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