StringBuf

StringBuf: StringBuf, size, operator[], operator[-], operator[]=, operator[-]=, cString, data, empty, append, removeFirst, removeLast, hash, iterator, contains, indexOf, substr, split, parseInt, escape, print, compare, all, any, none

Functions: operator+, operator==

struct StringBuf: Comparable, Hashable, Printable

A resizable string buffer.

StringBuf

StringBuf()

Initializes an empty string.

StringBuf(public int capacity)

Initializes an empty string with pre-allocated capacity.

StringBuf(string s)

Initializes a string with the contents of the given string.

StringBuf(char* pointer, int length)

Initializes a string with the characters from a character array of known length.

StringBuf(char* cString)

Initializes a string with the contents of a null-terminated C-style string.

Note: This constructor has a time complexity of O(n) because it has to calculate the length of the argument string. If you know the length beforehand, consider using StringBuf(char* pointer, int length) instead.

StringBuf(public int uninitializedSize)

Initializes the buffer to contain the given number of uninitialized bytes.

size

int size()

Returns the number of characters.

operator[]

char operator[](int index)

Returns the character at the given index.

operator[-]

char operator[-](int index)

Returns the character at the given offset from the end. Offset 1 is the last character; offsets outside 1..size() abort.

operator[]=

void operator[]=(int index, char c)

Sets the character at the given index.

operator[-]=

void operator[-]=(int index, char c)

Sets the character at the given offset from the end. Offset 1 is the last character; offsets outside 1..size() abort.

cString

char* cString()

Returns the string as a C-style, i.e. null-terminated, string. Modifying this after calling this function invalidates the returned pointer.

data

char[*] data()

Returns a pointer to the first character in the string.

empty

bool empty()

Returns true if the string has no characters.

append

void append(char c)

Appends the given character.

void append(string s)

Appends the given string.

void append<T: Printable>(T& value)

Appends the printed form of the given value.

void append(RepeatIterator<string> repetitions)

Appends the repetition without allocating a temporary.

removeFirst

void removeFirst()

Removes the first character from the string. Other characters are moved towards the beginning of the string by one index.

removeLast

void removeLast()

Removes the last character from the string.

hash

uint64 hash()

Supports using strings with sets and dicts

iterator

StringIterator iterator()

Returns an iterator over the characters.

contains

bool contains(char c)

Returns true if the string contains the given character.

indexOf

int indexOf(char c)

Returns the index of the given character, or the size if it's not found.

int indexOf(char c, int start)

Returns the index of the given character, or the size if it's not found. Starts from start.

substr

string substr(int start)

Returns the substring of the string starting from the given index, until the end of the string.

string substr(Range<int> range)

Returns the substring of the string in the given range, [inclusive, exclusive]

split

List<string> split(char delim)

Splits the string by the given delimiter.

List<string> split()

Splits the string by spaces.

parseInt

int? parseInt()

Parses the string as an integer, or returns null.

escape

StringBuf escape()

Returns the string with special characters escaped.

print

void print(OutputStream& stream)

Writes this string to the given stream.

compare

Ordering compare(StringBuf& other)

Compares this string to another.

all

bool all(bool(char&) predicate)

Returns true if all characters satisfy the predicate.

any

bool any(bool(char&) predicate)

Returns true if any character satisfies the predicate.

none

bool none(bool(char&) predicate)

Returns true if no character satisfies the predicate.

operator+

StringBuf operator+(string a, string b)

Concatenates the two strings.

StringBuf operator+(string a, char b)

Concatenates the string and the character.

StringBuf operator+(string a, char[*] b)

Concatenates the two strings.

StringBuf operator+(StringBuf a, string b)

Concatenates the two strings.

StringBuf operator+(string a, StringBuf b)

Concatenates the two strings.

StringBuf operator+(StringBuf a, StringBuf b)

Concatenates the two strings.

StringBuf operator+(StringBuf a, char b)

Concatenates the string and the character.

StringBuf operator+(StringBuf a, char[*] b)

Concatenates the two strings.

StringBuf operator+(RepeatIterator<string> a, string b)

Concatenates the repetition and the string.

StringBuf operator+(string a, RepeatIterator<string> b)

Concatenates the string and the repetition.

StringBuf operator+(RepeatIterator<string> a, RepeatIterator<string> b)

Concatenates two repetitions.

operator==

bool operator==(StringBuf& a, StringBuf& b)

Returns true if both strings are equal.

bool operator==(StringBuf& a, string b)

Returns true if both strings are equal.

bool operator==(string a, StringBuf& b)

Returns true if both strings are equal.

bool operator==(char[*] a, StringBuf& b)

Returns true if both strings are equal.

bool operator==(StringBuf& a, char[*] b)

Returns true if both strings are equal.