io_lib

IO Library Functions

This module contains functions for converting to and from strings (lists of characters). They are used for implementing the functions in the io module. There is no guarantee that the character lists returned from some of the functions are flat, they can be deep lists. lists:flatten/1 can be used for flattening deep lists.

Types


chars() = [char() | chars()]

continuation()

A continuation as returned by fread/3.

depth() = -1 | integer() >= 0

fread_error() =
            atom |
            based |
            character |
            float |
            format |
            input |
            integer |
            string |
            unsigned

fread_item() = string() | atom() | integer() | float()

latin1_string() = [unicode:latin1_char()]

format_spec() =
            #{control_char => char(),
              args => [any()],
              width => none | integer(),
              adjust => left | right,
              precision => none | integer(),
              pad_char => char(),
              encoding => unicode | latin1,
              strings => boolean()}

Description:

control_char is the type of control sequence: $P, $w, and so on;

args is a list of the arguments used by the control sequence, or an empty list if the control sequence does not take any arguments;

width is the field width;

adjust is the adjustment;

precision is the precision of the printed argument;

pad_char is the padding character;

encoding is set to true if the translation modifier t is present;

strings is set to false if the modifier l is present.

Functions


nl() -> string()

Returns a character list which represents a new line character.

write(Term) -> chars()

  • Term = term()

write(Term, Depth) -> chars()

Returns a character list which represents Term. The Depth (-1) argument controls the depth of the structures written. When the specified depth is reached, everything below this level is replaced by "...". For example:

1> lists:flatten(io_lib:write({1,[2],[3],[4,5],6,7,8,9})).
"{1,[2],[3],[4,5],6,7,8,9}"
2> lists:flatten(io_lib:write({1,[2],[3],[4,5],6,7,8,9}, 5)).
"{1,[2],[3],[...],...}"

print(Term) -> chars()

  • Term = term()

print(Term, Column, LineLength, Depth) -> chars()

  • Term = term()
  • Column = LineLength = integer() >= 0
  • Depth = depth()

Also returns a list of characters which represents Term, but breaks representations which are longer than one line into many lines and indents each line sensibly. It also tries to detect and output lists of printable characters as strings. Column is the starting column (1), LineLength the maximum line length (80), and Depth (-1) the maximum print depth.

fwrite(Format, Data) -> chars()

format(Format, Data) -> chars()

Returns a character list which represents Data formatted in accordance with Format. See io:fwrite/1,2,3 for a detailed description of the available formatting options. A fault is generated if there is an error in the format string or argument list.

If (and only if) the Unicode translation modifier is used in the format string (i.e. ~ts or ~tc), the resulting list may contain characters beyond the ISO-latin-1 character range (in other words, numbers larger than 255). If so, the result is not an ordinary Erlang string(), but can well be used in any context where Unicode data is allowed.

fread(Format, String) -> Result

  • Format = String = string()
  • Result =
        {ok, InputList :: [fread_item()], LeftOverChars :: string()} |
        {more,
         RestFormat :: string(),
         Nchars :: integer() >= 0,
         InputStack :: chars()} |
        {error, {fread, What :: fread_error()}}

Tries to read String in accordance with the control sequences in Format. See io:fread/3 for a detailed description of the available formatting options. It is assumed that String contains whole lines. It returns:

{ok, InputList, LeftOverChars}

The string was read. InputList is the list of successfully matched and read items, and LeftOverChars are the input characters not used.

{more, RestFormat, Nchars, InputStack}

The string was read, but more input is needed in order to complete the original format string. RestFormat is the remaining format string, Nchars the number of characters scanned, and InputStack is the reversed list of inputs matched up to that point.

{error, What}

The read operation failed and the parameter What gives a hint about the error.

Example:

3> io_lib:fread("~f~f~f", "15.6 17.3e-6 24.5").
{ok,[15.6,1.73e-5,24.5],[]}

fread(Continuation, CharSpec, Format) -> Return

  • Continuation = continuation() | []
  • CharSpec = string() | eof
  • Format = string()
  • Return =
        {more, Continuation1 :: continuation()} |
        {done, Result, LeftOverChars :: string()}
  • Result =
        {ok, InputList :: [fread_item()]} |
        eof |
        {error, {fread, What :: fread_error()}}

This is the re-entrant formatted reader. The continuation of the first call to the functions must be []. Refer to Armstrong, Virding, Williams, 'Concurrent Programming in Erlang', Chapter 13 for a complete description of how the re-entrant input scheme works.

The function returns:

{done, Result, LeftOverChars}

The input is complete. The result is one of the following:

{ok, InputList}

The string was read. InputList is the list of successfully matched and read items, and LeftOverChars are the remaining characters.

eof

End of file has been encountered. LeftOverChars are the input characters not used.

{error, What}

An error occurred and the parameter What gives a hint about the error.

{more, Continuation}

More data is required to build a term. Continuation must be passed to fread/3, when more data becomes available.

write_atom(Atom) -> chars()

  • Atom = atom()

Returns the list of characters needed to print the atom Atom.

write_string(String) -> chars()

  • String = string()

Returns the list of characters needed to print String as a string.

write_string_as_latin1(String) -> latin1_string()

  • String = string()

Returns the list of characters needed to print String as a string. Non-Latin-1 characters are escaped.

write_latin1_string(Latin1String) -> latin1_string()

Returns the list of characters needed to print Latin1String as a string.

write_char(Char) -> chars()

  • Char = char()

Returns the list of characters needed to print a character constant in the Unicode character set.

write_char_as_latin1(Char) -> latin1_string()

  • Char = char()

Returns the list of characters needed to print a character constant in the Unicode character set. Non-Latin-1 characters are escaped.

write_latin1_char(Latin1Char) -> latin1_string()

Returns the list of characters needed to print a character constant in the ISO-latin-1 character set.

scan_format(Format, Data) -> FormatList

Returns a list corresponding to the given format string, where control sequences have been replaced with corresponding tuples. This list can be passed to io_lib:build_text/1 to have the same effect as io_lib:format(Format, Args), or to io_lib:unscan_format/1 in order to get the corresponding pair of Format and Args (with every * and corresponding argument expanded to numeric values).

A typical use of this function is to replace unbounded-size control sequences like ~w and ~p with the depth-limited variants ~W and ~P before formatting to text, e.g. in a logger.

unscan_format(FormatList) -> {Format, Data}

See io_lib:scan_format/2 for details.

build_text(FormatList) -> chars()

See io_lib:scan_format/2 for details.

indentation(String, StartIndent) -> integer()

  • String = string()
  • StartIndent = integer()

Returns the indentation if String has been printed, starting at StartIndent.

char_list(Term) -> boolean()

  • Term = term()

Returns true if Term is a flat list of characters in the Unicode range, otherwise it returns false.

latin1_char_list(Term) -> boolean()

  • Term = term()

Returns true if Term is a flat list of characters in the ISO-latin-1 range, otherwise it returns false.

deep_char_list(Term) -> boolean()

  • Term = term()

Returns true if Term is a, possibly deep, list of characters in the Unicode range, otherwise it returns false.

deep_latin1_char_list(Term) -> boolean()

  • Term = term()

Returns true if Term is a, possibly deep, list of characters in the ISO-latin-1 range, otherwise it returns false.

printable_list(Term) -> boolean()

  • Term = term()

Returns true if Term is a flat list of printable characters, otherwise it returns false.

What is a printable character in this case is determined by the +pc start up flag to the Erlang VM. See io:printable_range/0 and erl(1).

printable_latin1_list(Term) -> boolean()

  • Term = term()

Returns true if Term is a flat list of printable ISO-latin-1 characters, otherwise it returns false.

printable_unicode_list(Term) -> boolean()

  • Term = term()

Returns true if Term is a flat list of printable Unicode characters, otherwise it returns false.