|
3 | 3 | module StringEncodings
|
4 | 4 | import Base: close, eof, flush, read, readall, write, show
|
5 | 5 | import Base.Libc: errno, strerror, E2BIG, EINVAL, EILSEQ
|
6 |
| -export StringEncoder, StringDecoder, encode, decode |
| 6 | + |
| 7 | +export StringEncoder, StringDecoder, encode, decode, encodings |
7 | 8 | export StringEncodingError, OutputBufferError, IConvError
|
8 | 9 | export InvalidEncodingError, InvalidSequenceError, IncompleteSequenceError
|
9 | 10 |
|
@@ -326,4 +327,36 @@ function encode(s::AbstractString, enc::ASCIIString)
|
326 | 327 | takebuf_array(b)
|
327 | 328 | end
|
328 | 329 |
|
| 330 | +## Function to list supported encodings |
| 331 | +include("encodings.jl") |
| 332 | + |
| 333 | +function test_encoding(enc) |
| 334 | + # We assume that an encoding is supported if it's possible to convert from it to UTF-8: |
| 335 | + cd = ccall((:iconv_open, libiconv), Ptr{Void}, (Cstring, Cstring), enc, "UTF-8") |
| 336 | + if cd == Ptr{Void}(-1) |
| 337 | + return false |
| 338 | + else |
| 339 | + iconv_close(cd) |
| 340 | + return true |
| 341 | + end |
| 342 | +end |
| 343 | + |
| 344 | +""" |
| 345 | + encodings() |
| 346 | +
|
| 347 | +List all encodings supported by `encode`, `decode`, `StringEncoder` and `StringDecoder` |
| 348 | +(i.e. by the current iconv implementation). |
| 349 | +
|
| 350 | +Note that encodings typically appear several times under different names. |
| 351 | +In addition to the encodings returned by this function, the empty string (i.e. `""`) |
| 352 | +is equivalent to the encoding of the current locale. |
| 353 | +
|
| 354 | +Some implementations may support even more encodings: this can be checked by attempting |
| 355 | +a conversion. In theory, it is not guaranteed that all conversions between all pairs of encodings |
| 356 | +are possible; but this is the case with all reasonable implementations. |
| 357 | +""" |
| 358 | +function encodings() |
| 359 | + filter(test_encoding, encodings_list) |
| 360 | +end |
| 361 | + |
329 | 362 | end # module
|
0 commit comments