Skip to content

Commit e6fd1bf

Browse files
committed
Fix 0.5 deprecations using LegacyEncodings
The package isn't strictly needed, but we need to depend on it just to provide encodings() methods for legacy string types on 0.5, which can always be useful to people working with encodings (until encoded strings are supported directly by the package).
1 parent 9ddb506 commit e6fd1bf

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

REQUIRE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
julia 0.4
22
BinDeps
3-
Compat
3+
Compat 0.8.0
44
@windows WinRPM
5+
LegacyStrings 0.1.1

src/StringEncodings.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
# This file is a part of StringEncodings.jl. License is MIT: http://julialang.org/license
22

33
module StringEncodings
4+
using Base.Libc: errno, strerror, E2BIG, EINVAL, EILSEQ
5+
using Compat: String, ASCIIString, UTF8String, view
6+
47
import Base: close, eachline, eof, flush, isreadable, iswritable,
5-
open, read, readline, readlines, readuntil, show, write
6-
import Base.Libc: errno, strerror, E2BIG, EINVAL, EILSEQ
8+
open, readline, readlines, readuntil, show, write
79
import Compat: read
810

911
export StringEncoder, StringDecoder, encode, decode, encodings
1012
export StringEncodingError, OutputBufferError, IConvError
1113
export InvalidEncodingError, InvalidSequenceError, IncompleteSequenceError
1214

1315
include("encodings.jl")
16+
using StringEncodings.Encodings
17+
export encoding, encodings_list, Encoding, @enc_str
1418

1519
abstract StringEncodingError
1620

@@ -31,7 +35,7 @@ message(::Type{InvalidSequenceError}) = "Byte sequence 0x<<1>> is invalid in sou
3135
type IConvError <: StringEncodingError
3236
args::Tuple{ASCIIString, Int, ASCIIString}
3337
end
34-
IConvError(func::ASCIIString) = IConvError((func, errno(), strerror(errno())))
38+
IConvError(func::String) = IConvError((func, errno(), strerror(errno())))
3539
message(::Type{IConvError}) = "<<1>>: <<2>> (<<3>>)"
3640

3741
# Input ended with incomplete byte sequence
@@ -65,7 +69,7 @@ function iconv_close(cd::Ptr{Void})
6569
end
6670
end
6771

68-
function iconv_open(tocode::ASCIIString, fromcode::ASCIIString)
72+
function iconv_open(tocode::String, fromcode::String)
6973
p = ccall((:iconv_open, libiconv), Ptr{Void}, (Cstring, Cstring), tocode, fromcode)
7074
if p != Ptr{Void}(-1)
7175
return p
@@ -192,7 +196,7 @@ stream is necessary to complete the encoding (but does not close `stream`).
192196
`to` and `from` can be specified either as a string or as an `Encoding` object.
193197
"""
194198
function StringEncoder(stream::IO, to::Encoding, from::Encoding=enc"UTF-8")
195-
cd = iconv_open(ASCIIString(to), ASCIIString(from))
199+
cd = iconv_open(String(to), String(from))
196200
inbuf = Vector{UInt8}(BUFSIZE)
197201
outbuf = Vector{UInt8}(BUFSIZE)
198202
s = StringEncoder{typeof(from), typeof(to), typeof(stream)}(stream, false,
@@ -225,7 +229,7 @@ function flush(s::StringEncoder)
225229
s.outbytesleft[] = 0
226230
while s.outbytesleft[] < BUFSIZE
227231
iconv!(s.cd, s.inbuf, s.outbuf, s.inbufptr, s.outbufptr, s.inbytesleft, s.outbytesleft)
228-
write(s.stream, sub(s.outbuf, 1:(BUFSIZE - Int(s.outbytesleft[]))))
232+
write(s.stream, view(s.outbuf, 1:(BUFSIZE - Int(s.outbytesleft[]))))
229233
end
230234

231235
s
@@ -268,7 +272,7 @@ Note that some implementations (notably the Windows one) may accept invalid sequ
268272
in the input data without raising an error.
269273
"""
270274
function StringDecoder(stream::IO, from::Encoding, to::Encoding=enc"UTF-8")
271-
cd = iconv_open(ASCIIString(to), ASCIIString(from))
275+
cd = iconv_open(String(to), String(from))
272276
inbuf = Vector{UInt8}(BUFSIZE)
273277
outbuf = Vector{UInt8}(BUFSIZE)
274278
s = StringDecoder{typeof(from), typeof(to), typeof(stream)}(stream, false,
@@ -303,7 +307,7 @@ function fill_buffer!(s::StringDecoder)
303307
return i
304308
end
305309

306-
s.inbytesleft[] += readbytes!(s.stream, sub(s.inbuf, Int(s.inbytesleft[]+1):BUFSIZE))
310+
s.inbytesleft[] += readbytes!(s.stream, view(s.inbuf, Int(s.inbytesleft[]+1):BUFSIZE))
307311
iconv!(s.cd, s.inbuf, s.outbuf, s.inbufptr, s.outbufptr, s.inbytesleft, s.outbytesleft)
308312
end
309313

@@ -449,7 +453,7 @@ end
449453
decode([T,] a::Vector{UInt8}, enc)
450454
451455
Convert an array of bytes `a` representing text in encoding `enc` to a string of type `T`.
452-
By default, a `UTF8String` is returned.
456+
By default, a `String` is returned.
453457
454458
`enc` can be specified either as a string or as an `Encoding` object.
455459
@@ -486,7 +490,7 @@ end
486490

487491
encode(s::AbstractString, enc::AbstractString) = encode(s, Encoding(enc))
488492

489-
function test_encoding(enc::ASCIIString)
493+
function test_encoding(enc::String)
490494
# We assume that an encoding is supported if it's possible to convert from it to UTF-8:
491495
cd = ccall((:iconv_open, libiconv), Ptr{Void}, (Cstring, Cstring), enc, "UTF-8")
492496
if cd == Ptr{Void}(-1)

src/encodings.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
# Parametric singleton type representing a given string encoding via its symbol parameter
44

5+
module Encodings
6+
7+
using Compat
58
import Base: show, print, convert
6-
export Encoding, @enc_str
9+
export encoding, encodings_list, Encoding, @enc_str
10+
11+
if VERSION >= v"0.5.0-"
12+
using LegacyStrings: ASCIIString, UTF8String, UTF16String, UTF32String
13+
end
714

815
immutable Encoding{enc} end
916

10-
Encoding(s) = Encoding{symbol(s)}()
17+
Encoding(s) = Encoding{Symbol(s)}()
1118
macro enc_str(s)
12-
:(Encoding{$(Expr(:quote, symbol(s)))}())
19+
:(Encoding{$(Expr(:quote, Symbol(s)))}())
1320
end
1421

1522
convert{T<:AbstractString, enc}(::Type{T}, ::Encoding{enc}) = string(enc)
@@ -19,6 +26,10 @@ print{enc}(io::IO, ::Encoding{enc}) = print(io, enc)
1926

2027

2128
## Get the encoding used by a string type
29+
if VERSION >= v"0.5.0-"
30+
encoding(::Type{String}) = enc"UTF-8"
31+
end
32+
2233
encoding(::Type{ASCIIString}) = enc"ASCII"
2334
encoding(::Type{UTF8String}) = enc"UTF-8"
2435

@@ -300,3 +311,4 @@ encodings_list = ["1026", "1046", "1047", "10646-1:1993", "10646-1:1993/UCS4",
300311
"x-mac-icelandic", "x-mac-japanese", "x-mac-korean", "x-mac-romanian",
301312
"x-mac-thai", "x-mac-turkish", "x-mac-ukrainian", "X0201", "X0208",
302313
"X0212", "YU"]
314+
end # module

test/runtests.jl

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Base.Test
2-
import Compat: readstring
2+
using Compat: readstring
3+
using LegacyStrings: UTF8String, UTF16String, UTF32String
34
using StringEncodings
45

56
for s in ("", "\0", "a", "café crème",
@@ -89,18 +90,14 @@ catch err
8990
"Byte sequence 0xc3a9e282ac is invalid in source encoding or cannot be represented in target encoding"
9091
end
9192

92-
# win_iconv currently does not throw an error on bytes >= 0x80 in ASCII sources
93-
# https://github.com/win-iconv/win-iconv/pull/26
94-
if OS_NAME != :Windows
95-
@test_throws InvalidSequenceError decode(b"qwertyé€", "ASCII")
96-
try
97-
decode(b"qwertyé€", "ASCII")
98-
catch err
99-
io = IOBuffer()
100-
showerror(io, err)
101-
@test takebuf_string(io) ==
102-
"Byte sequence 0xc3a9e282ac is invalid in source encoding or cannot be represented in target encoding"
103-
end
93+
@test_throws InvalidSequenceError decode(b"qwertyé€", "ASCII")
94+
try
95+
decode(b"qwertyé€", "ASCII")
96+
catch err
97+
io = IOBuffer()
98+
showerror(io, err)
99+
@test takebuf_string(io) ==
100+
"Byte sequence 0xc3a9e282ac is invalid in source encoding or cannot be represented in target encoding"
104101
end
105102

106103
let x = encode("ÄÆä", "ISO-8859-1")

0 commit comments

Comments
 (0)