Skip to content

String Rt

Spice
import "std/runtime/string_rt";

String struct

Heap-allocated builtin String type to enable dynamic modification in contrast to the primitive string type.

An empty String stays in an unallocated default state: contents is nil and capacity is 0. The heap buffer is only allocated once the first character is stored, keeping default-constructed and empty strings free of heap traffic.

Constructors

ctor

Spice
public p String.ctor(const string value = "")

Construct a String from a raw string value

Parameters

Name Type Description
value const string Initial raw string value (default: "")

ctor

Spice
public p String.ctor(const char value)

Construct a String holding a single character

Parameters

Name Type Description
value const char Initial character

ctor

Spice
public p String.ctor(const String& original)

Construct a String as a deep copy of another String

Parameters

Name Type Description
original const String& String to copy

ctor

Spice
public p String.ctor<IntLong>(IntLong initialSize)

Construct a String pre-allocating space for the given number of characters

Parameters

Name Type Description
initialSize IntLong Number of characters to pre-allocate space for

dtor

Spice
public p String.dtor()

Destruct the String, freeing its heap-allocated character buffer

Methods

append

Spice
public p String.append(const string appendix)

Appends the given string to the current one

Parameters

Name Type Description
appendix const string String to be appended

append

Spice
public p String.append(const String& appendix)

Appends the given String to the current one

Parameters

Name Type Description
appendix const String& String to be appended

append

Spice
public p String.append(const string appendix, unsigned long appendixLength)

Appends the first appendixLength bytes of the given string to the current one.

In contrast to the single-argument overload, the appendix is not required to be null-terminated and may contain embedded null bytes. This makes the overload the right choice for raw payloads that were read from a file, a socket or any other byte source.

Parameters

Name Type Description
appendix const string Buffer to be appended
appendixLength unsigned long Number of bytes to append from the buffer

append

Spice
public p String.append(const char c)

Appends the given char to the string and resize it if needed

Parameters

Name Type Description
c const char Char to append

insert

Spice
public p String.insert<IntLong>(unsigned IntLong position, string str)

Insert a raw string into the string at the given position

Parameters

Name Type Description
position unsigned IntLong Position to insert at
str string Raw string to insert

insert

Spice
public p String.insert<IntLong>(unsigned IntLong position, const String& str)

Insert a String into the string at the given position

Parameters

Name Type Description
position unsigned IntLong Position to insert at
str const String& String to insert

insert

Spice
public p String.insert<IntLong>(unsigned IntLong position, char c)

Insert a single character into the string at the given position

Parameters

Name Type Description
position unsigned IntLong Position to insert at
c char Character to insert

getRaw

Spice
public inline f<string> String.getRaw()

Get the raw and immutable string from this container instance

Returns: string — Raw immutable string

getLength

Spice
public inline f<unsigned long> String.getLength()

Retrieve the current length of the string

Returns: unsigned long — Current length of the string

getCapacity

Spice
public inline f<unsigned long> String.getCapacity()

Retrieve the current capacity of the string

Returns: unsigned long — Current capacity of the string

isEmpty

Spice
public inline f<bool> String.isEmpty()

Check if the string is empty

Returns: bool

isFull

Spice
public inline f<bool> String.isFull()

Checks if the string exhausts its capacity

Returns: bool — Full or not full

clear

Spice
public p String.clear()

Replaces the current contents of the string with an empty string

find

Spice
public f<long> String.find(string needle, unsigned long startIndex = 0l)

Searches for a substring in a string. Returns -1 if the string was not found.

Parameters

Name Type Description
needle string
startIndex unsigned long Index where to start the search (default: 0l)

Returns: long — Index, where the substring was found / -1

find

Spice
public f<long> String.find(string needle, unsigned int startIndex)

Searches for a substring in a string. Returns -1 if the string was not found.

Parameters

Name Type Description
needle string
startIndex unsigned int Index where to start the search

Returns: long — Index, where the substring was found / -1

find

Spice
public f<long> String.find(char needle, unsigned long startIndex = 0l)

Searches for a char in a string. Returns -1 if the char was not found.

Parameters

Name Type Description
needle char
startIndex unsigned long Index where to start the search (default: 0l)

Returns: long — Index, where the char was found / -1

rfind

Spice
public f<long> String.rfind(string needle, unsigned long startIndex = 0l)

Searches for a substring in a string from the back. Returns -1 if the string was not found.

Parameters

Name Type Description
needle string
startIndex unsigned long Index where to start the search (default: 0l)

Returns: long — Index, where the substring was found / -1

rfind

Spice
public f<long> String.rfind(string needle, unsigned int startIndex)

Searches for a substring in a string from the back. Returns -1 if the string was not found.

Parameters

Name Type Description
needle string
startIndex unsigned int Index where to start the search

Returns: long — Index, where the substring was found / -1

rfind

Spice
public f<long> String.rfind(char needle, unsigned long startIndex = 0l)

Searches for a char in a string from the back. Returns -1 if the char was not found.

Parameters

Name Type Description
needle char
startIndex unsigned long Index where to start the search (default: 0l)

Returns: long — Index, where the char was found / -1

contains

Spice
public inline f<bool> String.contains(string needle)

Checks if the string contains a substring

Parameters

Name Type Description
needle string Substring to search for

Returns: bool — Found or not

startsWith

Spice
public f<bool> String.startsWith(string prefix)

Checks if the string starts with a given prefix

Parameters

Name Type Description
prefix string Prefix to check for

Returns: bool — Starts with prefix or not

endsWith

Spice
public f<bool> String.endsWith(string suffix)

Checks if the string ends with a given suffix

Parameters

Name Type Description
suffix string

Returns: bool — Ends with suffix or not

reverse

Spice
public p String.reverse()

Reverse the string

replace

Spice
public f<bool> String.replace( string needle, string replacement, unsigned long startIdx = 0l )

Replace occurrence of substring with the replacement string

Parameters

Name Type Description
needle string Substring to replace
replacement string Replacement for the substring
startIdx unsigned long (default: 0l)

Returns: bool

replaceAll

Spice
public f<unsigned long> String.replaceAll(string needle, string replacement)

Replace all occurrences of substring with the replacement string

Parameters

Name Type Description
needle string Substring to replace
replacement string Replacement for the substring

Returns: unsigned long

replaceAll

Spice
public f<unsigned long> String.replaceAll(char needle, char replacement)

Replaces all occurrences of a character with another character

Parameters

Name Type Description
needle char Character to search for
replacement char Character to replace each occurrence with

Returns: unsigned long — Number of replacements made

getSubstring

Spice
public f<String> String.getSubstring<IntLongShort>(unsigned IntLongShort startIdx, long length = -1l)

Returns the substring of the current string, starting at position startIndex with the length of length.

Parameters

Name Type Description
startIdx unsigned IntLongShort
length long Length of substring (default: -1l)

Returns: String — Substring

trim

Spice
public f<String> String.trim()

Returns a new string without leading or trailing whitespaces.

Returns: String — Trimmed string

reserve

Spice
public p String.reserve<IntLongShort>(unsigned IntLongShort charCount)

Reserves charCount items

Parameters

Name Type Description
charCount unsigned IntLongShort Number of chars to reserve for the string

Functions

getRawLength

Spice
public f<unsigned long> getRawLength(string input)

Returns the length of a string

Parameters

Name Type Description
input string Input string

Returns: unsigned long — Length of the input string

isRawEqual

Spice
public f<bool> isRawEqual(string lhs, string rhs)

Checks the equality of two raw strings

Parameters

Name Type Description
lhs string First input raw string
rhs string Second input raw string

Returns: bool — Equality of lhs and rhs

Operators

operator=

Spice
public p operator=(String& this, const String& newValue)

Copy-assign the contents of another String into this one

Parameters

Name Type Description
newValue const String& String to copy from

operator+

Spice
public f<String> operator+<StrTy, StrTyChar>(const StrTy& a, const StrTyChar& b)

Concatenates two strings and returns the result

Parameters

Name Type Description
a const StrTy& String a
b const StrTyChar& String b

Returns: String — Concatenated string

operator+=

Spice
public p operator+=<StrTyChar>(String& a, const StrTyChar& b)

Concatenates b to the end of a

Parameters

Name Type Description
a String& String a
b const StrTyChar& String/string/char b

operator*

Spice
public f<String> operator*<IntLongShort>(const String& str, const IntLongShort n)

Concatenates the given string with itself n times.

Parameters

Name Type Description
str const String& Input string
n const IntLongShort Multiplication operand

Returns: String — Result string

operator*

Spice
public f<String> operator*<IntLongShort>(const IntLongShort n, const String& str)

Concatenates the given string with itself n times

Parameters

Name Type Description
n const IntLongShort Multiplication operand
str const String& Input string

Returns: String — Result string

operator*=

Spice
public p operator*=<IntLongShort>(String& str, const IntLongShort n)

Concatenates this string with itself n times

Parameters

Name Type Description
str String& Input string
n const IntLongShort Multiplication operand

operator==

Spice
public f<bool> operator==(const String& a, const String& b)

Checks if two strings have the same value

Parameters

Name Type Description
a const String& First input string
b const String& Second input string

Returns: bool — Equal or not

operator==

Spice
public f<bool> operator==(const String& a, string b)

Checks if a String and a raw string have the same value

Parameters

Name Type Description
a const String& First input string
b string Second input string

Returns: bool — Equal or not

operator==

Spice
public f<bool> operator==(string a, const String& b)

Checks if a raw string and a String have the same value

Parameters

Name Type Description
a string First input string
b const String& Second input string

Returns: bool — Equal or not

operator!=

Spice
public f<bool> operator!=(const String& a, const String& b)

Checks if two strings have not the same value

Parameters

Name Type Description
a const String& First input string
b const String& Second input string

Returns: bool — Not equal or not

operator!=

Spice
public f<bool> operator!=(const String& a, const string& b)

Checks if a String and a raw string do not have the same value

Parameters

Name Type Description
a const String& First input string
b const string& Second input string

Returns: bool — Not equal or not

operator!=

Spice
public f<bool> operator!=(const string& a, const String& b)

Checks if a raw string and a String do not have the same value

Parameters

Name Type Description
a const string& First input string
b const String& Second input string

Returns: bool — Not equal or not

operator[]

Spice
public f<char&> operator[](String& str, unsigned long idx)

Extract the char at the given index and return it

Parameters

Name Type Description
str String&
idx unsigned long

Returns: char& — Character at the given index

operator[]

Spice
public f<char&> operator[](String& str, unsigned int idx)

Extract the char at the given index and return it

Parameters

Name Type Description
str String& Input string
idx unsigned int

Returns: char& — Character at the given index