Skip to content

Commit 7e4b8bb

Browse files
committed
Add encodings() to list supported encodings
1 parent 01ac5d1 commit 7e4b8bb

File tree

3 files changed

+309
-1
lines changed

3 files changed

+309
-1
lines changed

src/StringEncodings.jl

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
module StringEncodings
44
import Base: close, eof, flush, read, readall, write, show
55
import Base.Libc: errno, strerror, E2BIG, EINVAL, EILSEQ
6-
export StringEncoder, StringDecoder, encode, decode
6+
7+
export StringEncoder, StringDecoder, encode, decode, encodings
78
export StringEncodingError, OutputBufferError, IConvError
89
export InvalidEncodingError, InvalidSequenceError, IncompleteSequenceError
910

@@ -326,4 +327,36 @@ function encode(s::AbstractString, enc::ASCIIString)
326327
takebuf_array(b)
327328
end
328329

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+
329362
end # module

0 commit comments

Comments
 (0)