(std text)

Reference

Overview

Persistent text for editing: a B+ tree of UTF-8 chunks, with builder and transient.

(import (std text))

(def r (string->text "hello world\nsecond line"))
(def c (text-cursor-forward r (text-cursor-start r) 5))
(def r2 (text-insert r c ","))                   ; "hello, world\nsecond line"
(text->string (text-line r2 1))                  ; "second line"
(def w (option-ref (text-search r2 "world")))
(text-cursor-line r2 w)                          ; 0
(text->string (text-substring r2 w (text-cursor-end r2)))   ; "world\nsecond line"
(text->string r)                                 ; r is unchanged

A Text is immutable text that is cheap to edit at any position. Inserting, deleting, splitting, appending and taking substrings take O(log n) time and share every chunk and node they do not change, so old versions stay valid and cost only what differs.

As a string. The API is a string’s: the functions a string has are here under the same names, with a text where the string was, and return what the string’s return. text-length is in bytes, as string-length is, and text-count counts characters, in O(1) time. Positions are cursors, as in a string, and no function takes or returns a byte offset.

Cursors. A TextCursor is a position that holds the chunk it is in, so stepping it reads that chunk’s bytes and does not search the tree again. The cursor functions take the text and the cursor, as the string cursor functions take the string. Every node keeps the characters of its children, so text-cursor-forward and text-cursor-back move any number of characters in O(log n) time: character n of a text is (text-cursor-forward r (text-cursor-start r) n). A cursor starts a loop: (:for c cursor) goes to the end, and (:for c (text-backward r cursor)) goes back to the start.

Edits. text-insert, text-delete, text-replace-range and text-split-at take cursors of the text they edit. A cursor belongs to one text: the text an edit returns needs cursors of its own.

Lines. Every node keeps the newlines of its children, so text-cursor-at-line and text-cursor-line take O(log n) time. Lines are separated by "\n" and counted from 0; a text has one line more than it has newlines.

Graphemes. text-cursor-next-grapheme and text-cursor-prev-grapheme step by grapheme clusters, the characters a reader sees, by the rules of (std unicode grapheme), as an editor’s arrow keys and backspace do.

Structure. Text is held in chunks of at most 1024 bytes, in nodes of up to 16 children, each with the bytes, newlines and characters of every child. A text of at most 1024 bytes is a single string and no tree. Only the nodes are C# (Bjolang.Runtime.TextTree), because they hold their slots in inline arrays; every operation is Bjolang.

Batch edits. A TransientText edits nodes it owns in place and keeps the chunk being edited in a mutable buffer: a run of edits close together, such as typing, allocates nothing. Its positions are character indices. A TextBuilder appends strings and builds the tree in one pass, and the texting collector does that in a loop.

Traits. Implements Eq, Ord, Iterable (the characters), Collection (whose length is the character count), Refable and Cursor (by TextCursor), Written, Displayed and Addable (of strings, at the end). Equality, order and hashing depend on the characters only, not on how they are split into chunks. TextBuilder and TransientText implement Addable!.

See also: Text, TextCursor, text-insert, text-delete, text-substring, text-split-at, text-append, text-cursor-forward, text-cursor-at-line, TransientText, TextBuilder, Texting, text-replace, text-split

Reference

Types

Functions

Types

Text

record ↑ Types

Immutable text as a balanced tree of UTF-8 chunks.

(def r (text "abc" "déf"))
(text-count r)                   ; 6
(text-length r)                  ; 7, in bytes
(= r (string->text "abcdéf"))    ; #t
(loop (:for c r) (:do (display c)))

See also: string->text, text->string, text-insert, text-delete, TransientText

TextWalk

record ↑ Types

The cursor of a loop over a text’s characters.

A struct holding the cursor of the current chunk; a loop with (:for c r) uses it and allocates nothing.

See also: Text

TextCursor

record ↑ Types

A position in a text, on a character boundary, that steps by characters.

(def r (string->text "one two\nthree"))
(def c (option-ref (text-search r "two")))
(text-cursor-ref r c)                            ; #\t
(text-cursor-line r (text-cursor-forward r c 4)) ; 1
(loop (:for ch c) (:acc (stringing ch)))         ; "two\nthree"

A struct of the text, the chunk the position is in, and the position in it. The functions on it take the text first, as the string cursor functions take the string; the cursor must be one of that text. It stays valid for as long as the text does: texts do not change.

A step inside the chunk reads its bytes and allocates nothing; a step out of it looks up the next chunk from the root, once per chunk. A move of more than a few characters looks its character up from the root by the character counts.

= and compare compare the positions. So do text-cursor<? and the rest, since < is for numbers. Cursors of two texts compare without complaint and mean nothing.

As an Iterable, a cursor is the characters from it to the end.

See also: text-cursor-start, text-cursor-forward, text-cursor-next, text-cursor-prev, text-cursor-search, text-backward

TextBackward

record ↑ Types

The characters before a cursor, last first, as a loop goes over them.

See also: text-backward, TextCursor

TextBackwardStep

record ↑ Types

The step of a loop over a TextBackward.

A struct of the cursor on the current character. Exported because the loop that uses it is compiled where it is written.

See also: TextBackward

TextBuilder

record ↑ Types

Mutable builder that appends text and returns a text.

(def b (textbuilder-empty))
(textbuilder-add-string! b "first line\n")
(textbuilder-add-string! b "second line\n")
(textbuilder->text b)

See also: textbuilder-empty, textbuilder-add-string!, textbuilder->text

Texting

union ↑ Types

The collector that gathers characters or strings into a text.

texting
Written in an :acc clause with the character or string to add.
(loop (:for line lines)
      (:acc t (texting #"${line}\n"))
      => t)                                     ; a text of the lines
(loop (:for w words) (:acc (texting w #:into t)))   ; t with the words after it
(loop (:for c t) (:acc (texting (char-upcase c))))  ; a map over a text

The pieces are of one type, as TextPiece gives: a loop adds characters, as stringing does, or strings. Its accumulator is a TextBuilder, so the text is built in one pass and long strings are shared, not copied. #:into starts from a text: its chunks are added first, and it is not changed.

See also: TextBuilder, TextingInto, TextPiece, Stringing

TextPiece

trait ↑ Types

What a text builder takes a piece of text as: a string or a character.

See also: Texting, textbuilder-add-piece!

TextingInto

record ↑ Types

The collector a texting clause with #:into makes: it starts from a text.

seed
The text to start from, which is not changed.

See also: Texting

TransientText

record ↑ Types

Mutable text for batch edits in place, at character indices.

(def t (text->transienttext r))
(transienttext-insert! t 0 "# ")
(transienttext-delete! t 10 12)    ; characters 10 and 11
(transienttext->text t)

Positions are character indices, from 0 to transienttext-count: a cursor would not survive the edits.

Edits copy each node they do not own once, and then edit it in place. The chunk of the last edit is kept in a mutable buffer: an edit inside it, or within 64 characters of the edit before, moves bytes in the buffer and allocates nothing. An edit finds its byte in the buffer by stepping from where the last one ended, or at once in a chunk of ASCII. Not safe to use from two threads at once.

See also: text->transienttext, transienttext->text, transienttext-insert!, transienttext-delete!

Functions

textbuilder-add-piece!

function ↑ Functions

(: textbuilder-add-piece! (-> TextBuilder %a void))

Adds a string or a character to the end of a builder.

b
The builder.
x
The string or character.

See also: textbuilder-add-string!, textbuilder-add!, Texting

text-empty

function ↑ Functions

(: text-empty (-> Text))
(text-empty)

The empty text.

returns
A text of no text.

See also: text, string->text

text

function ↑ Functions

(: text (-> #:rest string Text))
(text #:rest parts)

A text of strings joined in order.

parts ...
The strings.
returns
A text of the strings, one after the other.
(text->string (text "ab" "" "cd"))   ; "abcd"

See also: string->text, textbuilder-empty

string->text

function ↑ Functions

(: string->text (-> string Text))
(string->text s)

A text of a string’s text.

s
The text. Chunks of a long string share its bytes.
returns
A text of the text of s.

See also: text->string, text

text->string

function ↑ Functions

(: text->string (-> Text string))
(text->string r)

The text of a text as one string.

r
The text.
returns
A string of the text of r, made in one allocation of its size.

See also: string->text, text-substring

text-length

function ↑ Functions

(: text-length (-> Text int))
(text-length r)

The length of a text in bytes of UTF-8, as string-length is.

r
The text.
returns
The number of bytes of r's UTF-8 text, in O(1) time.

See also: text-count, text-line-count

text-count

function ↑ Functions

(: text-count (-> Text int))
(text-count r)

The number of characters in a text.

r
The text.
returns
How many characters r holds, in O(1) time: every node keeps the characters of its children.

See also: text-length

text-empty?

function ↑ Functions

(: text-empty? (-> Text bool))
(text-empty? r)

Whether a text has no characters.

r
The text.
returns
#t for the empty text.

See also: text-length, text-count

text-line-count

function ↑ Functions

(: text-line-count (-> Text int))
(text-line-count r)

The number of lines in a text.

r
The text.
returns
One more than the number of newlines in r, in O(1) time. The empty text has one line.

See also: text-line, text-cursor-at-line

text-substring

function ↑ Functions

(: text-substring (-> Text TextCursor TextCursor Text))
(text-substring r a b)

The characters between two cursors, as a text.

r
The text (unmodified).
a
Where it begins, a cursor of r.
b
Where it ends, not included: a cursor of r, not before a.
returns
A text of the characters from a to b, in O(log n) time.
raises System.ArgumentOutOfRangeException
When b is before a.

A substring of up to 1024 bytes is a string of its own. A longer one shares r’s chunks: those in at most 16 chunks are pieces of them in one node, and the rest are cut from r’s tree. text->string gives it as a string.

See also: text-split-at, text-take, text-drop, text->string

text-split-at

function ↑ Functions

(: text-split-at (-> Text TextCursor (Tuple Text Text)))
(text-split-at r c)

A text split in two at a cursor.

r
The text (unmodified).
c
The cursor to split at, a cursor of r.
returns
(Tuple before after): the texts of the characters before c and from c, in O(log n) time.
(def r (string->text "hello world"))
(def (Tuple a b) (text-split-at r (text-cursor-forward r (text-cursor-start r) 5)))
(text->string b)   ; " world"

See also: text-append, text-substring, text-split

text-append

function ↑ Functions

(: text-append (-> Text Text Text))
(text-append a b)

Two texts joined, as string-append joins two strings.

a
The first text (unmodified).
b
The second text (unmodified).
returns
A text of the text of a and then b, sharing their nodes, in O(log n) time.

See also: text-split-at, text-insert-text, text-join

text-insert

function ↑ Functions

(: text-insert (-> Text TextCursor string Text))
(text-insert r c s)

A text with a string inserted at a cursor.

r
The text (unmodified).
c
Where to insert, a cursor of r.
s
The string to insert.
returns
A text of r's text with s before the character at c, in O(log n) time. Copies the nodes on the path and one chunk.

See also: text-insert-text, text-delete, text-replace-range, transienttext-insert!

text-insert-text

function ↑ Functions

(: text-insert-text (-> Text TextCursor Text Text))
(text-insert-text r c x)

A text with another text inserted at a cursor.

r
The text (unmodified).
c
Where to insert, a cursor of r.
x
The text to insert (unmodified).
returns
A text of r's text with x's before the character at c, sharing x's nodes.

See also: text-insert, text-append

text-delete

function ↑ Functions

(: text-delete (-> Text TextCursor TextCursor Text))
(text-delete r a b)

A text with the characters between two cursors removed.

r
The text (unmodified).
a
The first character to remove, a cursor of r.
b
The cursor after the last to remove, not before a.
returns
A text of r's text without the characters from a to b, in O(log n) time.
raises System.ArgumentOutOfRangeException
When b is before a.

See also: text-insert, text-replace-range, transienttext-delete!

text-replace-range

function ↑ Functions

(: text-replace-range (-> Text TextCursor TextCursor string Text))
(text-replace-range r a b s)

A text with the characters between two cursors replaced by a string.

r
The text (unmodified).
a
The first character to replace, a cursor of r.
b
The cursor after the last to replace, not before a.
s
The string to put in their place.
returns
A text of r's text with the characters from a to b replaced by s.
raises System.ArgumentOutOfRangeException
When b is before a.

See also: text-insert, text-delete, text-replace, transienttext-replace-range!

text-take

function ↑ Functions

(: text-take (-> Text int Text))
(text-take r n)

The first characters of a text.

r
The text (unmodified).
n
How many characters.
returns
A text of the first n characters of r, or r when it has fewer. Shares r's chunks, in O(log n) time.
raises System.ArgumentException
When n is negative.

See also: text-drop, text-take-right, text-split-at

text-drop

function ↑ Functions

(: text-drop (-> Text int Text))
(text-drop r n)

A text without its first characters.

r
The text (unmodified).
n
How many characters to leave out.
returns
A text of r after its first n characters, or the empty text when it has fewer.
raises System.ArgumentException
When n is negative.

See also: text-take, text-drop-right, text-split-at

text-take-right

function ↑ Functions

(: text-take-right (-> Text int Text))
(text-take-right r n)

The last characters of a text.

r
The text (unmodified).
n
How many characters.
returns
A text of the last n characters of r, or r when it has fewer.
raises System.ArgumentException
When n is negative.

See also: text-drop-right, text-take

text-drop-right

function ↑ Functions

(: text-drop-right (-> Text int Text))
(text-drop-right r n)

A text without its last characters.

r
The text (unmodified).
n
How many characters to leave out.
returns
A text of r before its last n characters, or the empty text when it has fewer.
raises System.ArgumentException
When n is negative.

See also: text-take-right, text-drop

text-contains?

function ↑ Functions

(: text-contains? (-> Text string bool))
(text-contains? r s)

Whether a text contains a string.

r
The text.
s
The string to find.
returns
#t when s occurs in r. Finds matches that span chunks.

See also: text-search, text-starts-with?

text-starts-with?

function ↑ Functions

(: text-starts-with? (-> Text string bool))
(text-starts-with? r s)

Whether a text starts with a string.

r
The text.
s
The string.
returns
#t when r's first characters are those of s.

See also: text-ends-with?, text-search

text-ends-with?

function ↑ Functions

(: text-ends-with? (-> Text string bool))
(text-ends-with? r s)

Whether a text ends with a string.

r
The text.
s
The string.
returns
#t when r's last characters are those of s.

See also: text-starts-with?

text-replace

function ↑ Functions

(: text-replace (-> Text string string Text))
(text-replace r old new)

A text with every occurrence of one string replaced by another.

r
The text (unmodified).
old
The string to replace.
new
The string to put in its place.
returns
A text of r with each old, from the start and not overlapping, replaced by new. r itself when there is none.
raises System.ArgumentException
When old is empty.
(text->string (text-replace (string->text "a-b-c") "-" ", "))   ; "a, b, c"

Searches through one iteration of r’s chunks, and adds the text between the matches from another, so the result is built in one pass and shares the arrays of the chunks it keeps whole.

See also: text-replace-range, text-search, text-split

text-split

function ↑ Functions

(: text-split (-> Text string (Vec Text)))
(text-split r sep)

The fields of a text between occurrences of a separator.

r
The text (unmodified).
sep
The separator.
returns
The fields in order, empty ones kept: one more than the separators in r. An empty separator gives r as the one field.
(vec-length (text-split (string->text "a,,b") ","))   ; 3
(text-split r "\n")                                    ; the lines of r

A field inside one chunk shares the chunk’s bytes; a field across chunks is a string of its own up to 1024 bytes, and shares r’s chunks after.

See also: text-split-once, text-join, text-split-at

text-split-once

function ↑ Functions

(: text-split-once (-> Text string (Option (Tuple Text Text))))
(text-split-once r sep)

A text split around the first occurrence of a separator.

r
The text (unmodified).
sep
The separator.
returns
(Some (Tuple before after)), the texts before and after the first sep, or None when there is none.

See also: text-split, text-split-at

text-join

function ↑ Functions

(: text-join (-> (Vec Text) string Text))
(text-join parts sep)

Texts joined with a separator between them.

parts
The texts, in order (unmodified).
sep
The separator.
returns
A text of the parts with sep between each two, built in one pass.

See also: text-split, text-append, Texting

function ↑ Functions

(: text-search (-> Text string (Option TextCursor)))
(text-search r needle)

Where the first occurrence of a string in a text starts.

r
The text.
needle
The string to look for.
returns
(Some cursor) on the start of the first needle, or None. An empty needle is found at the start.

See also: text-search-backward, text-cursor-search, text-find, text-contains?

text-search-backward

function ↑ Functions

(: text-search-backward (-> Text string (Option TextCursor)))
(text-search-backward r needle)

Where the last occurrence of a string in a text starts.

r
The text.
needle
The string to look for.
returns
(Some cursor) on the start of the last needle, or None. An empty needle is found at the end.

See also: text-search, text-cursor-search-backward

text-search-any

function ↑ Functions

(: text-search-any (-> Text string TextCursor (Option TextCursor)))
(text-search-any r chars start)

Where the first character of a set is, at or after a cursor.

r
The text to search.
chars
The set, as a string of its characters. Their order does not matter.
start
Where to start, a cursor of r.
returns
(Some cursor) on the first character at or after start that is in chars, or None. An empty set is never found.

A set of ASCII characters is searched for in each chunk’s bytes, many at a time.

See also: text-search, text-find, text-cursor-find

text-find

function ↑ Functions

(: text-find (-> (-?-> char bool) Text (Option TextCursor)))
(text-find pred r)

Where the first character satisfying a predicate is.

pred
The test, given each character in turn from the start.
r
The text.
returns
(Some cursor) on the first character pred holds for, or None.

See also: text-cursor-find, text-search-any

text-trim

function ↑ Functions

(: text-trim (-> Text Text))
(text-trim r)

A text without the white space at both ends.

r
The text (unmodified).
returns
A text of r without its leading and trailing white space, as char-whitespace? says. r itself when there is none.

See also: text-trim-left, text-trim-right

text-trim-left

function ↑ Functions

(: text-trim-left (-> Text Text))
(text-trim-left r)

A text without the white space at its start.

r
The text (unmodified).
returns
A text of r from its first character that is not white space.

See also: text-trim, text-trim-right

text-trim-right

function ↑ Functions

(: text-trim-right (-> Text Text))
(text-trim-right r)

A text without the white space at its end.

r
The text (unmodified).
returns
A text of r up to its last character that is not white space.

See also: text-trim, text-trim-left

text-pad-left

function ↑ Functions

(: text-pad-left (-> Text int (#:with char) Text))
(text-pad-left r width #:with #\space)

A text padded at the start to a width in characters.

r
The text (unmodified).
width
The width to pad to.
#:with
The character to pad with, a space by default.
returns
A text of r with copies of with before it until it is width characters long; r when it is that long already.

See also: text-pad-right

text-pad-right

function ↑ Functions

(: text-pad-right (-> Text int (#:with char) Text))
(text-pad-right r width #:with #\space)

A text padded at the end to a width in characters.

r
The text (unmodified).
width
The width to pad to.
#:with
The character to pad with, a space by default.
returns
A text of r with copies of with after it until it is width characters long; r when it is that long already.

See also: text-pad-left

text-upcase

function ↑ Functions

(: text-upcase (-> Text Text))
(text-upcase r)

A text in upper case.

r
The text (unmodified).
returns
A text of r with each character uppercased in the invariant culture.

See also: text-downcase

text-downcase

function ↑ Functions

(: text-downcase (-> Text Text))
(text-downcase r)

A text in lower case.

r
The text (unmodified).
returns
A text of r with each character lowercased in the invariant culture.

See also: text-upcase

text-reverse

function ↑ Functions

(: text-reverse (-> Text Text))
(text-reverse r)

A text with its characters in reverse order.

r
The text (unmodified).
returns
A text of r's characters, last first.

See also: text-backward

text-fold

function ↑ Functions

(: text-fold (-> (-?-> char %a %a) %a Text %a))
(text-fold f init r)

Folds over the characters of a text, first to last.

f
Called with each character and the accumulator, and returns the next accumulator.
init
The first accumulator.
r
The text.
returns
The last accumulator.

See also: text-fold-right, text-for-each, text-fold-chunks

text-fold-right

function ↑ Functions

(: text-fold-right (-> (-?-> char %a %a) %a Text %a))
(text-fold-right f init r)

Folds over the characters of a text, last to first.

f
Called with each character and the accumulator, and returns the next accumulator.
init
The first accumulator.
r
The text.
returns
The last accumulator.

See also: text-fold, text-backward

text-for-each

function ↑ Functions

(: text-for-each (-> (-?-> char void) Text void))
(text-for-each f r)

Calls a function with each character of a text in order.

f
Called with each character.
r
The text.

See also: text-fold

text->list

function ↑ Functions

(: text->list (-> Text (List char)))
(text->list r)

The characters of a text as a list.

r
The text.
returns
A list of r's characters in order.

See also: text->vec

text->vec

function ↑ Functions

(: text->vec (-> Text (Vec char)))
(text->vec r)

The characters of a text as a vec.

r
The text.
returns
A vec of r's characters in order.

See also: text->list

text-line

function ↑ Functions

(: text-line (-> Text int Text))
(text-line r n)

The text of a line.

r
The text (unmodified).
n
The line, counting from 0.
returns
A text of line n, without its newline.
raises System.ArgumentOutOfRangeException
When r has no line n.
(text->string (text-line (string->text "one\ntwo\n") 1))   ; "two"
(text->string (text-line (string->text "one\ntwo\n") 2))   ; ""

See also: text-cursor-at-line, text-line-count, text-substring

text-cursor-start

function ↑ Functions

(: text-cursor-start (-> Text TextCursor))
(text-cursor-start r)

A cursor on the first character of a text.

r
The text.
returns
The cursor on its first character. For the empty text it is also the end.
(let go ((c (text-cursor-start r)))
  (unless (text-cursor-end? r c)
    (println (char->string (text-cursor-ref r c)))
    (go (text-cursor-next r c))))

See also: text-cursor-end, text-cursor-next, text-cursor-forward

text-cursor-end

function ↑ Functions

(: text-cursor-end (-> Text TextCursor))
(text-cursor-end r)

The past-the-end cursor of a text.

r
The text.
returns
The cursor after the last character.

See also: text-cursor-end?, text-cursor-prev, text-backward

text-cursor-at-line

function ↑ Functions

(: text-cursor-at-line (-> Text int (#:column int) TextCursor))
(text-cursor-at-line r n #:column 0)

A cursor on a line, a number of characters in.

r
The text.
n
The line, counting from 0.
#:column
How many characters into the line. Default 0.
returns
The cursor column characters after the start of line n, or at the end of the line when it is shorter.
raises System.ArgumentOutOfRangeException
When r has no line n.
;; One line down, in the same column, as an editor moves.
(text-cursor-at-line r (+ (text-cursor-line r c) 1) #:column (text-cursor-column r c))

Finds the line in O(log n) time, then steps through the column.

See also: text-cursor-line, text-cursor-column, text-line

text-cursor-start?

function ↑ Functions

(: text-cursor-start? (-> Text TextCursor bool))
(text-cursor-start? r c)

Whether a cursor is at the start of its text.

r
The text.
c
A cursor of r.
returns
#t when there is no character before c.

See also: text-cursor-end?, text-cursor-prev

text-cursor-end?

function ↑ Functions

(: text-cursor-end? (-> Text TextCursor bool))
(text-cursor-end? r c)

Whether a cursor has reached the end of its text.

r
The text.
c
A cursor of r.
returns
#t when c is the past-the-end cursor, and there is no character at it.

See also: text-cursor-start?, text-cursor-ref

text-cursor-ref

function ↑ Functions

(: text-cursor-ref (-> Text TextCursor char))
(text-cursor-ref r c)

The character at a cursor.

r
The text.
c
A cursor of r, not at the end.
returns
The character c is on.
raises System.ArgumentOutOfRangeException
When c is at the end of r.

See also: text-cursor-ref+next, text-cursor-end?

text-cursor-next

function ↑ Functions

(: text-cursor-next (-> Text TextCursor TextCursor))
(text-cursor-next r c)

The cursor on the next character.

r
The text.
c
A cursor of r, not at the end.
returns
The cursor after the character at c, which is the end after the last one.
raises System.ArgumentOutOfRangeException
When c is at the end of r.

See also: text-cursor-prev, text-cursor-ref+next, text-cursor-forward

text-cursor-prev

function ↑ Functions

(: text-cursor-prev (-> Text TextCursor TextCursor))
(text-cursor-prev r c)

The cursor on the previous character.

r
The text.
c
A cursor of r, not at the start.
returns
The cursor on the character before c.
raises System.ArgumentOutOfRangeException
When c is at the start of r.

See also: text-cursor-next, text-cursor-back, text-backward

text-cursor-ref+next

function ↑ Functions

(: text-cursor-ref+next (-> Text TextCursor (Tuple char TextCursor)))
(text-cursor-ref+next r c)

The character at a cursor and the cursor after it, in one call.

r
The text.
c
A cursor of r, not at the end.
returns
A tuple of the character at c and the cursor on the next one.
raises System.ArgumentOutOfRangeException
When c is at the end of r.
(def (Tuple ch next) (text-cursor-ref+next r c))

One read of the chunk gives both, so it is the fastest step of a loop that wants the characters.

See also: text-cursor-ref, text-cursor-next

text-cursor-forward

function ↑ Functions

(: text-cursor-forward (-> Text TextCursor int TextCursor))
(text-cursor-forward r c n)

The cursor n characters after another.

r
The text.
c
A cursor of r.
n
How many characters, 0 or more.
returns
The cursor after the n characters from c.
raises System.ArgumentOutOfRangeException
When there are fewer than n characters after c.
raises System.ArgumentException
When n is negative.
(text-cursor-forward r (text-cursor-start r) 1000)   ; character 1000

Up to 16 characters are stepped over. A longer move finds its character from the root by the character counts of the nodes, in O(log n) time, and then in its chunk by counting 32 bytes at a time.

See also: text-cursor-back, text-cursor-next

text-cursor-back

function ↑ Functions

(: text-cursor-back (-> Text TextCursor int TextCursor))
(text-cursor-back r c n)

The cursor n characters before another.

r
The text.
c
A cursor of r.
n
How many characters, 0 or more.
returns
The cursor n characters before c.
raises System.ArgumentOutOfRangeException
When there are fewer than n characters before c.
raises System.ArgumentException
When n is negative.

Steps or looks up as text-cursor-forward does.

See also: text-cursor-forward, text-cursor-prev

text-cursor-index

function ↑ Functions

(: text-cursor-index (-> Text TextCursor int))
(text-cursor-index r c)

The index of the character at a cursor.

r
The text.
c
A cursor of r.
returns
The characters before c, from 0 to text-count, in O(log n) time.
;; A caret kept across an edit, as an index.
(def i (text-cursor-index r caret))
(def r2 (text-insert r (text-cursor-start r) "> "))
(def caret2 (text-cursor-forward r2 (text-cursor-start r2) (+ i 2)))

A cursor belongs to one text; an index is how a position is carried to the text an edit returns. The inverse of moving from the start: (text-cursor-forward r (text-cursor-start r) n) is the cursor of index n.

See also: text-cursor-forward, text-cursor-line, text-count

text-cursor-line

function ↑ Functions

(: text-cursor-line (-> Text TextCursor int))
(text-cursor-line r c)

The line a cursor is on.

r
The text.
c
A cursor of r.
returns
Its line, counting from 0: the newlines before it. O(log n) time.

See also: text-cursor-column, text-cursor-line-start, text-cursor-at-line

text-cursor-column

function ↑ Functions

(: text-cursor-column (-> Text TextCursor int))
(text-cursor-column r c)

How many characters a cursor is into its line.

r
The text.
c
A cursor of r.
returns
The characters between the start of its line and c, in O(log n) time.

See also: text-cursor-line, text-cursor-at-line

text-cursor-line-start

function ↑ Functions

(: text-cursor-line-start (-> Text TextCursor TextCursor))
(text-cursor-line-start r c)

The cursor at the start of a cursor’s line.

r
The text.
c
A cursor of r.
returns
The cursor on the first character of its line.

See also: text-cursor-line-end, text-cursor-at-line

text-cursor-line-end

function ↑ Functions

(: text-cursor-line-end (-> Text TextCursor TextCursor))
(text-cursor-line-end r c)

The cursor at the end of a cursor’s line.

r
The text.
c
A cursor of r.
returns
The cursor on the newline that ends its line, or the end cursor on the last line.

See also: text-cursor-line-start

function ↑ Functions

(: text-cursor-search (-> Text TextCursor string (Option TextCursor)))
(text-cursor-search r c s)

Where the first occurrence of a string at or after a cursor starts.

r
The text.
c
The cursor of r to search from.
s
The string to find.
returns
(Some cursor) on the start of the first occurrence that starts at or after c, or None. An empty s is found at c.

See also: text-cursor-search-backward, text-search, text-cursor-find

text-cursor-search-backward

function ↑ Functions

(: text-cursor-search-backward (-> Text TextCursor string (Option TextCursor)))
(text-cursor-search-backward r c s)

Where the last occurrence of a string before a cursor starts.

r
The text.
c
The cursor of r to search back from.
s
The string to find.
returns
(Some cursor) on the start of the last occurrence that ends at or before c, or None. An empty s is found at c.

See also: text-cursor-search, text-search-backward

text-cursor-find

function ↑ Functions

(: text-cursor-find (-> (-?-> char bool) Text TextCursor (Option TextCursor)))
(text-cursor-find pred r c)

The first character at or after a cursor that satisfies a predicate.

pred
The test, given each character in turn.
r
The text.
c
The cursor of r to start at.
returns
(Some cursor) on the first character from c that pred returns #t for, or None.
(text-cursor-find char-whitespace? r c)   ; the next space, tab or newline

See also: text-cursor-find-backward, text-cursor-search, text-find

text-cursor-find-backward

function ↑ Functions

(: text-cursor-find-backward (-> (-?-> char bool) Text TextCursor (Option TextCursor)))
(text-cursor-find-backward pred r c)

The last character before a cursor that satisfies a predicate.

pred
The test, given each character in turn, going back.
r
The text.
c
The cursor of r to search back from.
returns
(Some cursor) on the last character before c that pred returns #t for, or None.

See also: text-cursor-find

text-cursor=?

function ↑ Functions

(: text-cursor=? (-> TextCursor TextCursor bool))
(text-cursor=? a b)

Whether two cursors are at the same position.

a
A cursor.
b
A cursor of the same text.
returns
#t when they are at the same position. The same as =.

See also: text-cursor<?

text-cursor<?

function ↑ Functions

(: text-cursor<? (-> TextCursor TextCursor bool))
(text-cursor<? a b)

Whether one cursor is before another.

a
A cursor.
b
A cursor of the same text.
returns
#t when a is before b.

See also: text-cursor<=?, text-cursor>?, text-cursor=?

text-cursor>?

function ↑ Functions

(: text-cursor>? (-> TextCursor TextCursor bool))
(text-cursor>? a b)

Whether one cursor is after another.

a
A cursor.
b
A cursor of the same text.
returns
#t when a is after b.

See also: text-cursor>=?, text-cursor<?

text-cursor<=?

function ↑ Functions

(: text-cursor<=? (-> TextCursor TextCursor bool))
(text-cursor<=? a b)

Whether one cursor is before another or at it.

a
A cursor.
b
A cursor of the same text.
returns
#t when a is not after b.

See also: text-cursor<?, text-cursor>=?

text-cursor>=?

function ↑ Functions

(: text-cursor>=? (-> TextCursor TextCursor bool))
(text-cursor>=? a b)

Whether one cursor is after another or at it.

a
A cursor.
b
A cursor of the same text.
returns
#t when a is not before b.

See also: text-cursor>?, text-cursor<=?

text-backward

function ↑ Functions

(: text-backward (-> Text TextCursor TextBackward))
(text-backward r c)

The characters before a cursor, last first, for a loop.

r
The text.
c
A cursor of r.
returns
A TextBackward: (:for ch (text-backward r c)) goes from the character before c back to the first.
(loop (:for ch (text-backward r (text-cursor-end r))) (:acc (stringing ch)))   ; r reversed

See also: TextCursor, text-cursor-prev, text-cursor-find-backward

text-cursor-next-grapheme

function ↑ Functions

(: text-cursor-next-grapheme (-> Text TextCursor TextCursor))
(text-cursor-next-grapheme r c)

The cursor after the grapheme cluster that starts at a cursor.

r
The text.
c
A cursor of r on a cluster boundary, not at the end.
returns
The cursor at the end of the cluster that starts at c, which is the next boundary.
raises System.ArgumentOutOfRangeException
When c is at the end of r.
;; The right arrow of an editor.
(def caret2 (text-cursor-next-grapheme r caret))

The rules are those of (std unicode grapheme), which also says what a cluster boundary is and what the steps cost. A cluster may cross chunks. Between two bytes of printable ASCII, the step reads only those.

See also: text-cursor-prev-grapheme, text-cursor-next, string-cursor-next-grapheme

text-cursor-prev-grapheme

function ↑ Functions

(: text-cursor-prev-grapheme (-> Text TextCursor TextCursor))
(text-cursor-prev-grapheme r c)

The cursor at the start of the grapheme cluster that ends at a cursor.

r
The text.
c
A cursor of r on a cluster boundary, not at the start.
returns
The cursor at the start of the cluster before c, which is the boundary before.
raises System.ArgumentOutOfRangeException
When c is at the start of r.
;; Backspace: delete the cluster before the caret.
(text-delete r (text-cursor-prev-grapheme r caret) caret)

A loop back over a text takes O(n) time: see (std unicode grapheme).

See also: text-cursor-next-grapheme, text-cursor-prev, string-cursor-prev-grapheme

text-cursor-grapheme-boundary?

function ↑ Functions

(: text-cursor-grapheme-boundary? (-> Text TextCursor bool))
(text-cursor-grapheme-boundary? r c)

Whether a cursor is on a grapheme cluster boundary.

r
The text.
c
A cursor of r.
returns
#t at the start, at the end, and between two clusters.

Use it on a cursor that came from elsewhere, such as text-cursor-forward, before stepping by clusters from it.

See also: text-cursor-next-grapheme, string-cursor-grapheme-boundary?

text-grapheme-count

function ↑ Functions

(: text-grapheme-count (-> Text int))
(text-grapheme-count r)

The number of grapheme clusters in a text.

r
The text.
returns
How many clusters r holds: 0 for the empty text. O(n) time.

See also: text->graphemes, text-count, string-grapheme-count

text->graphemes

function ↑ Functions

(: text->graphemes (-> Text (Vec Text)))
(text->graphemes r)

The grapheme clusters of a text.

r
The text.
returns
A vec of the clusters of r, in order, each a text.

See also: text-grapheme-count, text-cursor-next-grapheme, string->graphemes

text-fold-chunks

function ↑ Functions

(: text-fold-chunks (-> (-?-> string %a %a) %a Text %a))
(text-fold-chunks f init r)

Folds over a text’s chunks in order.

f
Called with each chunk and the accumulator, and returns the next accumulator.
init
The first accumulator.
r
The text.
returns
The last accumulator.

The chunks are strings that start and end on character boundaries. How a text is split into chunks depends on its history; the empty text has none.

See also: text-for-each-chunk, text-chunk-count, text-fold

text-for-each-chunk

function ↑ Functions

(: text-for-each-chunk (-> (-?-> string void) Text void))
(text-for-each-chunk f r)

Calls a function with each of a text’s chunks in order.

f
Called with each chunk.
r
The text.

See also: text-fold-chunks

text-chunk-count

function ↑ Functions

(: text-chunk-count (-> Text int))
(text-chunk-count r)

The number of chunks of a text.

r
The text.
returns
The number of chunks r's text is held in.

See also: text-fold-chunks

textbuilder-empty

function ↑ Functions

(: textbuilder-empty (-> TextBuilder))
(textbuilder-empty)

A new, empty text builder.

returns
A builder with no text.

See also: textbuilder-add-string!, textbuilder->text

textbuilder-add-string!

function ↑ Functions

(: textbuilder-add-string! (-> TextBuilder string void))
(textbuilder-add-string! b s)

Adds a string to the end of a builder.

b
The builder.
s
The string. The chunks of a long string share its bytes.

See also: textbuilder-add-text!, textbuilder->text

textbuilder-add!

function ↑ Functions

(: textbuilder-add! (-> TextBuilder char void))
(textbuilder-add! b ch)

Adds a character to the end of a builder.

b
The builder.
ch
The character.

See also: textbuilder-add-string!, textbuilder-add-piece!

textbuilder-add-text!

function ↑ Functions

(: textbuilder-add-text! (-> TextBuilder Text void))
(textbuilder-add-text! b r)

Adds the text of a text to the end of a builder.

b
The builder.
r
The text, whose chunks are added in order.

See also: textbuilder-add-string!

textbuilder-length

function ↑ Functions

(: textbuilder-length (-> TextBuilder int))
(textbuilder-length b)

The bytes added to a builder.

b
The builder.
returns
The number of bytes added since it was made or last emptied.

See also: textbuilder-add-string!

textbuilder->text

function ↑ Functions

(: textbuilder->text (-> TextBuilder Text))
(textbuilder->text b)

The text of a builder’s text, emptying the builder.

b
The builder.
returns
A text of the text added, in order. The builder is then empty and can be used again.

See also: textbuilder-empty

text->transienttext

function ↑ Functions

(: text->transienttext (-> Text TransientText))
(text->transienttext r)

A transient that starts from a text.

r
The text, which the transient's edits do not change.
returns
A transient holding r's text.

See also: transienttext->text, TransientText

transienttext->text

function ↑ Functions

(: transienttext->text (-> TransientText Text))
(transienttext->text t)

The text of a transient’s text.

t
The transient.
returns
A text of t's text. Later edits to t do not change it, and t can be edited further.

See also: text->transienttext

transienttext-insert!

function ↑ Functions

(: transienttext-insert! (-> TransientText int string void))
(transienttext-insert! t n s)

Inserts a string into a transient before a character.

t
The transient.
n
The index of the character to insert before, from 0 to the characters t holds.
s
The string to insert.
raises System.ArgumentOutOfRangeException
When n is outside t's text.

See also: transienttext-delete!, text-insert

transienttext-delete!

function ↑ Functions

(: transienttext-delete! (-> TransientText int int void))
(transienttext-delete! t from to)

Removes a range of characters from a transient.

t
The transient.
from
The index of the first character to remove.
to
The index after the last to remove.
raises System.ArgumentOutOfRangeException
When the range is not in t's text, or ends before it starts.

See also: transienttext-insert!, text-delete

transienttext-replace-range!

function ↑ Functions

(: transienttext-replace-range! (-> TransientText int int string void))
(transienttext-replace-range! t from to s)

Replaces a range of characters of a transient with a string.

t
The transient.
from
The index of the first character to replace.
to
The index after the last to replace.
s
The string to put in their place.
raises System.ArgumentOutOfRangeException
When the range is not in t's text, or ends before it starts.

See also: transienttext-insert!, transienttext-delete!, text-replace-range

transienttext-append!

function ↑ Functions

(: transienttext-append! (-> TransientText string void))
(transienttext-append! t s)

Adds a string to the end of a transient.

t
The transient.
s
The string to add.

See also: transienttext-insert!

transienttext-length

function ↑ Functions

(: transienttext-length (-> TransientText int))
(transienttext-length t)

The length of a transient’s text in bytes.

t
The transient.
returns
The number of bytes of t's text, in O(1) time.

See also: transienttext-count, text-length

transienttext-count

function ↑ Functions

(: transienttext-count (-> TransientText int))
(transienttext-count t)

The number of characters in a transient’s text.

t
The transient.
returns
How many characters t holds, in O(1) time: the end of the range its indices take.

See also: transienttext-length, text-count

text-check

function ↑ Functions

(: text-check (-> Text (Option string)))
(text-check r)

What is wrong with a text’s tree, if anything. For tests.

r
The text.
returns
None when r's tree is sound, otherwise (Some why) for the first problem found.

Checks the heights, the number of children of every node, the size of every chunk, and the byte, newline and character counts. O(n) time.

See also: text-height

text-height

function ↑ Functions

(: text-height (-> Text int))
(text-height r)

The height of a text’s tree. For tests.

r
The text.
returns
0 for a text that is one string, otherwise the levels of nodes above the chunks.

See also: text-check