Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,201 @@
|
||||
#! @file text.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains text walked lazily, each piece a view into the text as it is
|
||||
#! reached, without a vector of them: its runs between a byte, its lines, its
|
||||
#! words, and its code points. `.to_vector()` gives the pieces as a vector.
|
||||
|
||||
module core;
|
||||
|
||||
|
||||
#! Splits text by a byte, giving a view of each run between them in turn: one
|
||||
#! more than there are of the byte, so `"a,,b"` gives `a`, an empty run, and
|
||||
#! `b`, and empty text gives one empty run.
|
||||
#! @param this The text to split.
|
||||
#! @param delim The byte to split by, which no run holds.
|
||||
#! @returns The runs, each viewing into the text, so they are only good for as
|
||||
#! long as it is.
|
||||
pub def strview::split(&this, delim: u8): splits {
|
||||
return splits { _left = this, _delim = delim, _done = false };
|
||||
}
|
||||
|
||||
#! Splits the text by a byte, giving a view of each run between them in turn.
|
||||
#! @param this The string to split.
|
||||
#! @param delim The byte to split by, which no run holds.
|
||||
#! @returns The runs, each viewing into the string, so they are only good until
|
||||
#! it grows, shrinks, or closes.
|
||||
pub def string::split(&this, delim: u8): splits {
|
||||
return this.span.split(delim);
|
||||
}
|
||||
|
||||
#! What `split` gives: the runs of text between each of a byte, from the front
|
||||
#! or the far end.
|
||||
pub struct splits : backward_iterator<strview> {
|
||||
var _left: strview; #! The text not yet given.
|
||||
var _delim: u8; #! The byte split by.
|
||||
var _done: bool; #! Whether the last run has been given.
|
||||
}
|
||||
|
||||
#! A split that gives no runs at all, as of what has nothing to split.
|
||||
#! @returns The split.
|
||||
pub def splits::empty(): splits {
|
||||
return splits { _left = strview::blank(), _delim = 0, _done = true };
|
||||
}
|
||||
|
||||
#! Gives the next run, up to the next of the byte or the end of the text.
|
||||
#! @param this The split.
|
||||
#! @returns Maybe a view of the run; otherwise, `nothing` once the last is given.
|
||||
pub def splits::next(&mut this): maybe<strview> {
|
||||
return nothing {} if this._done;
|
||||
val text := this._left;
|
||||
val at := find_byte(text._dat, 0, text._len, this._delim);
|
||||
if at == text._len {
|
||||
this._done = true;
|
||||
return text;
|
||||
}
|
||||
this._left = text.substr(at + 1, text._len - at - 1);
|
||||
return text.substr(0, at);
|
||||
}
|
||||
|
||||
#! Gives the run at the far end, back to the last of the byte or the front of
|
||||
#! what is left.
|
||||
#! @param this The split.
|
||||
#! @returns Maybe a view of the run; otherwise, `nothing` once the last is given.
|
||||
pub def splits::next_back(&mut this): maybe<strview> {
|
||||
return nothing {} if this._done;
|
||||
val text := this._left;
|
||||
val at := find_last_byte(text._dat, 0, text._len, this._delim);
|
||||
if at == text._len {
|
||||
this._done = true;
|
||||
return text;
|
||||
}
|
||||
this._left = text.substr(0, at);
|
||||
return text.substr(at + 1, text._len - at - 1);
|
||||
}
|
||||
|
||||
#! Gives each line of text without the `\n` that ends it or a `\r` before that,
|
||||
#! as reading a stream's lines does: a last line without a line break is still a
|
||||
#! line, and text ending in one has no empty line after it.
|
||||
#! @param this The text to walk.
|
||||
#! @returns The lines, each viewing into the text.
|
||||
pub def strview::lines(&this): text_lines {
|
||||
return text_lines { _left = this };
|
||||
}
|
||||
|
||||
#! What `lines` gives: each line of text.
|
||||
pub struct text_lines : iter<strview> {
|
||||
var _left: strview; #! The text not yet given.
|
||||
}
|
||||
|
||||
#! Gives the next line.
|
||||
#! @param this The lines.
|
||||
#! @returns Maybe a view of the line; otherwise, `nothing` once the text ends.
|
||||
pub def text_lines::next(&mut this): maybe<strview> {
|
||||
val text := this._left;
|
||||
return nothing {} if text._len == 0;
|
||||
val at := find_byte(text._dat, 0, text._len, '\n');
|
||||
var line := text.substr(0, at);
|
||||
this._left = at == text._len ? text.substr(text._len, 0) : text.substr(at + 1, text._len - at - 1);
|
||||
if at < text._len && line._len > 0 && line[line._len - 1] == '\r' {
|
||||
line = line.substr(0, line._len - 1);
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
#! Gives each word of text: each run between whitespace, as `is_space` says,
|
||||
#! which no word holds.
|
||||
#! @param this The text to walk.
|
||||
#! @returns The words, each viewing into the text.
|
||||
pub def strview::words(&this): text_words {
|
||||
return text_words { _left = this };
|
||||
}
|
||||
|
||||
#! What `words` gives: each run of text between whitespace.
|
||||
pub struct text_words : iter<strview> {
|
||||
var _left: strview; #! The text not yet given.
|
||||
}
|
||||
|
||||
#! Gives the next word.
|
||||
#! @param this The words.
|
||||
#! @returns Maybe a view of the word; otherwise, `nothing` once the text ends.
|
||||
pub def text_words::next(&mut this): maybe<strview> {
|
||||
val text := this._left;
|
||||
var start: u64 = 0;
|
||||
while start < text._len && text[start].is_space {
|
||||
start++;
|
||||
}
|
||||
var end := start;
|
||||
while end < text._len && !text[end].is_space {
|
||||
end++;
|
||||
}
|
||||
this._left = text.substr(end, text._len - end);
|
||||
return nothing {} if start == end;
|
||||
return text.substr(start, end - start);
|
||||
}
|
||||
|
||||
#! Gives each code point of text, decoding UTF-8. A byte that begins no
|
||||
#! character, as one past a sequence's end, or a sequence that is cut short,
|
||||
#! too long for its value, or a surrogate's, gives U+FFFD, one for each byte
|
||||
#! that starts none.
|
||||
#! @param this The text to walk.
|
||||
#! @returns The code points.
|
||||
pub def strview::chars(&this): code_points {
|
||||
return code_points { _left = this };
|
||||
}
|
||||
|
||||
#! What `chars` gives: each code point of UTF-8 text.
|
||||
pub struct code_points : iter<u32> {
|
||||
var _left: strview; #! The text not yet given.
|
||||
}
|
||||
|
||||
#! What a malformed byte gives, U+FFFD.
|
||||
const replacement: u32 = 0xFFFD;
|
||||
|
||||
#! Gives the next code point.
|
||||
#! @param this The code points.
|
||||
#! @returns Maybe the code point; otherwise, `nothing` once the text ends.
|
||||
pub def code_points::next(&mut this): maybe<u32> {
|
||||
val text := this._left;
|
||||
return nothing {} if text._len == 0;
|
||||
val lead := text[0];
|
||||
if lead < 0x80 {
|
||||
this._left = text.substr(1, text._len - 1);
|
||||
return lead as u32;
|
||||
}
|
||||
|
||||
# How many bytes follow the lead, what it gives of the value, and the least
|
||||
# value that many encode, below which the sequence is too long.
|
||||
var follow: u64 = 0;
|
||||
var value: u32 = 0;
|
||||
var least: u32 = 0;
|
||||
if (lead & 0xE0) == 0xC0 {
|
||||
follow = 1;
|
||||
value = (lead & 0x1F) as u32;
|
||||
least = 0x80;
|
||||
} else if (lead & 0xF0) == 0xE0 {
|
||||
follow = 2;
|
||||
value = (lead & 0x0F) as u32;
|
||||
least = 0x800;
|
||||
} else if (lead & 0xF8) == 0xF0 {
|
||||
follow = 3;
|
||||
value = (lead & 0x07) as u32;
|
||||
least = 0x10000;
|
||||
}
|
||||
|
||||
var valid := follow != 0 && follow < text._len;
|
||||
for k in 1..=follow {
|
||||
break if !valid;
|
||||
val byte := text[k];
|
||||
valid = (byte & 0xC0) == 0x80;
|
||||
value = (value << 6) | (byte & 0x3F) as u32;
|
||||
}
|
||||
valid = valid && value >= least && value <= 0x10FFFF && (value < 0xD800 || value > 0xDFFF);
|
||||
if !valid {
|
||||
this._left = text.substr(1, text._len - 1);
|
||||
return replacement;
|
||||
}
|
||||
this._left = text.substr(follow + 1, text._len - follow - 1);
|
||||
return value;
|
||||
}
|
||||
Reference in new issue
Block a user