Skip to content

Commit 5a40053

Browse files
committed
Add support for Elixir IO.chardata_to_string/1
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent 389ec97 commit 5a40053

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
2929
- Add support to Elixir for `Keyword.split/2`
3030
- Support for `binary:split/3` and `string:find/2,3`
3131
- Support for large tuples (more than 255 elements) in external terms.
32+
- Support for Elixir `IO.chardata_to_string/1`
3233

3334
### Changed
3435

libs/exavmlib/lib/IO.ex

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,42 @@ defmodule IO do
2424
# This avoids crashing the compiler at build time
2525
@compile {:autoload, false}
2626

27+
# Taken from Elixir io.ex
28+
@type chardata :: String.t() | maybe_improper_list(char | chardata, String.t() | [])
29+
30+
# Taken from Elixir io.ex
31+
@doc """
32+
Converts chardata into a string.
33+
34+
For more information about chardata, see the ["Chardata"](#module-chardata)
35+
section in the module documentation.
36+
37+
In case the conversion fails, it raises an `UnicodeConversionError`.
38+
If a string is given, it returns the string itself.
39+
40+
## Examples
41+
42+
iex> IO.chardata_to_string([0x00E6, 0x00DF])
43+
"æß"
44+
45+
iex> IO.chardata_to_string([0x0061, "bc"])
46+
"abc"
47+
48+
iex> IO.chardata_to_string("string")
49+
"string"
50+
51+
"""
52+
@spec chardata_to_string(chardata) :: String.t()
53+
def chardata_to_string(chardata)
54+
55+
def chardata_to_string(string) when is_binary(string) do
56+
string
57+
end
58+
59+
def chardata_to_string(list) when is_list(list) do
60+
List.to_string(list)
61+
end
62+
2763
# Taken from Elixir io.ex
2864
@doc """
2965
Converts iodata (a list of integers representing bytes, lists

0 commit comments

Comments
 (0)