Builtin data types
In extension to the primitive data types, Spice offers builtin data types.
The String
data type¶
In opposite to the string primitive type, the String
builtin type is mutable and offers several ways to modify the contained value.
Tip
Use the string
primitive type over the String
builtin type as much as possible, due to its advantages in runtime performance. The usage of the String
builtin type is only recommended, when you need to modify the value of the string at runtime. string
variables are always immutable.
Constructors¶
The String
builtin type offers the following constructors:
void String()
: Initialize emptyvoid String(string)
: Initialize with a rawstring
as start valuevoid String(char)
: Initialize with a single charvoid String(const String&)
: Initialize by copying anotherString
(copy constructor)void String(int)
: Initialize with an initial sizevoid String(long)
: Initialize with an initial size
Methods¶
The String
builtin type offers the following methods:
void append(string)
: Appends a raw stringvoid append(const String&)
: Appends a stringvoid append(char)
: Appends a single charstring getRaw()
: Returns a char* to the heap allocated valueunsigned long getLength()
: Returns the length of the string in charsbool isEmpty()
: Checks if the string has a length of 0unsigned long getCapacity()
: Returns the allocated space in bytesbool isFull()
: Checks if the length is equal with the capacityvoid clear()
: Clear the value of the stringlong find(string, unsigned int)
: Returns the index, where a substring was found, starting from a start indexlong find(string, unsigned long)
: Returns the index, where a substring was found, starting from a start indexlong find(char, unsigned long)
: Returns the index, where a subchar was found, starting from a start indexlong rfind(string, unsigned int)
: Returns the index, where a substring was found, starting from reversed at a start indexlong rfind(string, unsigned long)
: Returns the index, where a substring was found, starting from reversed at a start indexlong rfind(char, unsigned long)
: Returns the index, where a subchar was found, starting from reversed at a start indexbool contains(string)
: Checks if the string contains a substringbool startsWith(string)
: Checks if the string starts with a substringbool endsWith(string)
: Checks if the string ends with a substringvoid reverse()
: Reverses the value of the stringvoid replace(string, string, unsigned long)
: Replaces a substring with another string, starting from a start indexvoid replaceAll(string, string)
: Replaces all occurrences of a substring with another stringvoid replaceAll(char, char)
: Replaces all occurrences of a subchar with another charString getSubstring(unsigned int, long)
: Returns the substring from start indexx
and lengthy
String getSubstring(unsigned long, long)
: Returns the substring from start indexx
and lengthy
String getSubstring(unsigned short, long)
: Returns the substring from start indexx
and lengthy
void reserve(unsigned int)
: Increase the capacity to the given numbervoid reserve(unsigned long)
: Increase the capacity to the given numbervoid reserve(unsigned short)
: Increase the capacity to the given number
Static functions¶
The String
builtin type offers the following static functions:
getRawLength(string)
: Returns the length of a raw stringisRawEqual(string, string)
: Checks if two raw strings are equal in value
Operators¶
The String
builtin type overrides the following operators:
String operator+(const String&, const String&)
: Concatenates two strings and returns the resultString operator+(const String&, const string&)
: Concatenates a string and a raw string and returns the resultString operator+(const String&, const char&)
: Concatenates a string and a raw string and returns the resultString operator+(const string&, const String&)
: Concatenates a raw string and a string and returns the resultString operator+(const string&, const string&)
: Concatenates two raw strings and returns the resultString operator+(const string&, const char&)
: Concatenates two raw strings and returns the resultvoid operator+=(String&, String)
: Appends a stringvoid operator+=(String&, string)
: Appends a raw stringvoid operator+=(String&, char)
: Appends a single charString operator*(const String&, int)
: Concatenates a string with itself n timesString operator*(const String&, long)
: Concatenates a string with itself n timesString operator*(const String&, short)
: Concatenates a string with itself n timesString operator*(int, const String&)
: Concatenates a string with itself n timesString operator*(long, const String&)
: Concatenates a string with itself n timesString operator*(short, const String&)
: Concatenates a string with itself n timesvoid operator*=(String&, int)
: Concatenates with itself n timesvoid operator*=(String&, long)
: Concatenates with itself n timesvoid operator*=(String&, short)
: Concatenates with itself n timesbool operator==(const String&, const String&)
: Checks if two strings are equal in valuebool operator==(const String&, string)
: Checks if two strings are equal in valuebool operator==(string, const String&)
: Checks if two strings are equal in valuebool operator!=(const String&, const String&)
: Checks if two strings are unequal in valuebool operator!=(const String&, string)
: Checks if two strings are unequal in valuebool operator!=(string, const String&)
: Checks if two strings are unequal in value
The Result
data type¶
The Result<T>
builtin type is a generic type, which is used to return a value or an error. It is used to handle errors
Constructors¶
The Result<T>
builtin type offers the following constructors:
void Result(const T&)
: Initialize Result object with a valuevoid Result(const Error&)
: Initialize Result object with an error
Methods¶
The Result<T>
builtin type offers the following methods:
T unwrap()
: Returns the value of the Result object. If the Result object contains an error, the program will panicError getErr()
: Returns the error of the Result object. If no error is present, an error object with error code 0 is returned.bool isOk()
: Checks if the Result object contains a valuebool isErr()
: Checks if the Result object contains an error
Static functions¶
The Result<T>
builtin type offers the following static functions:
Result<T> ok(const T&)
: Returns a Result object with a valueResult<T> err(const Error&)
: Returns a Result object with an errorResult<T> err(int, string)
: Returns a Result object with an error, constructed with an error code and an error messageResult<T> err(string)
: Returns a Result object with an error, constructed with an error message
The Error
data type¶
The Error
builtin type is used to represent an error. It can be used e.g. in combination with the Result<T>
type.
Constructors¶
The Error
builtin type offers the following constructors:
void Error()
: Initialize an empty error object. This object has an error code of 0 and the error messageRuntime error
void Error(int, string)
: Initialize an error object with an error code and an error messagevoid Error(string)
: Initialize an error object with an error message
Methods¶
The Error
builtin type offers the following methods:
void print()
: Prints the error message to the standard error outputvoid toPanic()
: Triggers a panic with this error