StringBuf

StringBuf: StringBuf, size, operator[], operator[-], operator[]=, operator[-]=, cString, data, empty, push, append, capacity, reserve, clear, toUpper, toLower, first, last, removeFirst, pop, hash, iterator, contains, indexOf, substr, split, parseInt, parseFloat, parseFloat64, escape, print, compare, all, any, none, take

Functions: operator+, toCString

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; out-of-bounds access aborts in debug and release-safe builds.

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; out-of-bounds access aborts in debug and release-safe builds.

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.

push

void push(char c)

Adds the given character to the end of the string.

append

void append(string s)

Appends the given string. The given string may not point to the StringBuf itself.

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

Appends the printed form of the given value.

void append<T: Printable + Copyable>(T value)

Appends the printed form of the given value. Copyable values (including constants, which have no address to borrow) bind by value.

void append(RepeatIterator<string> repetitions)

Appends the repetition without allocating a temporary.

capacity

int capacity()

Returns the number of characters the string can store without allocating more memory.

reserve

void reserve(int minimumCapacity)

Ensures that the capacity is large enough to store the given number of characters.

clear

void clear()

Removes all characters from the string, keeping the allocated buffer.

toUpper

void toUpper()

Converts all characters to uppercase in place (ASCII only).

toLower

void toLower()

Converts all characters to lowercase in place (ASCII only).

first

char first()

Returns the first character.

last

char last()

Returns the last character.

removeFirst

char removeFirst()

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

pop

char pop()

Removes and returns the last character.

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 null if it's not found.

int? indexOf(char c, int start)

Returns the index of the given character, or null 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.

parseFloat

float? parseFloat()

Parses the string as a 32-bit float, or returns null. See string.parseFloat.

parseFloat64

float64? parseFloat64()

Parses the string as a 64-bit float, or returns null. See string.parseFloat.

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<Pred: bool(char&)>(Pred predicate)

Returns true if all characters satisfy the predicate.

any

bool any<Pred: bool(char&)>(Pred predicate)

Returns true if any character satisfies the predicate.

none

bool none<Pred: bool(char&)>(Pred predicate)

Returns true if no character satisfies the predicate.

take

StringBuf take()

Moves the string out, leaving an empty string in its place.

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.

toCString

SmallList<char, 256> toCString(string text)

Copies the string into a null-terminated buffer for passing to C functions. Strings shorter than 256 characters use inline storage, avoiding heap allocation; longer ones spill to a heap buffer. The data pointer is only valid while the returned buffer is alive.