Skip to content

Commit

Permalink
Add support for Elixir IO.chardata_to_string/1
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Guyot <[email protected]>
  • Loading branch information
pguyot committed Sep 29, 2024
1 parent 4b37211 commit 972a038
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
- Support for `code:ensure_loaded/1`
- Support for `io_lib:latin1_char_list/1`
- Add support to Elixir for `Keyword.split/2`
- Support for Elixir `IO.chardata_to_string/1`

### Changed

Expand Down
36 changes: 36 additions & 0 deletions libs/exavmlib/lib/IO.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,42 @@ defmodule IO do
# This avoids crashing the compiler at build time
@compile {:autoload, false}

# Taken from Elixir io.ex
@type chardata :: String.t() | maybe_improper_list(char | chardata, String.t() | [])

# Taken from Elixir io.ex
@doc """
Converts chardata into a string.
For more information about chardata, see the ["Chardata"](#module-chardata)
section in the module documentation.
In case the conversion fails, it raises an `UnicodeConversionError`.
If a string is given, it returns the string itself.
## Examples
iex> IO.chardata_to_string([0x00E6, 0x00DF])
"æß"
iex> IO.chardata_to_string([0x0061, "bc"])
"abc"
iex> IO.chardata_to_string("string")
"string"
"""
@spec chardata_to_string(chardata) :: String.t()
def chardata_to_string(chardata)

def chardata_to_string(string) when is_binary(string) do
string
end

def chardata_to_string(list) when is_list(list) do
List.to_string(list)
end

# Taken from Elixir io.ex
@doc """
Converts iodata (a list of integers representing bytes, lists
Expand Down

0 comments on commit 972a038

Please sign in to comment.