11# This file is a part of StringEncodings.jl. License is MIT: http://julialang.org/license
22
33module StringEncodings
4+ using Base. Libc: errno, strerror, E2BIG, EINVAL, EILSEQ
5+ using Compat: String, ASCIIString, UTF8String, view
6+
47import 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
79import Compat: read
810
911export StringEncoder, StringDecoder, encode, decode, encodings
1012export StringEncodingError, OutputBufferError, IConvError
1113export InvalidEncodingError, InvalidSequenceError, IncompleteSequenceError
1214
1315include (" encodings.jl" )
16+ using StringEncodings. Encodings
17+ export encoding, encodings_list, Encoding, @enc_str
1418
1519abstract StringEncodingError
1620
@@ -31,7 +35,7 @@ message(::Type{InvalidSequenceError}) = "Byte sequence 0x<<1>> is invalid in sou
3135type IConvError <: StringEncodingError
3236 args:: Tuple{ASCIIString, Int, ASCIIString}
3337end
34- IConvError (func:: ASCIIString ) = IConvError ((func, errno (), strerror (errno ())))
38+ IConvError (func:: String ) = IConvError ((func, errno (), strerror (errno ())))
3539message (:: 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
6670end
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"""
194198function 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
268272in the input data without raising an error.
269273"""
270274function 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)
308312end
309313
449453 decode([T,] a::Vector{UInt8}, enc)
450454
451455Convert 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
486490
487491encode (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 )
0 commit comments