1
1
# This file is a part of StringEncodings.jl. License is MIT: http://julialang.org/license
2
2
3
3
module StringEncodings
4
+ using Base. Libc: errno, strerror, E2BIG, EINVAL, EILSEQ
5
+ using Compat: String, ASCIIString, UTF8String, view
6
+
4
7
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
7
9
import Compat: read
8
10
9
11
export StringEncoder, StringDecoder, encode, decode, encodings
10
12
export StringEncodingError, OutputBufferError, IConvError
11
13
export InvalidEncodingError, InvalidSequenceError, IncompleteSequenceError
12
14
13
15
include (" encodings.jl" )
16
+ using StringEncodings. Encodings
17
+ export encoding, encodings_list, Encoding, @enc_str
14
18
15
19
abstract StringEncodingError
16
20
@@ -31,7 +35,7 @@ message(::Type{InvalidSequenceError}) = "Byte sequence 0x<<1>> is invalid in sou
31
35
type IConvError <: StringEncodingError
32
36
args:: Tuple{ASCIIString, Int, ASCIIString}
33
37
end
34
- IConvError (func:: ASCIIString ) = IConvError ((func, errno (), strerror (errno ())))
38
+ IConvError (func:: String ) = IConvError ((func, errno (), strerror (errno ())))
35
39
message (:: Type{IConvError} ) = " <<1>>: <<2>> (<<3>>)"
36
40
37
41
# Input ended with incomplete byte sequence
@@ -65,7 +69,7 @@ function iconv_close(cd::Ptr{Void})
65
69
end
66
70
end
67
71
68
- function iconv_open (tocode:: ASCIIString , fromcode:: ASCIIString )
72
+ function iconv_open (tocode:: String , fromcode:: String )
69
73
p = ccall ((:iconv_open , libiconv), Ptr{Void}, (Cstring, Cstring), tocode, fromcode)
70
74
if p != Ptr {Void} (- 1 )
71
75
return p
@@ -192,7 +196,7 @@ stream is necessary to complete the encoding (but does not close `stream`).
192
196
`to` and `from` can be specified either as a string or as an `Encoding` object.
193
197
"""
194
198
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))
196
200
inbuf = Vector {UInt8} (BUFSIZE)
197
201
outbuf = Vector {UInt8} (BUFSIZE)
198
202
s = StringEncoder {typeof(from), typeof(to), typeof(stream)} (stream, false ,
@@ -225,7 +229,7 @@ function flush(s::StringEncoder)
225
229
s. outbytesleft[] = 0
226
230
while s. outbytesleft[] < BUFSIZE
227
231
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[]))))
229
233
end
230
234
231
235
s
@@ -268,7 +272,7 @@ Note that some implementations (notably the Windows one) may accept invalid sequ
268
272
in the input data without raising an error.
269
273
"""
270
274
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))
272
276
inbuf = Vector {UInt8} (BUFSIZE)
273
277
outbuf = Vector {UInt8} (BUFSIZE)
274
278
s = StringDecoder {typeof(from), typeof(to), typeof(stream)} (stream, false ,
@@ -303,7 +307,7 @@ function fill_buffer!(s::StringDecoder)
303
307
return i
304
308
end
305
309
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))
307
311
iconv! (s. cd, s. inbuf, s. outbuf, s. inbufptr, s. outbufptr, s. inbytesleft, s. outbytesleft)
308
312
end
309
313
449
453
decode([T,] a::Vector{UInt8}, enc)
450
454
451
455
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.
453
457
454
458
`enc` can be specified either as a string or as an `Encoding` object.
455
459
486
490
487
491
encode (s:: AbstractString , enc:: AbstractString ) = encode (s, Encoding (enc))
488
492
489
- function test_encoding (enc:: ASCIIString )
493
+ function test_encoding (enc:: String )
490
494
# We assume that an encoding is supported if it's possible to convert from it to UTF-8:
491
495
cd = ccall ((:iconv_open , libiconv), Ptr{Void}, (Cstring, Cstring), enc, " UTF-8" )
492
496
if cd == Ptr {Void} (- 1 )
0 commit comments