Skip to content

Commit 67acfac

Browse files
committed
Merge pull request #1297 from pguyot/w39/add-support-for-IO.chardata_to_string
Add support for Elixir `IO.chardata_to_string/1` These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 67157f5 + 5a40053 commit 67acfac

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
@@ -31,6 +31,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
3131
- Support for large tuples (more than 255 elements) in external terms.
3232
- Support for `io:put_chars/2`
3333
- Support for `lists:nthtail/2`
34+
- Support for Elixir `IO.chardata_to_string/1`
3435

3536
### Changed
3637

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)