Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,547 @@
|
||||
#! @file adapters.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the adapters, each the iterator one of `iterator`'s defaults makes of
|
||||
#! another, which it owns: what it gives, and, where what it adapts knows how
|
||||
#! many elements it has left or walks backward, how many it has left or its
|
||||
#! elements from the far end. Each is named for what it does, and only written
|
||||
#! out where a program keeps one in a field.
|
||||
|
||||
module core;
|
||||
|
||||
|
||||
#! What `map` makes: what a function makes of each element.
|
||||
#! @tparam T What the iterator it adapts gives.
|
||||
#! @tparam U What the function gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The function.
|
||||
pub struct mapped<T, U, I, F> : iterator<U> where I: iterator<T> where F: invokable<def(T): U> {
|
||||
var _from: I;
|
||||
var _f: F;
|
||||
}
|
||||
|
||||
#! Gives what the function makes of the next element.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe what it makes; otherwise, `nothing`.
|
||||
pub def mapped<T, U, I, F>::next(&mut this): maybe<U> {
|
||||
if this._from.next() is some(x) {
|
||||
return this._f(move(&mut x));
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! How many elements it has left, which are what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def mapped<T, U, I, F>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
return this._from.remaining();
|
||||
}
|
||||
|
||||
#! Gives what the function makes of the element at the far end.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe what it makes; otherwise, `nothing`.
|
||||
pub def mapped<T, U, I, F>::next_back(&mut this): maybe<U> where I: backward_iterator<T> {
|
||||
if this._from.next_back() is some(x) {
|
||||
return this._f(move(&mut x));
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `filter` makes: each element a test passes.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The test.
|
||||
pub struct filtered<T, I, F> : iterator<T> where I: iterator<T> where F: invokable<def(&T): bool> {
|
||||
var _from: I;
|
||||
var _test: F;
|
||||
}
|
||||
|
||||
#! Gives the next element the test passes.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def filtered<T, I, F>::next(&mut this): maybe<T> {
|
||||
while this._from.next() is some(x) {
|
||||
return move(&mut x) if this._test(&x);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! Gives the element nearest the far end the test passes.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def filtered<T, I, F>::next_back(&mut this): maybe<T> where I: backward_iterator<T> {
|
||||
while this._from.next_back() is some(x) {
|
||||
return move(&mut x) if this._test(&x);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `filter_map` makes: what a function makes of each element, where it
|
||||
#! makes something.
|
||||
#! @tparam T What the iterator it adapts gives.
|
||||
#! @tparam U What the function may give.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The function.
|
||||
pub struct filter_mapped<T, U, I, F> : iterator<U> where I: iterator<T> where F: invokable<def(T): maybe<U>> {
|
||||
var _from: I;
|
||||
var _f: F;
|
||||
}
|
||||
|
||||
#! Gives the next thing the function makes.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe what it makes; otherwise, `nothing`.
|
||||
pub def filter_mapped<T, U, I, F>::next(&mut this): maybe<U> {
|
||||
while this._from.next() is some(x) {
|
||||
var made := this._f(move(&mut x));
|
||||
if made is some(&mut kept) {
|
||||
return move(kept);
|
||||
}
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! Gives the thing the function makes nearest the far end.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe what it makes; otherwise, `nothing`.
|
||||
pub def filter_mapped<T, U, I, F>::next_back(&mut this): maybe<U> where I: backward_iterator<T> {
|
||||
while this._from.next_back() is some(x) {
|
||||
var made := this._f(move(&mut x));
|
||||
if made is some(&mut kept) {
|
||||
return move(kept);
|
||||
}
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `enumerate` makes: each element with its index, counting from 0.
|
||||
#! @tparam T What the iterator it adapts gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
pub struct enumerated<T, I> : iterator<pair<u64, T>> where I: iterator<T> {
|
||||
var _from: I;
|
||||
var _at: u64;
|
||||
}
|
||||
|
||||
#! Gives the next element with its index.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the pair; otherwise, `nothing`.
|
||||
pub def enumerated<T, I>::next(&mut this): maybe<pair<u64, T>> {
|
||||
if this._from.next() is some(x) {
|
||||
val at := this._at;
|
||||
this._at++;
|
||||
return pair::<u64, T> { first = at, second = move(&mut x) };
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! How many elements it has left, which are what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def enumerated<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
return this._from.remaining();
|
||||
}
|
||||
|
||||
#! Gives the element at the far end with its index, which how many are left
|
||||
#! says.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the pair; otherwise, `nothing`.
|
||||
pub def enumerated<T, I>::next_back(&mut this): maybe<pair<u64, T>> where I: backward_iterator<T>, sized_iterator<T> {
|
||||
val left := this._from.remaining();
|
||||
if this._from.next_back() is some(x) {
|
||||
return pair::<u64, T> { first = this._at + left - 1, second = move(&mut x) };
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `zip` makes: the elements of two iterators side by side, until either
|
||||
#! ends.
|
||||
#! @tparam T What the first gives.
|
||||
#! @tparam U What the second gives.
|
||||
#! @tparam I The first.
|
||||
#! @tparam J The second.
|
||||
pub struct zipped<T, U, I, J> : iterator<pair<T, U>> where I: iterator<T> where J: iterator<U> {
|
||||
var _from: I;
|
||||
var _other: J;
|
||||
}
|
||||
|
||||
#! Gives the next element of each, side by side.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the pair; otherwise, `nothing` once either ends.
|
||||
pub def zipped<T, U, I, J>::next(&mut this): maybe<pair<T, U>> {
|
||||
var first := this._from.next();
|
||||
if first is not some(&mut a) {
|
||||
return nothing {};
|
||||
}
|
||||
var second := this._other.next();
|
||||
if second is not some(&mut b) {
|
||||
return nothing {};
|
||||
}
|
||||
return pair::<T, U> { first = move(a), second = move(b) };
|
||||
}
|
||||
|
||||
#! How many pairs it has left: as many as the shorter has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def zipped<T, U, I, J>::remaining(&this): u64 where I: sized_iterator<T> where J: sized_iterator<U> {
|
||||
val a := this._from.remaining();
|
||||
val b := this._other.remaining();
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
#! What `chain` makes: the elements of one iterator, then another's.
|
||||
#! @tparam T What both give.
|
||||
#! @tparam I The first.
|
||||
#! @tparam J The second.
|
||||
pub struct chained<T, I, J> : iterator<T> where I: iterator<T> where J: iterator<T> {
|
||||
var _first: I;
|
||||
var _second: J;
|
||||
var _on_second: bool;
|
||||
}
|
||||
|
||||
#! Gives the next element of the first, or once it has ended, of the second.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def chained<T, I, J>::next(&mut this): maybe<T> {
|
||||
if !this._on_second {
|
||||
if this._first.next() is some(x) {
|
||||
return move(&mut x);
|
||||
}
|
||||
this._on_second = true;
|
||||
}
|
||||
return this._second.next();
|
||||
}
|
||||
|
||||
#! How many elements it has left: what both have.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def chained<T, I, J>::remaining(&this): u64 where I: sized_iterator<T> where J: sized_iterator<T> {
|
||||
return (this._on_second ? 0 : this._first.remaining()) + this._second.remaining();
|
||||
}
|
||||
|
||||
#! Gives the element at the far end of the second, or once it has met its
|
||||
#! front, of the first.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def chained<T, I, J>::next_back(&mut this): maybe<T> where I: backward_iterator<T> where J: backward_iterator<T> {
|
||||
if this._second.next_back() is some(x) {
|
||||
return move(&mut x);
|
||||
}
|
||||
return nothing {} if this._on_second;
|
||||
return this._first.next_back();
|
||||
}
|
||||
|
||||
#! What `take` makes: the first elements, up to a count.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
pub struct taken<T, I> : iterator<T> where I: iterator<T> {
|
||||
var _from: I;
|
||||
var _left: u64;
|
||||
}
|
||||
|
||||
#! Gives the next element, while the count lasts.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def taken<T, I>::next(&mut this): maybe<T> {
|
||||
return nothing {} if this._left == 0;
|
||||
this._left--;
|
||||
return this._from.next();
|
||||
}
|
||||
|
||||
#! How many elements it has left: the fewer of the count and what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def taken<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
val held := this._from.remaining();
|
||||
return held < this._left ? held : this._left;
|
||||
}
|
||||
|
||||
#! What `skip` makes: the elements after the first, up to a count.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
pub struct skipped<T, I> : iterator<T> where I: iterator<T> {
|
||||
var _from: I;
|
||||
var _skip: u64;
|
||||
}
|
||||
|
||||
#! Gives the next element, passing over those to skip the first time.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def skipped<T, I>::next(&mut this): maybe<T> {
|
||||
if this._skip != 0 {
|
||||
val n := this._skip;
|
||||
this._skip = 0;
|
||||
return this._from.nth(n);
|
||||
}
|
||||
return this._from.next();
|
||||
}
|
||||
|
||||
#! How many elements it has left: what it adapts has, past those to skip.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def skipped<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
val held := this._from.remaining();
|
||||
return held > this._skip ? held - this._skip : 0;
|
||||
}
|
||||
|
||||
#! Gives the element at the far end, while it is past those to skip.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def skipped<T, I>::next_back(&mut this): maybe<T> where I: backward_iterator<T>, sized_iterator<T> {
|
||||
return nothing {} if this._from.remaining() <= this._skip;
|
||||
return this._from.next_back();
|
||||
}
|
||||
|
||||
#! What `take_while` makes: the elements while a test passes them.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The test.
|
||||
pub struct taken_while<T, I, F> : iterator<T> where I: iterator<T> where F: invokable<def(&T): bool> {
|
||||
var _from: I;
|
||||
var _test: F;
|
||||
var _done: bool;
|
||||
}
|
||||
|
||||
#! Gives the next element where the test passes it, and ends at the first it
|
||||
#! does not.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def taken_while<T, I, F>::next(&mut this): maybe<T> {
|
||||
return nothing {} if this._done;
|
||||
if this._from.next() is some(x) {
|
||||
return move(&mut x) if this._test(&x);
|
||||
}
|
||||
this._done = true;
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `skip_while` makes: the elements from the first a test does not pass.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The test.
|
||||
pub struct skipped_while<T, I, F> : iterator<T> where I: iterator<T> where F: invokable<def(&T): bool> {
|
||||
var _from: I;
|
||||
var _test: F;
|
||||
var _past: bool;
|
||||
}
|
||||
|
||||
#! Gives the next element, passing over those the test passes the first time.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def skipped_while<T, I, F>::next(&mut this): maybe<T> {
|
||||
return this._from.next() if this._past;
|
||||
this._past = true;
|
||||
while this._from.next() is some(x) {
|
||||
return move(&mut x) if !this._test(&x);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `step` makes: every `n`th element, the first included.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
pub struct stepped<T, I> : iterator<T> where I: iterator<T> {
|
||||
var _from: I;
|
||||
var _by: u64;
|
||||
var _first: bool;
|
||||
}
|
||||
|
||||
#! Gives the next element, passing over those between.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def stepped<T, I>::next(&mut this): maybe<T> {
|
||||
if this._first {
|
||||
this._first = false;
|
||||
return this._from.next();
|
||||
}
|
||||
return this._from.nth(this._by - 1);
|
||||
}
|
||||
|
||||
#! How many elements it has left: every `n`th of what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def stepped<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
val held := this._from.remaining();
|
||||
return this._first ? (held + this._by - 1) / this._by : held / this._by;
|
||||
}
|
||||
|
||||
#! What `rev` makes: the elements from the far end.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts, which walks backward.
|
||||
pub struct reversed<T, I> : iterator<T> where I: backward_iterator<T> {
|
||||
var _from: I;
|
||||
}
|
||||
|
||||
#! Gives the element at the far end of what it adapts.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def reversed<T, I>::next(&mut this): maybe<T> {
|
||||
return this._from.next_back();
|
||||
}
|
||||
|
||||
#! Gives the element at the front of what it adapts, its own far end.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def reversed<T, I>::next_back(&mut this): maybe<T> {
|
||||
return this._from.next();
|
||||
}
|
||||
|
||||
#! How many elements it has left, which are what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def reversed<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
return this._from.remaining();
|
||||
}
|
||||
|
||||
#! What `peekable` makes: the elements, with the next looked at by `peek`
|
||||
#! before it is given.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
pub struct peeking<T, I> : iterator<T> where I: iterator<T> {
|
||||
var _from: I;
|
||||
var _held: maybe<T>;
|
||||
var _peeked: bool;
|
||||
}
|
||||
|
||||
#! Gives the next element, the one `peek` looked at where it did.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def peeking<T, I>::next(&mut this): maybe<T> {
|
||||
if this._peeked {
|
||||
this._peeked = false;
|
||||
return move(&mut this._held);
|
||||
}
|
||||
return this._from.next();
|
||||
}
|
||||
|
||||
#! Looks at the next element without giving it, so `next` gives it after.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe a reference to the element, which it holds; otherwise,
|
||||
#! `nothing`.
|
||||
pub def peeking<T, I>::peek(&mut this): maybe<&T> {
|
||||
if !this._peeked {
|
||||
this._held = this._from.next();
|
||||
this._peeked = true;
|
||||
}
|
||||
if this._held is some(&x) {
|
||||
return x;
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! How many elements it has left: what it adapts has, and the one it holds.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def peeking<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
var made := this._from.remaining();
|
||||
if this._peeked && this._held is some(&x) {
|
||||
made++;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! What `flat_map` makes: the elements of each iterator a function makes of
|
||||
#! each element, in turn.
|
||||
#! @tparam T What the iterator it adapts gives.
|
||||
#! @tparam U What the iterators the function makes give.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam J The iterators the function makes.
|
||||
#! @tparam F The function.
|
||||
pub struct flat_mapped<T, U, I, J, F> : iterator<U>
|
||||
where I: iterator<T> where J: iterator<U> where F: invokable<def(T): J> {
|
||||
var _from: I;
|
||||
var _f: F;
|
||||
var _inner: maybe<J>;
|
||||
}
|
||||
|
||||
#! Gives the next element of the iterator it is walking, or of the next it
|
||||
#! makes, once that one ends.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def flat_mapped<T, U, I, J, F>::next(&mut this): maybe<U> {
|
||||
while true {
|
||||
if this._inner is some(&mut inner) {
|
||||
if inner.next() is some(x) {
|
||||
return move(&mut x);
|
||||
}
|
||||
}
|
||||
if this._from.next() is not some(y) {
|
||||
return nothing {};
|
||||
}
|
||||
this._inner = this._f(move(&mut y));
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `copied` makes: a copy of what each reference refers to.
|
||||
#! @tparam X What the references refer to.
|
||||
#! @tparam I The iterator of references it adapts.
|
||||
pub struct copying<X, I> : iterator<X> where I: iterator<&X> {
|
||||
var _from: I;
|
||||
}
|
||||
|
||||
#! Gives a copy of what the next reference refers to.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the copy; otherwise, `nothing`.
|
||||
pub def copying<X, I>::next(&mut this): maybe<X> {
|
||||
if this._from.next() is some(r) {
|
||||
return copy(r);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! How many elements it has left, which are what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def copying<X, I>::remaining(&this): u64 where I: sized_iterator<&X> {
|
||||
return this._from.remaining();
|
||||
}
|
||||
|
||||
#! Gives a copy of what the reference at the far end refers to.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the copy; otherwise, `nothing`.
|
||||
pub def copying<X, I>::next_back(&mut this): maybe<X> where I: backward_iterator<&X> {
|
||||
if this._from.next_back() is some(r) {
|
||||
return copy(r);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `inspect` makes: the elements as they are, after a function is called on
|
||||
#! each.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The function.
|
||||
pub struct inspected<T, I, F> : iterator<T> where I: iterator<T> where F: invokable<def(&T): void> {
|
||||
var _from: I;
|
||||
var _f: F;
|
||||
}
|
||||
|
||||
#! Gives the next element, once the function has seen it.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def inspected<T, I, F>::next(&mut this): maybe<T> {
|
||||
if this._from.next() is some(x) {
|
||||
this._f(&x);
|
||||
return move(&mut x);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! How many elements it has left, which are what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def inspected<T, I, F>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
return this._from.remaining();
|
||||
}
|
||||
|
||||
#! Gives the element at the far end, once the function has seen it.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def inspected<T, I, F>::next_back(&mut this): maybe<T> where I: backward_iterator<T> {
|
||||
if this._from.next_back() is some(x) {
|
||||
this._f(&x);
|
||||
return move(&mut x);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
#! @file range.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the methods of `range`, the integers from a start up to an end, or
|
||||
#! the members of an enum between two, which `a..b` and `a..=b` make wherever
|
||||
#! they are written as a value.
|
||||
#!
|
||||
#! `range` is among the types the syntax makes, which the compiler declares
|
||||
#! itself. It is an iterator, walked forward or from its far end, that knows how
|
||||
#! many it has left, so `(0..n).rev()` and `(0..n).step(2)` count as a loop
|
||||
#! would. A `for` loop over `a..b` written in place counts without making one.
|
||||
#!
|
||||
#! A range of an enum's members gives them in the order of their values, each
|
||||
#! value once, passing over those no member has, as `color::red..=color::blue`
|
||||
#! gives `red`, `green`, and `blue` of `enum color { red, green = 4, blue }`.
|
||||
|
||||
module core;
|
||||
|
||||
|
||||
#! The place of a member of an enum among the values of its members, from 0 for
|
||||
#! the least, each value counted once.
|
||||
#! @tparam T The enum.
|
||||
#! @param member The member.
|
||||
#! @returns Its place.
|
||||
@intrinsic
|
||||
def rank_of<T>(member: T): u64;
|
||||
|
||||
#! The member of an enum at a place among the values of its members, as
|
||||
#! `rank_of` gives it.
|
||||
#! @tparam T The enum.
|
||||
#! @param rank The place, before the count of the members' values.
|
||||
#! @returns The member.
|
||||
@intrinsic
|
||||
def member_at<T>(rank: u64): T;
|
||||
|
||||
#! The value a number of places past another, as a range counts: the integer
|
||||
#! that far past it, or the member of an enum that many members past it.
|
||||
#! @tparam T The integer type, or the enum.
|
||||
#! @param from The value to count from.
|
||||
#! @param n How many places past it.
|
||||
#! @returns The value.
|
||||
def range<T>::past(from: T, n: u64): T {
|
||||
if const T is integer {
|
||||
return ((from as u64) + n) as T;
|
||||
}
|
||||
return member_at::<T>(rank_of(from) + n);
|
||||
}
|
||||
|
||||
#! How many places a range counts from one value up to another, which does not
|
||||
#! come before it.
|
||||
#! @tparam T The integer type, or the enum.
|
||||
#! @param from The first value.
|
||||
#! @param to The value it counts up to.
|
||||
#! @returns How many.
|
||||
def range<T>::between(from: T, to: T): u64 {
|
||||
if const T is integer {
|
||||
return (to as u64) - (from as u64);
|
||||
}
|
||||
return rank_of(to) - rank_of(from);
|
||||
}
|
||||
|
||||
|
||||
#! Gives the next value, counting up from the start.
|
||||
#! @param this The range.
|
||||
#! @returns Maybe the value; otherwise, `nothing` once it has given them all.
|
||||
pub def range<T>::next(&mut this): maybe<T> {
|
||||
if this.start < this.end {
|
||||
val at := this.start;
|
||||
if const T is integer {
|
||||
this.start++;
|
||||
} else {
|
||||
this.start = range::<T>::past(at, 1);
|
||||
}
|
||||
return at;
|
||||
}
|
||||
if this._last && this.start == this.end {
|
||||
this._last = false;
|
||||
return this.end;
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! Gives the value at the far end, counting down from the end.
|
||||
#! @param this The range.
|
||||
#! @returns Maybe the value; otherwise, `nothing` once the ends have met.
|
||||
pub def range<T>::next_back(&mut this): maybe<T> {
|
||||
if this._last && !(this.end < this.start) {
|
||||
this._last = false;
|
||||
return this.end;
|
||||
}
|
||||
if this.start < this.end {
|
||||
if const T is integer {
|
||||
this.end--;
|
||||
} else {
|
||||
this.end = member_at::<T>(rank_of(this.end) - 1);
|
||||
}
|
||||
return this.end;
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! How many values it has left to give.
|
||||
#! @param this The range.
|
||||
#! @returns How many.
|
||||
pub def range<T>::remaining(&this): u64 {
|
||||
var made: u64 = this.start < this.end ? range::<T>::between(this.start, this.end) : 0;
|
||||
if this._last && !(this.end < this.start) {
|
||||
made++;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! How many values it has left to give, as `remaining` says.
|
||||
#! @param this The range.
|
||||
#! @returns How many.
|
||||
pub val range<T>::length: u64 {
|
||||
get => this.remaining();
|
||||
}
|
||||
|
||||
#! Whether it has given every value, or had none to give.
|
||||
#! @param this The range.
|
||||
#! @returns Whether it has none left.
|
||||
pub def range<T>::is_empty(&this): bool {
|
||||
return !(this.start < this.end) && !(this._last && this.start == this.end);
|
||||
}
|
||||
|
||||
#! Whether a value is among those it has left to give.
|
||||
#! @param this The range.
|
||||
#! @param x The value.
|
||||
#! @returns Whether it is.
|
||||
pub def range<T>::contains(&this, x: T): bool {
|
||||
return !(x < this.start) && (x < this.end || (this._last && x == this.end));
|
||||
}
|
||||
|
||||
#! The value `n` along, counting from 0, passing over those before it at once.
|
||||
#! @param this The range.
|
||||
#! @param n How many to pass over first.
|
||||
#! @returns Maybe the value; otherwise, `nothing` where it has fewer left.
|
||||
pub def range<T>::nth(&mut this, n: u64): maybe<T> {
|
||||
val held: u64 = this.start < this.end ? range::<T>::between(this.start, this.end) : 0;
|
||||
if n < held {
|
||||
this.start = range::<T>::past(this.start, n);
|
||||
return this.next();
|
||||
}
|
||||
if this.start < this.end {
|
||||
this.start = this.end;
|
||||
}
|
||||
return this.next() if n == held;
|
||||
this._last = false;
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! Gives every value it has left at once, counting them.
|
||||
#! @param this The range.
|
||||
#! @returns How many it had left.
|
||||
pub def range<T>::count(&mut this): u64 {
|
||||
val made := this.remaining();
|
||||
if this.start < this.end {
|
||||
this.start = this.end;
|
||||
}
|
||||
this._last = false;
|
||||
return made;
|
||||
}
|
||||
@@ -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.view().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::empty(), _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 : iterator<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 : iterator<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 : iterator<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