Std.String (Cure Standard Library v0.37.2)

View Source

Value-surface string operations: length, concat, split, trim, case conversion, and integer/float/atom parsing.

String is a nominal text type. Its current storage is a Unicode code-point list, exposed only through characters and from_characters; the nominal boundary lets String and List(Char) carry distinct conformances and permits a future storage change without changing source-level identity.

BEAM boundaries receive and return code-point lists through small private externs. Raw binaries remain the responsibility of Std.Binary.

Examples

use Std.String

length("hello")                         # => 5   (code points, not bytes)
concat("foo", "bar")                    # => "foobar"
upcase("cafe")                          # => "CAFE"
split("a,b,c", ",")                     # => ["a", "b,c"]  (splits once)
repeat("ab", 3)                         # => "ababab"
use Std.String

from_int(42)                            # => "42"
to_int(trim("  -17 "))                  # => -17

Summary

Types

Nominal string storage. The constructor remains module-qualified outside this module; ordinary code should use literals and the conversion functions.

Functions

:lists.append/2 on two normalised charlists.

Render an Atom as its name; no : prefix is added.

Extract the underlying Unicode code-point list of a String.

:erlang.list_to_atom/1 on a normalised charlist. This is the explicit raw escape hatch for callers that really intend to intern a new atom.

:erlang.list_to_existing_atom/1 on a normalised charlist. This never grows the VM atom table, but raises when the atom is not already interned.

:erlang.list_to_float/1 on a normalised charlist.

:erlang.list_to_integer/1 on a normalised charlist.

Concatenate two strings.

Return true when s contains the character wanted.

:string.copies/2 on a normalised charlist.

Lower-case s using Unicode case folding.

Drop the leading count characters of s (returns s unchanged if count <= 0, or "" if count exceeds the length of s).

Drop the trailing count characters of s (returns s unchanged if count <= 0, or "" if count exceeds the length of s).

Return the first character of s, or None if s is empty.

Render a Float as its string form (:erlang.float_to_list/1).

Render an Atom as its name String (without a leading :).

Construct a String from a list of Unicode code points.

Render a Float as its String representation.

Render an Int as its decimal String representation.

Return true when s starts with prefix.

Return true when s ends with suffix.

Render an Int as its canonical decimal string.

true when s is the empty string. Written with the int_eq primitive directly (not ==): Std.String is inside the @prelude bootstrap closure, so it elaborates with no ambient Equatable and cannot use Std.Equatable (that would cycle, since Std.Equatable uses Std.String).

Return the last character of s, or None if s is empty.

Number of code points in s. Unlike a byte-length, this counts characters: a multi-byte code point is one.

:erlang.length/1 on a normalised charlist.

Lower-case s using Unicode case folding (:string.lowercase/1). Representation-preserving: a charlist maps to a charlist.

Lower-case a single character using Unicode case folding. One scalar may map to several, so the result is a String rather than a Char.

Return the leading count characters of s (or all of s if shorter; empty if count <= 0).

Return s concatenated with itself n times.

Reverse s by code point.

:lists.reverse/1 on a normalised charlist.

Split s on the first occurrence of sep.

Private helper: the :all mode of :string.split/3, used by split_on. A second wrapper over the same Erlang target as split_first_cl; its own @extern is required and is not a duplicate of the one above.

Private helper: :string.split/3 splitting on the first occurrence. Representation-preserving. This and split_all_cl below are two distinct wrappers over the same Erlang :string.split/3; each needs its own @extern annotation (an @extern binds to the single declaration that follows it), so the repeated target is intentional, not a duplicate declaration.

Split on every occurrence of one character, following Swift's default of omitting empty subsequences.

Return the trailing count characters of s (or all of s if shorter; empty if count <= 0).

Convert s to an atom that already exists in the VM atom table. Raises when no such atom exists; importantly, arbitrary input cannot grow the permanent atom table.

Parse s as a float. Raises on bad input.

Parse s as a base-10 integer. Raises on bad input.

Strip leading and trailing whitespace.

Private helper: :string.trim/2 with an explicit direction atom. Representation-preserving.

Strip leading whitespace only.

Strip trailing whitespace only.

UNSAFE: intern s as a permanent VM atom. Never pass attacker-controlled or otherwise unbounded input; atoms are not garbage-collected.

Upper-case s using Unicode case folding.

Upper-case s using Unicode case folding (:string.uppercase/1).

Swift-style alias for upcase/1.

Upper-case a single character using Unicode case folding. One scalar may map to several (for example, 'ß' yields "SS"), so the result is a String rather than a Char.

Types

struct String

 struct String
   ?

Nominal string storage. The constructor remains module-qualified outside this module; ordinary code should use literals and the conversion functions.

Functions

append_cl(a: List(Char), b: List(Char)) -> List(Char)

 fn append_cl(a: List(Char), b: List(Char)) -> List(Char)

:lists.append/2 on two normalised charlists.

as_charlist(s: String) -> List(Char)

 fn as_charlist(s: String) -> List(Char)

atom_to_cl(a: Atom) -> List(Char)

 fn atom_to_cl(a: Atom) -> List(Char)

Render an Atom as its name; no : prefix is added.

characters(value: String) -> List(Char)

 fn characters(value: String) -> List(Char)

Extract the underlying Unicode code-point list of a String.

cl_to_atom(s: List(Char)) -> Atom

 fn cl_to_atom(s: List(Char)) -> Atom

:erlang.list_to_atom/1 on a normalised charlist. This is the explicit raw escape hatch for callers that really intend to intern a new atom.

cl_to_existing_atom(s: List(Char)) -> Atom

 fn cl_to_existing_atom(s: List(Char)) -> Atom

:erlang.list_to_existing_atom/1 on a normalised charlist. This never grows the VM atom table, but raises when the atom is not already interned.

cl_to_float(s: List(Char)) -> Float

 fn cl_to_float(s: List(Char)) -> Float

:erlang.list_to_float/1 on a normalised charlist.

cl_to_int(s: List(Char)) -> Int

 fn cl_to_int(s: List(Char)) -> Int

:erlang.list_to_integer/1 on a normalised charlist.

concat(a: String, b: String) -> String

 fn concat(a: String, b: String) -> String

Concatenate two strings.

contains(s: String, wanted: Char) -> Bool

 fn contains(s: String, wanted: Char) -> Bool

Return true when s contains the character wanted.

contains_char(s: List(Char), wanted: Char) -> Bool

 fn contains_char(s: List(Char), wanted: Char) -> Bool

copies_cl(s: List(Char), n: Int) -> List(Char)

 fn copies_cl(s: List(Char), n: Int) -> List(Char)

:string.copies/2 on a normalised charlist.

downcase(s: String) -> String

 fn downcase(s: String) -> String

Lower-case s using Unicode case folding.

drop_chars(s: List(Char), count: Int) -> List(Char)

 fn drop_chars(s: List(Char), count: Int) -> List(Char)

drop_first(s: String, count: Int) -> String

 fn drop_first(s: String, count: Int) -> String

Drop the leading count characters of s (returns s unchanged if count <= 0, or "" if count exceeds the length of s).

drop_last(s: String, count: Int) -> String

 fn drop_last(s: String, count: Int) -> String

Drop the trailing count characters of s (returns s unchanged if count <= 0, or "" if count exceeds the length of s).

first(s: String) -> Option(Char)

 fn first(s: String) -> Option(Char)

Return the first character of s, or None if s is empty.

float_to_cl(f: Float) -> List(Char)

 fn float_to_cl(f: Float) -> List(Char)

Render a Float as its string form (:erlang.float_to_list/1).

from_atom(a: Atom) -> String

 fn from_atom(a: Atom) -> String

Render an Atom as its name String (without a leading :).

from_characters(value: List(Char)) -> String

 fn from_characters(value: List(Char)) -> String

Construct a String from a list of Unicode code points.

from_float(f: Float) -> String

 fn from_float(f: Float) -> String

Render a Float as its String representation.

from_int(n: Int) -> String

 fn from_int(n: Int) -> String

Render an Int as its decimal String representation.

has_prefix(s: String, prefix: String) -> Bool

 fn has_prefix(s: String, prefix: String) -> Bool

Return true when s starts with prefix.

has_prefix_chars(s: List(Char), prefix: List(Char)) -> Bool

 fn has_prefix_chars(s: List(Char), prefix: List(Char)) -> Bool

has_suffix(s: String, suffix: String) -> Bool

 fn has_suffix(s: String, suffix: String) -> Bool

Return true when s ends with suffix.

int_to_cl(n: Int) -> List(Char)

 fn int_to_cl(n: Int) -> List(Char)

Render an Int as its canonical decimal string.

is_empty(s: String) -> Bool

 fn is_empty(s: String) -> Bool

true when s is the empty string. Written with the int_eq primitive directly (not ==): Std.String is inside the @prelude bootstrap closure, so it elaborates with no ambient Equatable and cannot use Std.Equatable (that would cycle, since Std.Equatable uses Std.String).

last(s: String) -> Option(Char)

 fn last(s: String) -> Option(Char)

Return the last character of s, or None if s is empty.

last_char(s: List(Char)) -> Option(Char)

 fn last_char(s: List(Char)) -> Option(Char)

length(s: String) -> Int

 fn length(s: String) -> Int

Number of code points in s. Unlike a byte-length, this counts characters: a multi-byte code point is one.

list_length(s: List(Char)) -> Int

 fn list_length(s: List(Char)) -> Int

:erlang.length/1 on a normalised charlist.

lowercase_cl(s: List(Char)) -> List(Char)

 fn lowercase_cl(s: List(Char)) -> List(Char)

Lower-case s using Unicode case folding (:string.lowercase/1). Representation-preserving: a charlist maps to a charlist.

lowercased(s: String) -> String

 fn lowercased(s: String) -> String

Swift-style alias for downcase/1.

lowercased_character(c: Char) -> String

 fn lowercased_character(c: Char) -> String

Lower-case a single character using Unicode case folding. One scalar may map to several, so the result is a String rather than a Char.

omit_empty(parts: List(String)) -> List(String)

 fn omit_empty(parts: List(String)) -> List(String)

prefix(s: String, count: Int) -> String

 fn prefix(s: String, count: Int) -> String

Return the leading count characters of s (or all of s if shorter; empty if count <= 0).

repeat(s: String, n: Int) -> String

 fn repeat(s: String, n: Int) -> String

Return s concatenated with itself n times.

reverse(s: String) -> String

 fn reverse(s: String) -> String

Reverse s by code point.

reverse_cl(s: List(Char)) -> List(Char)

 fn reverse_cl(s: List(Char)) -> List(Char)

:lists.reverse/1 on a normalised charlist.

split(s: String, sep: String) -> List(String)

 fn split(s: String, sep: String) -> List(String)

Split s on the first occurrence of sep.

split_all_cl(s: List(Char), sep: List(Char), where: Atom) -> List(List(Char))

 fn split_all_cl(s: List(Char), sep: List(Char), where: Atom) -> List(List(Char))

Private helper: the :all mode of :string.split/3, used by split_on. A second wrapper over the same Erlang target as split_first_cl; its own @extern is required and is not a duplicate of the one above.

split_first_cl(s: List(Char), sep: List(Char), where: Atom) -> List(List(Char))

 fn split_first_cl(s: List(Char), sep: List(Char), where: Atom) -> List(List(Char))

Private helper: :string.split/3 splitting on the first occurrence. Representation-preserving. This and split_all_cl below are two distinct wrappers over the same Erlang :string.split/3; each needs its own @extern annotation (an @extern binds to the single declaration that follows it), so the repeated target is intentional, not a duplicate declaration.

split_on(s: String, separator: Char) -> List(String)

 fn split_on(s: String, separator: Char) -> List(String)

Split on every occurrence of one character, following Swift's default of omitting empty subsequences.

string_lt_chars(a: List(Char), b: List(Char)) -> Bool

 fn string_lt_chars(a: List(Char), b: List(Char)) -> Bool

strings_from_charlists(values: List(List(Char))) -> List(String)

 fn strings_from_charlists(values: List(List(Char))) -> List(String)

suffix(s: String, count: Int) -> String

 fn suffix(s: String, count: Int) -> String

Return the trailing count characters of s (or all of s if shorter; empty if count <= 0).

take_chars(s: List(Char), count: Int) -> List(Char)

 fn take_chars(s: List(Char), count: Int) -> List(Char)

to_existing_atom(s: String) -> Atom

 fn to_existing_atom(s: String) -> Atom

Convert s to an atom that already exists in the VM atom table. Raises when no such atom exists; importantly, arbitrary input cannot grow the permanent atom table.

to_float(s: String) -> Float

 fn to_float(s: String) -> Float

Parse s as a float. Raises on bad input.

to_int(s: String) -> Int

 fn to_int(s: String) -> Int

Parse s as a base-10 integer. Raises on bad input.

trim(s: String) -> String

 fn trim(s: String) -> String

Strip leading and trailing whitespace.

trim_cl(s: List(Char), direction: Atom) -> List(Char)

 fn trim_cl(s: List(Char), direction: Atom) -> List(Char)

Private helper: :string.trim/2 with an explicit direction atom. Representation-preserving.

trim_dir(s: String, direction: Atom) -> String

 fn trim_dir(s: String, direction: Atom) -> String

trim_leading(s: String) -> String

 fn trim_leading(s: String) -> String

Strip leading whitespace only.

trim_trailing(s: String) -> String

 fn trim_trailing(s: String) -> String

Strip trailing whitespace only.

unsafe_to_atom(s: String) -> Atom

 fn unsafe_to_atom(s: String) -> Atom

UNSAFE: intern s as a permanent VM atom. Never pass attacker-controlled or otherwise unbounded input; atoms are not garbage-collected.

upcase(s: String) -> String

 fn upcase(s: String) -> String

Upper-case s using Unicode case folding.

uppercase_cl(s: List(Char)) -> List(Char)

 fn uppercase_cl(s: List(Char)) -> List(Char)

Upper-case s using Unicode case folding (:string.uppercase/1).

uppercased(s: String) -> String

 fn uppercased(s: String) -> String

Swift-style alias for upcase/1.

uppercased_character(c: Char) -> String

 fn uppercased_character(c: Char) -> String

Upper-case a single character using Unicode case folding. One scalar may map to several (for example, 'ß' yields "SS"), so the result is a String rather than a Char.