String¶
A String is an ordered collection of characters.
Strings don't specify an encoding.
Example usage of some common String methods:
actor Main
new create(env: Env) =>
try
// construct a new string
let str = "Hello"
// make an uppercased version
let str_upper = str.upper()
// make a reversed version
let str_reversed = str.reverse()
// add " world" to the end of our original string
let str_new = str.add(" world")
// count occurrences of letter "l"
let count = str_new.count("l")
// find first occurrence of letter "w"
let first_w = str_new.find("w")
// find first occurrence of letter "d"
let first_d = str_new.find("d")
// get substring capturing "world"
let substr = str_new.substring(first_w, first_d+1)
// clone substring
let substr_clone = substr.clone()
// print our substr
env.out.print(consume substr)
end
class val String is
Seq[U8 val] ref,
Comparable[String box] ref,
Stringable box
Implements¶
- Seq[U8 val] ref
- Comparable[String box] ref
- Stringable box
Constructors¶
create¶
An empty string. Enough space for len bytes is reserved.
new ref create(
len: USize val = 0)
: String ref^
Parameters¶
- len: USize val = 0
Returns¶
- String ref^
from_array¶
Create a string from an array, reusing the underlying data pointer.
new val from_array(
data: Array[U8 val] val)
: String val^
Parameters¶
Returns¶
- String val^
from_iso_array¶
Create a string from an array, reusing the underlying data pointer
new iso from_iso_array(
data: Array[U8 val] iso)
: String iso^
Parameters¶
Returns¶
- String iso^
from_cpointer¶
Return a string from binary pointer data without making a copy. This must be done only with C-FFI functions that return pony_alloc'd character arrays. If a null pointer is given then an empty string is returned.
new ref from_cpointer(
str: Pointer[U8 val] ref,
len: USize val,
alloc: USize val = 0)
: String ref^
Parameters¶
Returns¶
- String ref^
from_cstring¶
Return a string from a pointer to a null-terminated cstring without making a copy. The data is not copied. This must be done only with C-FFI functions that return pony_alloc'd character arrays. The pointer is scanned for the first null byte, which will be interpreted as the null terminator. Note that the scan is unbounded; the pointed to data must be null-terminated within the allocated array to preserve memory safety. If a null pointer is given then an empty string is returned.
new ref from_cstring(
str: Pointer[U8 val] ref)
: String ref^
Parameters¶
Returns¶
- String ref^
copy_cpointer¶
Create a string by copying a fixed number of bytes from a pointer.
new ref copy_cpointer(
str: Pointer[U8 val] box,
len: USize val)
: String ref^
Parameters¶
Returns¶
- String ref^
copy_cstring¶
Create a string by copying a null-terminated C string. Note that the scan is unbounded; the pointed to data must be null-terminated within the allocated array to preserve memory safety. If a null pointer is given then an empty string is returned.
new ref copy_cstring(
str: Pointer[U8 val] box)
: String ref^
Parameters¶
Returns¶
- String ref^
from_utf32¶
Create a UTF-8 string from a single UTF-32 code point.
new ref from_utf32(
value: U32 val)
: String ref^
Parameters¶
- value: U32 val
Returns¶
- String ref^
Public Functions¶
push_utf32¶
Push a UTF-32 code point.
fun ref push_utf32(
value: U32 val)
: None val
Parameters¶
- value: U32 val
Returns¶
- None val
cpointer¶
Returns a C compatible pointer to the underlying string allocation.
fun box cpointer(
offset: USize val = 0)
: Pointer[U8 val] tag
Parameters¶
- offset: USize val = 0
Returns¶
cstring¶
Returns a C compatible pointer to a null-terminated version of the string, safe to pass to an FFI function that doesn't accept a size argument, expecting a null-terminator. If the underlying string is already null terminated, this is returned; otherwise the string is copied into a new, null-terminated allocation.
fun box cstring()
: Pointer[U8 val] tag
Returns¶
array¶
Returns an Array[U8] that reuses the underlying data pointer.
fun val array()
: Array[U8 val] val
Returns¶
iso_array¶
Returns an Array[U8] iso that reuses the underlying data pointer.
fun iso iso_array()
: Array[U8 val] iso^
Returns¶
size¶
Returns the length of the string data in bytes.
fun box size()
: USize val
Returns¶
- USize val
codepoints¶
Returns the number of unicode code points in the string between the two
offsets. Index range [from
.. to
) is half-open.
fun box codepoints(
from: ISize val = 0,
to: ISize val = call)
: USize val
Parameters¶
Returns¶
- USize val
space¶
Returns the space available for data, not including the null terminator.
fun box space()
: USize val
Returns¶
- USize val
reserve¶
Reserve space for len bytes. An additional byte will be reserved for the null terminator.
fun ref reserve(
len: USize val)
: None val
Parameters¶
- len: USize val
Returns¶
- None val
compact¶
Try to remove unused space, making it available for garbage collection. The request may be ignored. The string is returned to allow call chaining.
fun ref compact()
: None val
Returns¶
- None val
recalc¶
Recalculates the string length. This is only needed if the string is changed via an FFI call. If a null terminator byte is not found within the allocated length, the size will not be changed.
fun ref recalc()
: None val
Returns¶
- None val
truncate¶
Truncates the string at the minimum of len and space. Ensures there is a null terminator. Does not check for null terminators inside the string.
Note that memory is not freed by this operation.
fun ref truncate(
len: USize val)
: None val
Parameters¶
- len: USize val
Returns¶
- None val
trim_in_place¶
Trim the string to a portion of itself, covering from
until to
.
Unlike slice, the operation does not allocate a new string nor copy
elements.
fun ref trim_in_place(
from: USize val = 0,
to: USize val = call)
: None val
Parameters¶
Returns¶
- None val
trim¶
Return a shared portion of this string, covering from
until to
.
Both the original and the new string are immutable, as they share memory.
The operation does not allocate a new string pointer nor copy elements.
fun val trim(
from: USize val = 0,
to: USize val = call)
: String val
Parameters¶
Returns¶
- String val
chop¶
Chops the string in half at the split point requested and returns both the left and right portions. The original string is trimmed in place and returned as the left portion. If the split point is larger than the string, the left portion is the original string and the right portion is a new empty string. Both strings are isolated and mutable, as they do not share memory. The operation does not allocate a new string pointer nor copy elements.
fun iso chop(
split_point: USize val)
: (String iso^ , String iso^)
Parameters¶
- split_point: USize val
Returns¶
unchop¶
Unchops two iso strings to return the original string they were chopped from. Both input strings are isolated and mutable and were originally chopped from a single string. This function checks that they are indeed two strings chopped from the same original string and can be unchopped before doing the unchopping and returning the unchopped string. If the two strings cannot be unchopped it returns both strings without modifying them. The operation does not allocate a new string pointer nor copy elements.
fun iso unchop(
b: String iso)
: ((String iso^ , String iso^) | String iso^)
Parameters¶
- b: String iso
Returns¶
is_null_terminated¶
Return true if the string is null-terminated and safe to pass to an FFI function that doesn't accept a size argument, expecting a null-terminator. This method checks that there is a null byte just after the final position of populated bytes in the string, but does not check for other null bytes which may be present earlier in the content of the string. If you need a null-terminated copy of this string, use the clone method.
fun box is_null_terminated()
: Bool val
Returns¶
- Bool val
utf32¶
Return a UTF32 representation of the character at the given offset and the number of bytes needed to encode that character. If the offset does not point to the beginning of a valid UTF8 encoding, return 0xFFFD (the unicode replacement character) and a length of one. Raise an error if the offset is out of bounds.
fun box utf32(
offset: ISize val)
: (U32 val , U8 val) ?
Parameters¶
- offset: ISize val
Returns¶
apply¶
Returns the i-th byte. Raise an error if the index is out of bounds.
fun box apply(
i: USize val)
: U8 val ?
Parameters¶
- i: USize val
Returns¶
- U8 val ?
update¶
Change the i-th byte. Raise an error if the index is out of bounds.
fun ref update(
i: USize val,
value: U8 val)
: U8 val ?
Parameters¶
Returns¶
- U8 val ?
at_offset¶
Returns the byte at the given offset. Raise an error if the offset is out of bounds.
fun box at_offset(
offset: ISize val)
: U8 val ?
Parameters¶
- offset: ISize val
Returns¶
- U8 val ?
update_offset¶
Changes a byte in the string, returning the previous byte at that offset. Raise an error if the offset is out of bounds.
fun ref update_offset(
offset: ISize val,
value: U8 val)
: U8 val ?
Parameters¶
Returns¶
- U8 val ?
clone¶
Returns a copy of the string. The resulting string is null-terminated even if the original is not.
fun box clone()
: String iso^
Returns¶
- String iso^
repeat_str¶
Returns a copy of the string repeated num
times with an optional
separator added inbetween repeats.
fun box repeat_str(
num: USize val = 1,
sep: String val = "")
: String iso^
Parameters¶
Returns¶
- String iso^
mul¶
Returns a copy of the string repeated num
times.
fun box mul(
num: USize val)
: String iso^
Parameters¶
- num: USize val
Returns¶
- String iso^
find¶
Return the index of the n-th instance of s in the string starting from the beginning. Raise an error if there is no n-th occurrence of s or s is empty.
fun box find(
s: String box,
offset: ISize val = 0,
nth: USize val = 0)
: ISize val ?
Parameters¶
Returns¶
- ISize val ?
rfind¶
Return the index of n-th instance of s
in the string starting from the
end. The offset
represents the highest index to included in the search.
Raise an error if there is no n-th occurrence of s
or s
is empty.
fun box rfind(
s: String box,
offset: ISize val = call,
nth: USize val = 0)
: ISize val ?
Parameters¶
Returns¶
- ISize val ?
contains¶
Returns true if contains s as a substring, false otherwise.
fun box contains(
s: String box,
offset: ISize val = 0,
nth: USize val = 0)
: Bool val
Parameters¶
Returns¶
- Bool val
count¶
Counts the non-overlapping occurrences of s in the string.
fun box count(
s: String box,
offset: ISize val = 0)
: USize val
Parameters¶
Returns¶
- USize val
at¶
Returns true if the substring s is present at the given offset.
fun box at(
s: String box,
offset: ISize val = 0)
: Bool val
Parameters¶
Returns¶
- Bool val
delete¶
Delete len bytes at the supplied offset, compacting the string in place.
fun ref delete(
offset: ISize val,
len: USize val = 1)
: None val
Parameters¶
Returns¶
- None val
substring¶
Returns a substring. Index range [from
.. to
) is half-open.
Returns an empty string if nothing is in the range.
Note that this operation allocates a new string to be returned. For
similar operations that don't allocate a new string, see trim
and
trim_in_place
.
fun box substring(
from: ISize val,
to: ISize val = call)
: String iso^
Parameters¶
Returns¶
- String iso^
lower¶
Returns a lower case version of the string.
fun box lower()
: String iso^
Returns¶
- String iso^
lower_in_place¶
Transforms the string to lower case. Currently only knows ASCII case.
fun ref lower_in_place()
: None val
Returns¶
- None val
upper¶
Returns an upper case version of the string. Currently only knows ASCII case.
fun box upper()
: String iso^
Returns¶
- String iso^
upper_in_place¶
Transforms the string to upper case.
fun ref upper_in_place()
: None val
Returns¶
- None val
reverse¶
Returns a reversed version of the string.
fun box reverse()
: String iso^
Returns¶
- String iso^
reverse_in_place¶
Reverses the byte order in the string. This needs to be changed to handle UTF-8 correctly.
fun ref reverse_in_place()
: None val
Returns¶
- None val
push¶
Add a byte to the end of the string.
fun ref push(
value: U8 val)
: None val
Parameters¶
- value: U8 val
Returns¶
- None val
pop¶
Remove a byte from the end of the string.
fun ref pop()
: U8 val ?
Returns¶
- U8 val ?
unshift¶
Adds a byte to the beginning of the string.
fun ref unshift(
value: U8 val)
: None val
Parameters¶
- value: U8 val
Returns¶
- None val
shift¶
Removes a byte from the beginning of the string.
fun ref shift()
: U8 val ?
Returns¶
- U8 val ?
append¶
Append the elements from a sequence, starting from the given offset.
fun ref append(
seq: ReadSeq[U8 val] box,
offset: USize val = 0,
len: USize val = call)
: None val
Parameters¶
Returns¶
- None val
concat¶
Add len iterated bytes to the end of the string, starting from the given offset.
fun ref concat(
iter: Iterator[U8 val] ref,
offset: USize val = 0,
len: USize val = call)
: None val
Parameters¶
Returns¶
- None val
clear¶
Truncate the string to zero length.
fun ref clear()
: None val
Returns¶
- None val
insert¶
Returns a version of the string with the given string inserted at the given offset.
fun box insert(
offset: ISize val,
that: String val)
: String iso^
Parameters¶
Returns¶
- String iso^
insert_in_place¶
Inserts the given string at the given offset. Appends the string if the offset is out of bounds.
fun ref insert_in_place(
offset: ISize val,
that: String box)
: None val
Parameters¶
Returns¶
- None val
insert_byte¶
Inserts a byte at the given offset. Appends if the offset is out of bounds.
fun ref insert_byte(
offset: ISize val,
value: U8 val)
: None val
Parameters¶
Returns¶
- None val
cut¶
Returns a version of the string with the given range deleted.
Index range [from
.. to
) is half-open.
fun box cut(
from: ISize val,
to: ISize val = call)
: String iso^
Parameters¶
Returns¶
- String iso^
cut_in_place¶
Cuts the given range out of the string.
Index range [from
.. to
) is half-open.
fun ref cut_in_place(
from: ISize val,
to: ISize val = call)
: None val
Parameters¶
Returns¶
- None val
remove¶
Remove all instances of s from the string. Returns the count of removed instances.
fun ref remove(
s: String box)
: USize val
Parameters¶
- s: String box
Returns¶
- USize val
replace¶
Replace up to n occurrences of from
in this
with to
. If n is 0, all
occurrences will be replaced. Returns the count of replaced occurrences.
fun ref replace(
from: String box,
to: String box,
n: USize val = 0)
: USize val
Parameters¶
Returns¶
- USize val
split_by¶
Split the string into an array of strings that are delimited by delim
in
the original string. If n > 0
, then the split count is limited to n.
Example:
let original: String = "<b><span>Hello!</span></b>"
let delimiter: String = "><"
let split_array: Array[String] = original.split_by(delimiter)
env.out.print("OUTPUT:")
for value in split_array.values() do
env.out.print(value)
end
// OUTPUT:
// <b
// span>Hello!</span
// b>
Adjacent delimiters result in a zero length entry in the array. For
example, "1CutCut2".split_by("Cut") => ["1", "", "2"]
.
An empty delimiter results in an array that contains a single element equal to the whole string.
If you want to split the string with each individual character of delim
,
use split
.
fun box split_by(
delim: String val,
n: USize val = call)
: Array[String val] iso^
Parameters¶
Returns¶
split¶
Split the string into an array of strings with any character in the
delimiter string. By default, the string is split with whitespace
characters. If n > 0
, then the split count is limited to n.
Example:
let original: String = "name,job;department"
let delimiter: String = ".,;"
let split_array: Array[String] = original.split(delimiter)
env.out.print("OUTPUT:")
for value in split_array.values() do
env.out.print(value)
end
// OUTPUT:
// name
// job
// department
Adjacent delimiters result in a zero length entry in the array. For
example, "1,,2".split(",") => ["1", "", "2"]
.
If you want to split the string with the entire delimiter string delim
,
use split_by
.
fun box split(
delim: String val = "
",
n: USize val = 0)
: Array[String val] iso^
Parameters¶
Returns¶
strip¶
Remove all leading and trailing characters from the string that are in s.
fun ref strip(
s: String box = "
")
: None val
Parameters¶
- s: String box = " "
Returns¶
- None val
rstrip¶
Remove all trailing characters within the string that are in s. By default, trailing whitespace is removed.
fun ref rstrip(
s: String box = "
")
: None val
Parameters¶
- s: String box = " "
Returns¶
- None val
lstrip¶
Remove all leading characters within the string that are in s. By default, leading whitespace is removed.
fun ref lstrip(
s: String box = "
")
: None val
Parameters¶
- s: String box = " "
Returns¶
- None val
add¶
Return a string that is a concatenation of this and that.
fun box add(
that: String box)
: String val
Parameters¶
- that: String box
Returns¶
- String val
join¶
Return a string that is a concatenation of the strings in data, using this as a separator.
fun box join(
data: Iterator[Stringable box] ref)
: String iso^
Parameters¶
- data: Iterator[Stringable box] ref
Returns¶
- String iso^
compare¶
Lexically compare two strings.
fun box compare(
that: String box)
: (Less val | Equal val | Greater val)
Parameters¶
- that: String box
Returns¶
compare_sub¶
Lexically compare at most n
bytes of the substring of this
starting at
offset
with the substring of that
starting at that_offset
. The
comparison is case sensitive unless ignore_case
is true
.
If the substring of this
is a proper prefix of the substring of that
,
then this
is Less
than that
. Likewise, if that
is a proper prefix of
this
, then this
is Greater
than that
.
Both offset
and that_offset
can be negative, in which case the offsets
are computed from the end of the string.
If n + offset
is greater than the length of this
, or n + that_offset
is greater than the length of that
, then the number of positions compared
will be reduced to the length of the longest substring.
Needs to be made UTF-8 safe.
fun box compare_sub(
that: String box,
n: USize val,
offset: ISize val = 0,
that_offset: ISize val = 0,
ignore_case: Bool val = false)
: (Less val | Equal val | Greater val)
Parameters¶
- that: String box
- n: USize val
- offset: ISize val = 0
- that_offset: ISize val = 0
- ignore_case: Bool val = false
Returns¶
eq¶
Returns true if the two strings have the same contents.
fun box eq(
that: String box)
: Bool val
Parameters¶
- that: String box
Returns¶
- Bool val
lt¶
Returns true if this is lexically less than that. Needs to be made UTF-8 safe.
fun box lt(
that: String box)
: Bool val
Parameters¶
- that: String box
Returns¶
- Bool val
le¶
Returns true if this is lexically less than or equal to that. Needs to be made UTF-8 safe.
fun box le(
that: String box)
: Bool val
Parameters¶
- that: String box
Returns¶
- Bool val
offset_to_index¶
fun box offset_to_index(
i: ISize val)
: USize val
Parameters¶
- i: ISize val
Returns¶
- USize val
bool¶
fun box bool()
: Bool val ?
Returns¶
- Bool val ?
i8¶
fun box i8(
base: U8 val = 0)
: I8 val ?
Parameters¶
- base: U8 val = 0
Returns¶
- I8 val ?
i16¶
fun box i16(
base: U8 val = 0)
: I16 val ?
Parameters¶
- base: U8 val = 0
Returns¶
- I16 val ?
i32¶
fun box i32(
base: U8 val = 0)
: I32 val ?
Parameters¶
- base: U8 val = 0
Returns¶
- I32 val ?
i64¶
fun box i64(
base: U8 val = 0)
: I64 val ?
Parameters¶
- base: U8 val = 0
Returns¶
- I64 val ?
i128¶
fun box i128(
base: U8 val = 0)
: I128 val ?
Parameters¶
- base: U8 val = 0
Returns¶
- I128 val ?
ilong¶
fun box ilong(
base: U8 val = 0)
: ILong val ?
Parameters¶
- base: U8 val = 0
Returns¶
- ILong val ?
isize¶
fun box isize(
base: U8 val = 0)
: ISize val ?
Parameters¶
- base: U8 val = 0
Returns¶
- ISize val ?
u8¶
fun box u8(
base: U8 val = 0)
: U8 val ?
Parameters¶
- base: U8 val = 0
Returns¶
- U8 val ?
u16¶
fun box u16(
base: U8 val = 0)
: U16 val ?
Parameters¶
- base: U8 val = 0
Returns¶
- U16 val ?
u32¶
fun box u32(
base: U8 val = 0)
: U32 val ?
Parameters¶
- base: U8 val = 0
Returns¶
- U32 val ?
u64¶
fun box u64(
base: U8 val = 0)
: U64 val ?
Parameters¶
- base: U8 val = 0
Returns¶
- U64 val ?
u128¶
fun box u128(
base: U8 val = 0)
: U128 val ?
Parameters¶
- base: U8 val = 0
Returns¶
- U128 val ?
ulong¶
fun box ulong(
base: U8 val = 0)
: ULong val ?
Parameters¶
- base: U8 val = 0
Returns¶
- ULong val ?
usize¶
fun box usize(
base: U8 val = 0)
: USize val ?
Parameters¶
- base: U8 val = 0
Returns¶
- USize val ?
read_int[A: ((I8 val | I16 val | I32 val | I64 val | I128 val | ILong val | ISize val | U8 val | U16 val | U32 val | U64 val | U128 val | ULong val | USize val) & Integer[A] val)]¶
Read an integer from the specified location in this string. The integer value read and the number of characters consumed are reported. The base parameter specifies the base to use, 0 indicates using the prefix, if any, to detect base 2, 10 or 16. If no integer is found at the specified location, then (0, 0) is returned, since no characters have been used. An integer out of range for the target type throws an error. A leading minus is allowed for signed integer types. Underscore characters are allowed throughout the integer and are ignored.
fun box read_int[A: ((I8 val | I16 val | I32 val |
I64 val | I128 val | ILong val |
ISize val | U8 val | U16 val |
U32 val | U64 val | U128 val |
ULong val | USize val) & Integer[A] val)](
offset: ISize val = 0,
base: U8 val = 0)
: (A , USize val) ?
Parameters¶
Returns¶
- (A , USize val) ?
f32¶
Convert this string starting at the given offset to a 32-bit floating point number (F32).
This method errors if this string cannot be parsed to a float, if the result would over- or underflow, the offset exceeds the size of this string or there are leftover characters in the string after conversion.
Examples:
"1.5".f32()? == F32(1.5)
"1.19208e-07".f32()? == F32(1.19208e-07)
"NaN".f32()?.nan() == true
fun box f32(
offset: ISize val = 0)
: F32 val ?
Parameters¶
- offset: ISize val = 0
Returns¶
- F32 val ?
f64¶
Convert this string starting at the given offset to a 64-bit floating point number (F64).
This method errors if this string cannot be parsed to a float, if the result would over- or underflow, the offset exceeds the size of this string or there are leftover characters in the string after conversion.
Examples:
"1.5".f64()? == F64(1.5)
"1.19208e-07".f64()? == F64(1.19208e-07)
"Inf".f64()?.infinite() == true
fun box f64(
offset: ISize val = 0)
: F64 val ?
Parameters¶
- offset: ISize val = 0
Returns¶
- F64 val ?
hash¶
fun box hash()
: USize val
Returns¶
- USize val
hash64¶
fun box hash64()
: U64 val
Returns¶
- U64 val
string¶
fun box string()
: String iso^
Returns¶
- String iso^
values¶
Return an iterator over the bytes in the string.
fun box values()
: StringBytes ref^
Returns¶
- StringBytes ref^
runes¶
Return an iterator over the codepoints in the string.
fun box runes()
: StringRunes ref^
Returns¶
- StringRunes ref^
ge¶
fun box ge(
that: String box)
: Bool val
Parameters¶
- that: String box
Returns¶
- Bool val
gt¶
fun box gt(
that: String box)
: Bool val
Parameters¶
- that: String box
Returns¶
- Bool val
ne¶
fun box ne(
that: String box)
: Bool val
Parameters¶
- that: String box
Returns¶
- Bool val