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.
Swift-style alias for downcase/1.
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
Functions
fn append_cl(a: List(Char), b: List(Char)) -> List(Char)
:lists.append/2 on two normalised charlists.
fn as_charlist(s: String) -> List(Char)
fn atom_to_cl(a: Atom) -> List(Char)
Render an Atom as its name; no : prefix is added.
fn characters(value: String) -> List(Char)
Extract the underlying Unicode code-point list of a String.
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.
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.
fn cl_to_float(s: List(Char)) -> Float
:erlang.list_to_float/1 on a normalised charlist.
fn cl_to_int(s: List(Char)) -> Int
:erlang.list_to_integer/1 on a normalised charlist.
fn concat(a: String, b: String) -> String
Concatenate two strings.
fn contains(s: String, wanted: Char) -> Bool
Return true when s contains the character wanted.
fn contains_char(s: List(Char), wanted: Char) -> Bool
fn copies_cl(s: List(Char), n: Int) -> List(Char)
:string.copies/2 on a normalised charlist.
fn downcase(s: String) -> String
Lower-case s using Unicode case folding.
fn drop_chars(s: List(Char), count: Int) -> List(Char)
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).
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).
fn first(s: String) -> Option(Char)
Return the first character of s, or None if s is empty.
fn float_to_cl(f: Float) -> List(Char)
Render a Float as its string form (:erlang.float_to_list/1).
fn from_atom(a: Atom) -> String
Render an Atom as its name String (without a leading :).
fn from_characters(value: List(Char)) -> String
Construct a String from a list of Unicode code points.
fn from_float(f: Float) -> String
Render a Float as its String representation.
fn from_int(n: Int) -> String
Render an Int as its decimal String representation.
fn has_prefix(s: String, prefix: String) -> Bool
Return true when s starts with prefix.
fn has_prefix_chars(s: List(Char), prefix: List(Char)) -> Bool
fn has_suffix(s: String, suffix: String) -> Bool
Return true when s ends with suffix.
fn int_to_cl(n: Int) -> List(Char)
Render an Int as its canonical decimal string.
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).
fn last(s: String) -> Option(Char)
Return the last character of s, or None if s is empty.
fn last_char(s: List(Char)) -> Option(Char)
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.
fn list_length(s: List(Char)) -> Int
:erlang.length/1 on a normalised charlist.
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.
fn lowercased(s: String) -> String
Swift-style alias for downcase/1.
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.
fn omit_empty(parts: List(String)) -> List(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).
fn repeat(s: String, n: Int) -> String
Return s concatenated with itself n times.
fn reverse(s: String) -> String
Reverse s by code point.
fn reverse_cl(s: List(Char)) -> List(Char)
:lists.reverse/1 on a normalised charlist.
fn split(s: String, sep: String) -> List(String)
Split s on the first occurrence of sep.
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.
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.
fn split_on(s: String, separator: Char) -> List(String)
Split on every occurrence of one character, following Swift's default of omitting empty subsequences.
fn string_lt_chars(a: List(Char), b: List(Char)) -> Bool
fn strings_from_charlists(values: List(List(Char))) -> List(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).
fn take_chars(s: List(Char), count: Int) -> List(Char)
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.
fn to_float(s: String) -> Float
Parse s as a float. Raises on bad input.
fn to_int(s: String) -> Int
Parse s as a base-10 integer. Raises on bad input.
fn trim(s: String) -> String
Strip leading and trailing whitespace.
fn trim_cl(s: List(Char), direction: Atom) -> List(Char)
Private helper: :string.trim/2 with an explicit direction atom.
Representation-preserving.
fn trim_dir(s: String, direction: Atom) -> String
fn trim_leading(s: String) -> String
Strip leading whitespace only.
fn trim_trailing(s: String) -> String
Strip trailing whitespace only.
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.
fn upcase(s: String) -> String
Upper-case s using Unicode case folding.
fn uppercase_cl(s: List(Char)) -> List(Char)
Upper-case s using Unicode case folding (:string.uppercase/1).
fn uppercased(s: String) -> String
Swift-style alias for upcase/1.
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.