Skip to content

Commit

Permalink
Add documentation for Result<T> builtin type
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Mar 17, 2024
1 parent d8bddea commit 06dce5d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 40 deletions.
101 changes: 74 additions & 27 deletions docs/docs/language/builtin-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,97 @@ The `String` builtin type offers the following constructors:
- `void String()`: Initialize empty
- `void String(string)`: Initialize with a raw `string` as start value
- `void String(char)`: Initialize with a single char
- `void String(String)`: Initialize by copying another `String` (copy constructor)
- `void String(const String&)`: Initialize by copying another `String` (copy constructor)
- `void String(int)`: Initialize with an initial size
- `void String(long)`: Initialize with an initial size

### Methods
The `String` builtin type offers the following methods:

- `void append(string)`: Appends a raw string
- `void append(String)`: Appends a string
- `void append(const String&)`: Appends a string
- `void append(char)`: Appends a single char
- `char* getRaw()`: Returns a char* to the heap allocated value
- `long getLength()`: Returns the length of the string in chars
- `string getRaw()`: Returns a char* to the heap allocated value
- `unsigned long getLength()`: Returns the length of the string in chars
- `bool isEmpty()`: Checks if the string has a length of 0
- `long getCapacity()`: Returns the allocated space in bytes
- `unsigned long getCapacity()`: Returns the allocated space in bytes
- `bool isFull()`: Checks if the length is equal with the capacity
- `void clear()`: Clear the value of the string
- `long find(string, int)`: Returns the index, where a substring was found, starting from a start index
- `long find(string, long)`: Returns the index, where a substring was found, starting from a start index
- `long find(string, short)`: Returns the index, where a substring was found, starting from a start index
- `long find(string, unsigned int)`: Returns the index, where a substring was found, starting from a start index
- `long find(string, unsigned long)`: Returns the index, where a substring was found, starting from a start index
- `long find(char, unsigned long)`: Returns the index, where a subchar was found, starting from a start index
- `long rfind(string, unsigned int)`: Returns the index, where a substring was found, starting from reversed at a start index
- `long rfind(string, unsigned long)`: Returns the index, where a substring was found, starting from reversed at a start index
- `long rfind(char, unsigned long)`: Returns the index, where a subchar was found, starting from reversed at a start index
- `bool contains(string)`: Checks if the string contains a substring
- `bool startsWith(string)`: Checks if the string starts with a substring
- `bool endsWith(string)`: Checks if the string ends with a substring
- `void reverse()`: Reverses the value of the string
- `String substring(int, long)`: Returns the substring from start index `x` and length `y`
- `String substring(long, long)`: Returns the substring from start index `x` and length `y`
- `String substring(short, long)`: Returns the substring from start index `x` and length `y`
- `void reserve(int)`: Increase the capacity to the given number
- `void reserve(long)`: Increase the capacity to the given number
- `void reserve(short)`: Increase the capacity to the given number
- `void replace(string, string, unsigned long)`: Replaces a substring with another string, starting from a start index
- `void replaceAll(string, string)`: Replaces all occurrences of a substring with another string
- `void replaceAll(char, char)`: Replaces all occurrences of a subchar with another char
- `String getSubstring(unsigned int, long)`: Returns the substring from start index `x` and length `y`
- `String getSubstring(unsigned long, long)`: Returns the substring from start index `x` and length `y`
- `String getSubstring(unsigned short, long)`: Returns the substring from start index `x` and length `y`
- `void reserve(unsigned int)`: Increase the capacity to the given number
- `void reserve(unsigned long)`: Increase the capacity to the given number
- `void 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 string
- `isRawEqual(string, string)`: Checks if two raw strings are equal in value

### Operators
The `String` builtin type overrides the following operators:

- `String operator+(String, String)`: Concatenates two strings and returns the result
- `String operator+(String, string)`: Concatenates a string and a raw string and returns the result
- `String operator+(string, String)`: Concatenates a raw string and a string and returns the result
- `String operator+(string, string)`: Concatenates two raw strings and returns the result
- `String operator+(const String&, const String&)`: Concatenates two strings and returns the result
- `String operator+(const String&, const string&)`: Concatenates a string and a raw string and returns the result
- `String operator+(const String&, const char&)`: Concatenates a string and a raw string and returns the result
- `String operator+(const string&, const String&)`: Concatenates a raw string and a string and returns the result
- `String operator+(const string&, const string&)`: Concatenates two raw strings and returns the result
- `String operator+(const string&, const char&)`: Concatenates two raw strings and returns the result
- `void operator+=(String&, String)`: Appends a string
- `void operator+=(String&, string)`: Appends a raw string
- `void operator+=(String&, string)`: Appends a single char
- `String operator*(String, int)`: Concatenates a string with itself n times
- `String operator*(String, long)`: Concatenates a string with itself n times
- `String operator*(String, short)`: Concatenates a string with itself n times
- `String operator*(int, String)`: Concatenates a string with itself n times
- `String operator*(long, String)`: Concatenates a string with itself n times
- `String operator*(short, String)`: Concatenates a string with itself n times
- `void operator+=(String&, char)`: Appends a single char
- `String operator*(const String&, int)`: Concatenates a string with itself n times
- `String operator*(const String&, long)`: Concatenates a string with itself n times
- `String operator*(const String&, short)`: Concatenates a string with itself n times
- `String operator*(int, const String&)`: Concatenates a string with itself n times
- `String operator*(long, const String&)`: Concatenates a string with itself n times
- `String operator*(short, const String&)`: Concatenates a string with itself n times
- `void operator*=(String&, int)`: Concatenates with itself n times
- `void operator*=(String&, long)`: Concatenates with itself n times
- `void operator*=(String&, short)`: Concatenates with itself n times
- `bool operator==(String, String)`: Checks if two strings are equal in value
- `bool operator!=(String, String)`: Checks if two strings are unequal in value
- `bool operator==(const String&, const String&)`: Checks if two strings are equal in value
- `bool operator==(const String&, string)`: Checks if two strings are equal in value
- `bool operator==(string, const String&)`: Checks if two strings are equal in value
- `bool operator!=(const String&, const String&)`: Checks if two strings are unequal in value
- `bool operator!=(const String&, string)`: Checks if two strings are unequal in value
- `bool 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 value
- `void 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 panic
- `Error 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 value
- `bool 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 value
- `Result<T> err(const Error&)`: Returns a Result object with an error
- `Result<T> err(int, string)`: Returns a Result object with an error, constructed with an error code and an error message
- `Result<T> err(string)`: Returns a Result object with an error, constructed with an error message
26 changes: 13 additions & 13 deletions std/runtime/string_rt.spice
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ public f<bool> operator==(const String& a, const String& b) {
return true;
}

public f<bool> operator==(const String& a, const string& b) {
public f<bool> operator==(const String& a, string b) {
return isRawEqual(a.getRaw(), b);
}

public f<bool> operator==(const string& a, const String& b) {
public f<bool> operator==(string a, const String& b) {
return isRawEqual(a, b.getRaw());
}

Expand Down Expand Up @@ -294,7 +294,7 @@ public inline f<string> String.getRaw() {
*
* @return Current length of the string
*/
public inline f<long> String.getLength() {
public inline f<unsigned long> String.getLength() {
return this.length;
}

Expand All @@ -310,7 +310,7 @@ public inline f<bool> String.isEmpty() {
*
* @return Current capacity of the string
*/
public inline f<long> String.getCapacity() {
public inline f<unsigned long> String.getCapacity() {
return this.capacity;
}

Expand Down Expand Up @@ -417,24 +417,24 @@ public f<long> String.rfind(string needle, unsigned long startIndex = 0l) {
}

/**
* Searches for a subchar in a string from the back. Returns -1 if the char was not found.
* Searches for a substring in a string from the back. Returns -1 if the string was not found.
*
* @param startIndex Index where to start the search
* @return Index, where the subchar was found / -1
* @return Index, where the substring was found / -1
*/
public f<long> String.rfind(char needle, unsigned long startIndex = 0l) {
const String needleStr = String(needle);
return this.rfind(needleStr.getRaw(), startIndex);
public f<long> String.rfind(string needle, unsigned int startIndex) {
return this.rfind(needle, (unsigned long) startIndex);
}

/**
* Searches for a substring in a string from the back. Returns -1 if the string was not found.
* Searches for a subchar in a string from the back. Returns -1 if the char was not found.
*
* @param startIndex Index where to start the search
* @return Index, where the substring was found / -1
* @return Index, where the subchar was found / -1
*/
public f<long> String.rfind(string needle, unsigned int startIndex) {
return this.rfind(needle, (unsigned long) startIndex);
public f<long> String.rfind(char needle, unsigned long startIndex = 0l) {
const String needleStr = String(needle);
return this.rfind(needleStr.getRaw(), startIndex);
}

/**
Expand Down

0 comments on commit 06dce5d

Please sign in to comment.