Skip to content

Commit 9725e8d

Browse files
committed
Allow creating different types of strings from decode()
1 parent 300595d commit 9725e8d

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/StringEncodings.jl

+7-4
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,25 @@ encoding_string(::Type{UTF16String}) = (ENDIAN_BOM == 0x04030201) ? "UTF-16LE" :
298298
encoding_string(::Type{UTF32String}) = (ENDIAN_BOM == 0x04030201) ? "UTF-32LE" : "UTF-32BE"
299299

300300
"""
301-
decode(a::Vector{UInt8}, enc::ASCIIString)
301+
decode([T,] a::Vector{UInt8}, enc::ASCIIString)
302302
303-
Convert an array of bytes `a` representing text in encoding `enc` to a string.
303+
Convert an array of bytes `a` representing text in encoding `enc` to a string of type `T`.
304+
By default, a `UTF8String` is returned.
304305
305306
Note that some implementations (notably the Windows one) may accept invalid sequences
306307
in the input data without raising an error.
307308
"""
308-
function decode(a::Vector{UInt8}, enc::ASCIIString)
309+
function decode{T<:AbstractString}(::Type{T}, a::Vector{UInt8}, enc::ASCIIString)
309310
b = IOBuffer(a)
310311
try
311-
UTF8String(readbytes(StringDecoder(b, enc, "UTF-8")))
312+
T(readbytes(StringDecoder(b, enc, encoding_string(T))))
312313
finally
313314
close(b)
314315
end
315316
end
316317

318+
decode(a::Vector{UInt8}, enc::ASCIIString) = decode(UTF8String, a, enc)
319+
317320
"""
318321
encode(s::AbstractString, enc::ASCIIString)
319322

test/runtests.jl

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ for s in ("", "\0", "a", "café crème",
1212
# Adjust for explicit \0 only for .data on UTF16String/UTF32String
1313
a = a[1:end - nullen]
1414
@test decode(a, enc) == s
15+
@test decode(UTF16String, a, enc) == s
16+
@test decode(UTF32String, a, enc) == s
1517
@test decode(encode(s, enc), enc) == s
1618
end
1719
end

0 commit comments

Comments
 (0)