#! @file string.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the methods of `string`, which owns the bytes of text it holds, and #! the functions that build one and give it back. #! #! `string` is among the types the syntax makes, which the compiler declares #! itself, since interpolation, as `$"{count} items"`, builds one. A string is #! `owning`, so the memory goes back where its scope ends. Text that is only read #! is a `strview`, which owns nothing: a literal such as `"hello"` is one, and so #! is what `view` gives. A string whose allocator is `nothing` holds memory from #! `raw_allocate`, and one made with an allocator gives its memory back to that #! allocator. Either way the zero value of a string holds no memory, so closing #! one that was never built does nothing. module core; #! The number of bytes the string holds. #! @param this The string to measure. #! @returns How many bytes of text it holds, which is no more than its capacity. pub val string::length: u64 { get => this._len; } #! The number of bytes the string has room for. #! @param this The string to measure. #! @returns How many bytes it can hold before it has to grow. pub val string::capacity: u64 { get => this._cap; } #! The allocator the string takes its memory from, or `nothing` where it takes #! it from `raw_allocate`, so what is made from it can take memory alike. #! @param this The string. #! @returns The allocator, or `nothing`. pub val string::alloc: maybe<&mut allocator> { get => this._alc; } #! Whether the string holds no text. #! @param this The string to check. #! @returns Whether its length is zero. pub val string::is_empty: bool { get => this._len == 0; } #! Whether the string has no room left, so the next byte added makes it grow. #! @param this The string to check. #! @returns Whether its length is its capacity. pub val string::is_full: bool { get => this._len == this._cap; } #! Makes a string that holds no text yet. #! @param alloc An optional allocator for the string to take memory from. A #! string given none takes it from `raw_allocate`. #! @returns The empty string, which holds no memory until it is written to. pub def string::new(alloc: maybe<&mut allocator>): string { return string { _alc = alloc, _dat = null, _len = 0, _cap = 0 }; } #! Makes a string holding a copy of the text given. #! @param text The text to copy, such as a literal or a view of one. #! @param alloc An optional allocator for the string to take memory from. #! @returns The string, which holds no text where there was no memory for it. #! @remarks An allocator that gives no memory stops the program. pub def string::from(text: strview, alloc: maybe<&mut allocator>): string { var made := string::new(alloc); made.append(text); return made; } #! Views the text the string holds, without copying it. #! @param this The string to view. #! @returns A view of its bytes, which is only good until the string grows, #! shrinks, or closes. pub def string::view(&this): strview { return strview::over(this._dat as *u8, this._len); } #! Lends a writable view of the bytes the string holds, without copying them. #! @param this The string to view. #! @returns A view of its bytes, which writes them where they are, and is only #! good until the string grows, shrinks, or closes. pub def string::slice(&mut this): slice { return slice::::over(this._dat, this._len); } #! Reads the byte at an index, which `s[index]` calls. #! @param this The string to read. #! @param index The index of the byte, below `length`, past which a checked #! program stops. #! @returns The byte, which is only good until the string grows, shrinks, or #! closes. pub def string::at(&this, index: u64): &u8 { # Indexed as a view of the bytes, it is checked as a view's index is. return &span:: { _dat = this._dat, _len = this._len }[index]; } #! Reaches the byte at an index to write it, which `s[index] = b` calls. #! @param this The string to write. #! @param index The index of the byte, below `length`, past which a checked #! program stops. #! @returns The byte, which is only good until the string grows, shrinks, or #! closes. pub def string::at(&mut this, index: u64): &mut u8 { return &mut slice:: { _dat = this._dat, _len = this._len }[index]; } #! Gets the byte at an index, if the string holds one there, for an index that #! may be past the end in a program that is right. #! @param this The string to read. #! @param index The index of the byte. #! @returns Maybe the byte, which is only good until the string grows, shrinks, #! or closes; otherwise, `nothing`. pub def string::get(&this, index: u64): maybe<&u8> { # Reached as a view's byte, whose check the test answers. return index < this._len ? &span:: { _dat = this._dat, _len = this._len }[index] : nothing {}; } #! Gets the byte at an index to write it, if the string holds one there. #! @param this The string to write. #! @param index The index of the byte. #! @returns Maybe the byte, which is only good until the string grows, shrinks, #! or closes; otherwise, `nothing`. pub def string::get(&mut this, index: u64): maybe<&mut u8> { return index < this._len ? &mut slice:: { _dat = this._dat, _len = this._len }[index] : nothing {}; } #! Makes room for at least a number of bytes, keeping the text it holds. #! @param this The string to make room in. #! @param capacity The number of bytes it should have room for. #! @remarks A string already that large is left as it is, and an allocator that #! gives no more memory stops the program. pub def string::reserve(&mut this, capacity: u64): void { if capacity <= this._cap { return; } this.recapacity(capacity); } #! Makes the string hold a number of bytes, adding zero bytes after the last or #! removing those past it, as a reader filling the room it made does. #! @param this The string to resize. #! @param length How many bytes it holds after. #! @remarks The memory is kept where it shrinks, and an allocator that gives no #! more memory stops the program where it grows. pub def string::resize(&mut this, length: u64): void { if length > this._cap { this.grow(length); } if length > this._len { memset(this._dat + this._len, 0, length - this._len); } this._len = length; } #! Adds a byte after the last. #! @param this The string to append to. #! @param byte The byte to append. #! @remarks An allocator that gives no more memory stops the program. pub def string::push(&mut this, byte: u8): void { if this._len == this._cap { this.grow(this._len + 1); } this._dat[this._len] = byte; this._len++; } #! Adds the bytes of some text after the last. #! @param this The string to append to. #! @param text The text to append, which may view this string's own bytes. #! @remarks An allocator that gives no more memory stops the program. pub def string::append(&mut this, text: strview): void { val count := text.length; if count == 0 { return; } if this._len + count > this._cap { this.grow(this._len + count); } memcpy(this._dat + this._len, text.data, count); this._len += count; } #! Removes every byte, keeping the memory the string holds them in. #! @param this The string to empty. pub def string::clear(&mut this): void { this._len = 0; } #! Gives back the memory the string holds, leaving it empty. #! @param this The string to close out. pub def string::close(&mut this): void { if this._dat == null { return; } match this._alc { some(owner) => owner.dispose(this._dat, this._cap); none => raw_release(this._dat); } this._dat = null; this._len = 0; this._cap = 0; } #! Compares one string to another for equality. #! @param this The string to compare against. #! @param other The string to compare with. #! @returns Whether both hold the same bytes, in the same order. pub def string::equals(&this, other: &string): bool { return this.view().equals(&other.view()); } #! Compares one string with another, by their bytes and then by their length. #! @param this The string to compare against. #! @param other The string to compare with. #! @retval -1 Where this string orders before the other. #! @retval 0 Where they hold the same bytes. #! @retval 1 Where this string orders after the other. pub def string::compare(&this, other: &string): i64 { return this.view().compare(other.view()); } #! What `iter(&this)` lends, which reads the bytes where the string holds them. pub type string::cursor = span::cursor; #! What `iter(&mut this)` lends, which writes the bytes where the string holds #! them. pub type string::mut_cursor = slice::mut_cursor; #! Lends a cursor that reads the bytes, in order, which `for byte in s` walks. #! @param this The string to walk. #! @returns The cursor, which is only good until the string grows, shrinks, or #! closes. pub def string::iter(&this): span::cursor { return this.view().iter(); } #! Lends a cursor that writes the bytes where they are, which `for &mut byte in #! s` walks. #! @param this The string to walk. #! @returns The cursor, which is only good until the string grows, shrinks, or #! closes. pub def string::iter(&mut this): slice::mut_cursor { var whole := this.slice(); return whole.iter(); } #! Hashes the string provided. #! @param this The string to hash. #! @returns The hash code of the text it holds, which equal strings share. pub def string::hash(&this): u64 { return this.view().hash(); } #! Builds a string holding the same text as another, with its allocator. #! @param other The string to copy text from. #! @returns The copy, which owns memory of its own. pub def string::copy_of(other: &string): string { return string::from(other.view(), other._alc); } #! Builds a string holding the text of another, leaving the other empty. #! @param other The string to move text from, which holds nothing after. #! @returns The moved string, which takes the memory and the allocator with it. #! @remarks Nothing is copied: the memory moves as it stands, and the other is #! left empty rather than dead, so closing it afterwards is harmless. pub def string::move_of(other: &mut string): string { var made: string = string { _alc = other._alc, _dat = other._dat, _len = other._len, _cap = other._cap }; other._dat = null; other._len = 0; other._cap = 0; return made; } #! Views a run of the text, from an index for a number of bytes. #! @param this The string to narrow. #! @param start Where the narrower view begins, from `0` to `length`. #! @param count How many bytes it covers, cut short by the end of the text. #! @returns A view of that run, which is empty where it begins past the end, and #! is only good until the string grows, shrinks, or closes. pub def string::substr(&this, start: u64, count: u64): strview { return this.view().substr(start, count); } #! Tells whether the text begins with some other text. #! @param this The string to read. #! @param prefix The text to look for at its beginning. #! @returns Whether it begins with the prefix, which empty text always does. pub def string::starts_with(&this, prefix: strview): bool { return this.view().starts_with(prefix); } #! Tells whether the text ends with some other text. #! @param this The string to read. #! @param suffix The text to look for at its end. #! @returns Whether it ends with the suffix, which empty text always does. pub def string::ends_with(&this, suffix: strview): bool { return this.view().ends_with(suffix); } #! Makes a string of the text without the whitespace at its beginning. #! @param this The string to read. #! @returns A new string holding what is left, from the same allocator. pub def string::ltrim(&this): string { return string::from(this.view().ltrim(), this._alc); } #! Makes a string of the text without the whitespace at its end. #! @param this The string to read. #! @returns A new string holding what is left, from the same allocator. pub def string::rtrim(&this): string { return string::from(this.view().rtrim(), this._alc); } #! Makes a string of the text without the whitespace at either end. #! @param this The string to read. #! @returns A new string holding what is left, from the same allocator. pub def string::trim(&this): string { return string::from(this.view().trim(), this._alc); } #! Compares the text with other text, by their bytes and then by their length. #! @param this The string to compare against. #! @param other The text to compare with. #! @retval -1 Where this string orders before the other. #! @retval 0 Where they hold the same bytes. #! @retval 1 Where this string orders after the other. pub def string::compare(&this, other: strview): i64 { return this.view().compare(other); } #! Makes text of the string with every letter in upper case. #! @param this The string to read. #! @returns A new string holding the converted text. pub def string::upper(&this): string { return this.view().upper(); } #! Makes text of the string with every letter in lower case. #! @param this The string to read. #! @returns A new string holding the converted text. pub def string::lower(&this): string { return this.view().lower(); } #! Makes text of a width, with the string's text at its end and a byte before #! it. #! @param this The string to read. #! @param width How many bytes the text should hold. #! @param fill The byte to put before it. #! @returns A new string holding the padded text, which is a copy of this one #! where it is already that wide. pub def string::lpad(&this, width: u64, fill: u8): string { return this.view().lpad(width, fill); } #! Makes text of a width, with the string's text at its beginning and a byte #! after it. #! @param this The string to read. #! @param width How many bytes the text should hold. #! @param fill The byte to put after it. #! @returns A new string holding the padded text, which is a copy of this one #! where it is already that wide. pub def string::rpad(&this, width: u64, fill: u8): string { return this.view().rpad(width, fill); } # Gives the string room for more bytes than it holds, which is what it needs # before more are added. def string::grow(&mut this, wanted: u64): void { var capacity := this._cap == 0 ? 16u : this._cap; while capacity < wanted { capacity *= 2; } this.recapacity(capacity); } # Moves the bytes to memory of another size, keeping what fits. An allocator that # gives no memory stops the program, as `resize` does. def string::recapacity(&mut this, capacity: u64): void { if capacity == this._cap { return; } if capacity == 0 { this.close(); return; } this._dat = resize::(this._alc, this._dat, this._cap, capacity); this._cap = capacity; if this._len > capacity { this._len = capacity; } }