Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+301
View File
@@ -0,0 +1,301 @@
#! @file strview.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains `strview`, the view of text that is only read, and the methods of
#! text: narrowing, splitting, finding, trimming, comparing, changing case,
#! padding, and reading numbers.
#!
#! Anything that only reads text takes a `strview`, so a literal, a view of one,
#! and the text a `string` lends are all read by the same function, since a
#! string lends a view of itself wherever one is wanted. Anything that makes new
#! text gives a `string`, which owns the memory it is written into and gives it
#! back where its scope ends.
#!
#! `strview` is an alias of `span<u8>`, among the types the syntax makes, which
#! the compiler declares itself, so it converts to and from one implicitly and
#! has its methods too. The methods of text are declared on the
#! alias because a method cannot be declared on one instance of a generic type:
#! `def span<u8>::trim` would declare a type parameter named `u8`.
import core::swar;
module core;
#! Views a run of the text, from an index for a number of bytes.
#! @param this The text 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.
@inline
pub def strview::substr(&this, start: u64, count: u64): strview {
if start >= this.length {
return strview::empty();
}
val left := this.length - start;
return this.sub(start, count < left ? count : left).some;
}
#! Tells whether the text begins with some other text.
#! @param this The text 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 strview::starts_with(&this, prefix: strview): bool {
return
this.length >= prefix.length &&
this.substr(0, prefix.length).equals(&prefix);
}
#! Tells whether the text ends with some other text.
#! @param this The text 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 strview::ends_with(&this, suffix: strview): bool {
return
this.length >= suffix.length &&
this.substr(this.length - suffix.length, suffix.length).equals(&suffix);
}
#! Finds where a run of text first begins within the text.
#! @param this The text to search.
#! @param text The run to look for.
#! @returns Maybe the index where it first begins; otherwise, `nothing`. Empty
#! text is found at 0.
#! @remarks Its first and last bytes are looked for at once, at eight places a
#! step, and the bytes between compared only where both are found, as
#! Wojciech Muła's search does with vectors.
pub def strview::find(&this, text: strview): maybe<u64> {
val length := text.length;
return 0 if length == 0;
return nothing {} if length > this._len;
return this.index_of(&text[0]) if length == 1;
# The places it may begin are 0 to `last`, eight at a time, then the last few
# read without passing the end.
val data := this._dat;
val last := this._len - length;
val first_byte := swar::repeat(text[0]);
val last_byte := swar::repeat(text[length - 1]);
var at: u64 = 0;
while at <= last {
val places := last + 1 - at;
var firsts: swar::bytes8;
var lasts: swar::bytes8;
if places >= 8 {
firsts = swar::load(data + at) ^ first_byte;
lasts = swar::load(data + at + length - 1) ^ last_byte;
} else {
firsts = swar::load_last(data + at, places) ^ first_byte;
lasts = swar::load_last(data + at + length - 1, places) ^ last_byte;
}
var found: swar::mask8 = firsts.zero() & lasts.zero() & swar::mask8::leading(places);
while found.any() {
val begins := at + found.first();
return begins if memcmp(data + begins + 1, text._dat + 1, length - 2) == 0;
found = found.rest();
}
at += 8;
}
return nothing {};
}
#! Finds where the text first holds any of some bytes.
#! @param this The text to search.
#! @param bytes The bytes to look for.
#! @returns Maybe the index of the first; otherwise, `nothing`, as for no bytes.
#! @remarks Up to four bytes are looked for eight places a step, and more a byte
#! at a time, through a set of the 256.
pub def strview::find_any(&this, bytes: span<u8>): maybe<u64> {
val count := bytes.length;
return nothing {} if count == 0;
val data := this._dat;
if count <= 4 {
# Each byte repeated, where the first stands for any not given.
val a := swar::repeat(bytes[0]);
val b := swar::repeat(bytes[count > 1 ? 1 : 0]);
val c := swar::repeat(bytes[count > 2 ? 2 : 0]);
val d := swar::repeat(bytes[count > 3 ? 3 : 0]);
var at: u64 = 0;
while at < this._len {
val left := this._len - at;
val word := left >= 8 ? swar::load(data + at) : swar::load_last(data + at, left);
val wa: swar::bytes8 = word ^ a;
val wb: swar::bytes8 = word ^ b;
val wc: swar::bytes8 = word ^ c;
val wd: swar::bytes8 = word ^ d;
val found: swar::mask8 = (wa.zero() | wb.zero() | wc.zero() | wd.zero()) & swar::mask8::leading(left);
return at + found.first() if found.any();
at += 8;
}
return nothing {};
}
var set: u64[4] = [0, 0, 0, 0];
for byte in bytes {
set[(byte >> 6) as u64] |= 1u << (byte & 63) as u64;
}
for at in 0..this._len {
val byte := data[at];
return at if ((set[(byte >> 6) as u64] >> (byte & 63) as u64) & 1) != 0;
}
return nothing {};
}
#! Tells whether the text is all ASCII, every byte below 128.
#! @param this The text to check.
#! @returns Whether it is, which empty text is.
#! @remarks Thirty-two bytes are read a step, as four words whose high bits are
#! gathered, then eight, then the last few.
pub def strview::is_ascii(&this): bool {
val data := this._dat;
var at: u64 = 0;
while at + 32 <= this._len {
val high := (swar::load(data + at) | swar::load(data + at + 8) | swar::load(data + at + 16) | swar::load(data + at + 24))
& 0x8080808080808080;
return false if high != 0;
at += 32;
}
while at + 8 <= this._len {
return false if (swar::load(data + at) & 0x8080808080808080) != 0;
at += 8;
}
return (swar::load_last(data + at, this._len - at) & 0x8080808080808080) == 0;
}
#! Views the text without the whitespace at its beginning.
#! @param this The text to read.
#! @returns A view of what is left, which views into the same text.
pub def strview::ltrim(&this): strview {
var at := 0u;
while at < this.length && this[at].is_space {
at++;
}
return this.substr(at, this.length - at);
}
#! Views the text without the whitespace at its end.
#! @param this The text to read.
#! @returns A view of what is left, which views into the same text.
pub def strview::rtrim(&this): strview {
var count := this.length;
while count > 0 && this[count - 1].is_space {
count--;
}
return this.substr(0, count);
}
#! Views the text without the whitespace at either end.
#! @param this The text to read.
#! @returns A view of what is left, which views into the same text.
pub def strview::trim(&this): strview {
return this.rtrim().ltrim();
}
#! Compares text with other text, by their bytes and then by their length.
#! @param this The text to compare against.
#! @param other The text to compare with.
#! @retval -1 Where this text orders before the other.
#! @retval 0 Where they hold the same bytes.
#! @retval 1 Where this text orders after the other.
pub def strview::compare(&this, other: strview): i64 {
val shorter := this.length < other.length ? this.length : other.length;
if shorter != 0 {
val order := memcmp(this._dat, other._dat, shorter);
return order if order != 0;
}
if this.length == other.length {
return 0;
}
return this.length < other.length ? -1 : 1;
}
#! Makes a copy of the text with every ASCII letter in upper case.
#! @param this The text to read.
#! @returns A string holding the converted text.
pub def strview::upper(&this): string {
var made := string::from(this, nothing {});
change_case(made._dat, made._len, true);
return made;
}
#! Makes a copy of the text with every ASCII letter in lower case.
#! @param this The text to read.
#! @returns A string holding the converted text.
pub def strview::lower(&this): string {
var made := string::from(this, nothing {});
change_case(made._dat, made._len, false);
return made;
}
# Changes the case of the ASCII letters of bytes where they are, eight at a time,
# the last word ending where the bytes do, over letters the words before changed,
# which changing again leaves as they are. Fewer than eight a byte at a time.
def change_case(data: *mut u8, length: u64, upper: bool): void {
if length < 8 {
for at in 0..length {
data[at] = upper ? data[at].to_upper : data[at].to_lower;
}
return;
}
var at: u64 = 0;
while true {
val word := swar::load(data + at);
*((data + at) as *mut u64) = upper ? word.upper() : word.lower();
break if at + 8 == length;
at = at + 16 <= length ? at + 8 : length - 8;
}
}
#! Makes text of a width, with this text at its end and a byte repeated before
#! it.
#! @param this The text to read.
#! @param width How many bytes the text should hold.
#! @param fill The byte to put before it.
#! @returns A string holding the padded text, which is a copy of this text where
#! it is already that wide.
pub def strview::lpad(&this, width: u64, fill: u8): string {
var made := string::new(nothing {});
made.reserve(width > this.length ? width : this.length);
var at := this.length;
while at < width {
made.push(fill);
at++;
}
made.append(this);
return made;
}
#! Makes text of a width, with this text at its beginning and a byte repeated
#! after it.
#! @param this The text to read.
#! @param width How many bytes the text should hold.
#! @param fill The byte to put after it.
#! @returns A string holding the padded text, which is a copy of this text where
#! it is already that wide.
pub def strview::rpad(&this, width: u64, fill: u8): string {
var made := string::new(nothing {});
made.reserve(width > this.length ? width : this.length);
made.append(this);
var at := this.length;
while at < width {
made.push(fill);
at++;
}
return made;
}
# NOTE: Extremely fragile! Should be moved somewhere else or dropped because
# it is just storing a string. Replace with a standard error when it is
# finally added.
#! Explains why text could not be read as a number.
pub struct failure {
pub var message: string; #! What was wrong with the text.
}