Library / Strings / Text / StringView
Non-owning, bounded slice over a code-point array.
Constructors 1
INIT(REFERENCE char32[] source, int64 start, int64 length)
Construct a bounded view over [start, start+length) in source.
Throws (via the C-string helper) if start < 0 or length < 0 or
start + length > source.length(). A zero length is allowed.
Methods 18
METHOD length() RETURNS int64reads
Code-point count of this view.
METHOD size() RETURNS int64reads
Countable — View EXTENDS Countable, so the view owes size() and
isEmpty() as well as its own length(). All three are the same
number here: the count of code points the window spans.
METHOD isEmpty() RETURNS booleanreads
METHOD __op_index__(int64 i) RETURNS char32reads
Code point at index i within this view. sv[i] is the only read
syntax. Throws (C-string helper) if i is out of [0, view.length).
METHOD find(REFERENCE char32[] needle) RETURNS | int64reads
First occurrence of needle (itself a view) within this view's whole
code-point range. Pipe-XOR: SUCCESS yields the view-relative index of
the match; FAILURE means no match. Case-sensitive — to search a
sub-range, narrow with view(start, length) first.
METHOD find(REFERENCE char32[] needle, boolean caseMatch) RETURNS | int64reads
As find, with ASCII case-fold when caseMatch == FALSE.
METHOD startsWith(REFERENCE char32[] needle) RETURNS booleanreads
True iff this view begins with needle (case-sensitive). An empty
needle returns TRUE; a needle longer than the view returns FALSE.
METHOD endsWith(REFERENCE char32[] needle) RETURNS booleanreads
True iff this view ends with needle (case-sensitive). An empty
needle returns TRUE; a needle longer than the view returns FALSE.
METHOD lastIndexOf(REFERENCE char32[] needle) RETURNS | int64reads
Last occurrence of needle within this view (case-sensitive). Pipe-XOR:
SUCCESS yields the view-relative index of the final match; FAILURE means
no match. An empty needle yields the view length. Scans backward from the
last candidate window, so the first hit found is the last occurrence.
METHOD contains(REFERENCE char32[] needle) RETURNS booleanreads
True iff needle occurs at least once in this view (case-sensitive).
METHOD contains(REFERENCE char32[] needle, boolean caseMatch) RETURNS booleanreads
METHOD count(REFERENCE char32[] needle) RETURNS int64reads
Non-overlapping occurrence count of needle in this view
(case-sensitive). Empty needle returns 0.
METHOD count(REFERENCE char32[] needle, boolean caseMatch) RETURNS int64reads
METHOD find_first_of(REFERENCE char32[] set) RETURNS | int64reads
B1: index of the first code point that IS a member of set. Pipe-XOR:
SUCCESS = the index; FAILURE = no code point in this view is in set.
METHOD find_first_not_of(REFERENCE char32[] set) RETURNS | int64reads
B1: index of the first code point that is NOT a member of set.
METHOD view(int64 start, int64 length) RETURNS StringViewreads
Narrow to a sub-window [start, start+length) relative to THIS view
(composition — a sub-window of a window is a window). Bounds are
checked against this view's length, so a sub-view can never escape
its parent. Satisfies Viewable.
METHOD copy() RETURNS char32[]reads
Materialise this window's code points into a fresh char32[] (the "keep this slice" escape hatch — the view itself never escapes). Wrap with CREATE String(view->copy()) for an owned String. DEFECT FIXED. This used .source->copyTo(piece, length), which copies from the SOURCE'S index 0 and ignores .start — so any view whose window did not begin at 0 returned the wrong code points. _ValueArray::copyTo(other, n) takes no source offset, so it cannot express a windowed copy at all; the element loop below (which was here, commented out) is the only correct form. ByteBufferView.copy() never had the bug — it always used this shape.
It stayed latent because every caller passed a FULL-SPAN view
(view(0, length())), where .start is 0 and the two agree. The first
caller with a non-zero start was DynamicString.substring, added in
Phase 2, which is what surfaced it.
METHOD iterator() RETURNS ValueIterator[char32]reads
Walk the view's code points in order. The iterator snapshots the
bounded slice into its own storage at construction, so it is
independent of this view and its source char32[] for its entire
lifetime (matching ByteBufferIterator's design). The snapshot is
materialised inline because the emit does not currently auto-deref a
REFERENCE self-field when passed to a value-typed ctor param.