text.bend checks
raw source on the hub · import bend-kit-text@0.2.0.1/text.bend as Text
Text utilities: Myers diff with unified output, Levenshtein distance, and word wrap by display width.
3 imports
import Base import bend-kit-unicode@0.1.0.0/unicode.bend as Unicode import bend-kit-tty@0.1.0.1/text.bend as Tty
Types
type Edit source · line 9 · raw
@-A:Data -> Data
Same pairs an old item with the new item it matched; before keeps old items, after keeps new ones.
Same@-A:Data -> @old:A -> @new:A -> Edit<A>
Del@-A:Data -> @old:A -> Edit<A>
Ins@-A:Data -> @new:A -> Edit<A>
type Move source · line 14 · raw
Data
MSameMove
MDelMove
MInsMove
type Ctx source · line 73 · raw
@-A:Data -> Type
The items, and the forward and reverse frontiers: v[off + k] is the furthest x on diagonal k = x - y.
Ctx@-A:Data -> @a:Array<A> -> @b:Array<A> -> @vf:Array<U32> -> @vb:Array<U32> -> Ctx<A>
type Geo source · line 77 · raw
Data
A segment of n items of a from os, and m items of b from ns. off centres the frontiers.
Geo@os:U32 -> @ns:U32 -> @n:U32 -> @m:U32 -> @off:U32 -> Geo
type Pt source · line 80 · raw
Data
Pt@x:U32 -> @y:U32 -> Pt
type Job source · line 243 · raw
Data
A segment still to diff, or moves ready to emit.
JSolve@os:U32 -> @oe:U32 -> @ns:U32 -> @ne:U32 -> Job
JEmit@ms:List<&2, Move> -> Job
type Row source · line 464 · raw
Data
A Gap is a Same line outside every hunk. dold and dnew count the old and new lines a row consumes.
GapRow
Line@text:String -> @dold:U32 -> @dnew:U32 -> Row
type Hunk source · line 480 · raw
Data
Hunk@o:U32 -> @n:U32 -> @ol:U32 -> @nl:U32 -> @body:List<&2, String> -> Hunk
type Wr source · line 643 · raw
Data
The finished lines, newest first, and the line being built.
Wr@out:List<&2, String> -> @cur:Maybe<&2, String> -> Wr
Definitions
def before source · line 19 · raw
@-A:Data -> @es:List<&2, Edit<A>> -> List<&2, A>
def after source · line 30 · raw
@-A:Data -> @es:List<&2, Edit<A>> -> List<&2, A>
def walk.ins source · line 41 · raw
@-A:Data -> @b:List<&2, A> -> List<&2, Edit<A>>
def walk.rest source · line 48 · raw
@-A:Data -> @a:List<&2, A> -> @b:List<&2, A> -> List<&2, Edit<A>>
def walk source · line 57 · raw
@-A:Data -> @ms:List<&2, Move> -> @a:List<&2, A> -> @b:List<&2, A> -> List<&2, Edit<A>>
Replays moves over a and b. A move that does not fit is skipped, and leftovers become Del then Ins, so before and after give a and b back for any moves.
def geo.os source · line 83 · raw
@+g:Geo -> U32
def geo.ns source · line 88 · raw
@+g:Geo -> U32
def geo.n source · line 93 · raw
@+g:Geo -> U32
def geo.m source · line 98 · raw
@+g:Geo -> U32
def geo.off source · line 103 · raw
@+g:Geo -> U32
def changes source · line 247 · raw
@+dels:U32 -> @+ins:U32 -> List<&2, Move>
def canon.flush source · line 313 · raw
@d:Nat -> @i:Nat -> @rest:List<&2, Move> -> List<&2, Move>
def canon source · line 317 · raw
@ms:List<&2, Move> -> @+d:Nat -> @+i:Nat -> List<&2, Move>
Each run of changes puts its deletes first, as diff does.
def lines.push source · line 363 · raw
@c:Char -> @ls:List<&2, String> -> List<&2, String>
def lines.pick source · line 370 · raw
@nl:Bool -> @c:Char -> @ls:List<&2, String> -> List<&2, String>
def lines source · line 378 · raw
@s:String -> List<&2, String>
Lines with their "\n". Only the last line can lack one, and joining the lines gives s back.
def fnv source · line 385 · raw
@s:String -> @+h:U32 -> U32
def hashes source · line 392 · raw
@ls:List<&2, String> -> List<&2, U32>
def lines.ok source · line 400 · raw
@ms:List<&2, Move> -> @a:List<&2, String> -> @b:List<&2, String> -> Bool
Whether every pair that walk would match holds equal lines.
def lines.pick.moves source · line 413 · raw
@ok:Bool -> @ms:List<&2, Move> -> @+a:List<&2, String> -> @+b:List<&2, String> -> List<&2, Move>
def lines.moves source · line 421 · raw
@+a:List<&2, String> -> @+b:List<&2, String> -> List<&2, Move>
Diffs line hashes, which is much faster than comparing strings. A hash collision that pairs unequal lines falls back to strings.
def diff_lines source · line 425 · raw
@a:String -> @b:String -> List<&2, Edit<String>>
def diff_chars source · line 430 · raw
@a:String -> @b:String -> List<&2, Edit<Char>>
def far source · line 435 · raw
U32
def ahead.next source · line 438 · raw
@+r:List<&2, U32> -> U32
def ahead.cons source · line 445 · raw
@+change:Bool -> @+r:List<&2, U32> -> List<&2, U32>
def is_change source · line 448 · raw
@-A:Data -> @e:Edit<A> -> Bool
def ahead source · line 456 · raw
@-A:Data -> @es:List<&2, Edit<A>> -> List<&2, U32>
For each edit, how many edits ahead the next change is: 0 for a change.
def rows source · line 468 · raw
@es:List<&2, Edit<String>> -> @ds:List<&2, U32> -> @+ctx:U32 -> @+back:U32 -> List<&2, Row>
def range.pick source · line 484 · raw
@empty:Bool -> @+start:U32 -> @+len:U32 -> String
GNU form: an empty range names the line before it, and a one-line range drops ",1".
def range source · line 492 · raw
@+start:U32 -> @+len:U32 -> String
def hunk.show source · line 495 · raw
@h:Hunk -> String
def close source · line 501 · raw
@cur:Maybe<&2, Hunk> -> @out:List<&2, String> -> List<&2, String>
def open source · line 508 · raw
@cur:Maybe<&2, Hunk> -> @+o:U32 -> @+n:U32 -> Hunk
def eol source · line 516 · raw
@+text:String -> String
Text whose line lacks "\n" is the last line of its file, as diff -u marks it.
def hunk.add source · line 519 · raw
@h:Hunk -> @+text:String -> @+dold:U32 -> @+dnew:U32 -> Hunk
def emit source · line 525 · raw
@rs:List<&2, Row> -> @+o:U32 -> @+n:U32 -> @cur:Maybe<&2, Hunk> -> @out:List<&2, String> -> List<&2, String>
o and n count the old and new lines before the row.
def unified.fin source · line 534 · raw
@hs:List<&2, String> -> @old_name:String -> @new_name:String -> String
def unified source · line 543 · raw
@old_name:String -> @new_name:String -> @+es:List<&2, Edit<String>> -> @+ctx:U32 -> String
Unified diff text with ctx lines of context around each change; "" when nothing changed. Each line must end with "\n" except the last line of a file, as lines() gives them.
def lev.last source · line 556 · raw
@xs:List<&2, U32> -> @+d:U32 -> U32
def lev.init source · line 563 · raw
@n:Nat -> @+i:U32 -> List<&2, U32>
def distance source · line 583 · raw
@a:String -> @b:String -> U32
Levenshtein distance by code point.
def words.flush source · line 588 · raw
@cur:String -> @out:List<&2, String> -> List<&2, String>
def words.step source · line 596 · raw
@sp:Bool -> @+c:Char -> @st:Pair(String, List<&2, String>) -> Pair(String, List<&2, String>)
cur holds the word being read, reversed.
def words.go source · line 605 · raw
@s:String -> @st:Pair(String, List<&2, String>) -> List<&2, String>
def words source · line 614 · raw
@s:String -> List<&2, String>
Runs of non-space chars.
def fits source · line 617 · raw
@+line:String -> @+width:U32 -> Bool
def single.of source · line 620 · raw
@gs:List<&2, String> -> Bool
def single source · line 628 · raw
@+s:String -> Bool
One grapheme cluster: the only kind of line that may be wider than the width.
def ok.pick source · line 631 · raw
@f:Bool -> @+s:String -> Bool
def ok source · line 639 · raw
@+s:String -> @+width:U32 -> Bool
What wrap promises for each line: it fits, or it is one grapheme cluster.
def step0.pick source · line 646 · raw
@f:Bool -> @out:List<&2, String> -> @s:String -> @+c:Char -> Wr
def step0 source · line 653 · raw
@st:Wr -> @+c:Char -> @+width:U32 -> Wr
def run0 source · line 661 · raw
@s:String -> @+width:U32 -> @st:Wr -> Wr
Code points, for a piece that neither fits nor segments as one cluster. One code point is one cluster.
def start1 source · line 668 · raw
@ok:Bool -> @out:List<&2, String> -> @+g:String -> @+width:U32 -> Wr
def step1.pick source · line 675 · raw
@f:Bool -> @out:List<&2, String> -> @s:String -> @+g:String -> @+width:U32 -> Wr
def step1 source · line 682 · raw
@st:Wr -> @+g:String -> @+width:U32 -> Wr
def run1 source · line 690 · raw
@gs:List<&2, String> -> @+width:U32 -> @st:Wr -> Wr
Grapheme clusters, for a word wider than the line.
def start2 source · line 697 · raw
@ok:Bool -> @out:List<&2, String> -> @+w:String -> @+width:U32 -> Wr
def step2.pick source · line 704 · raw
@f:Bool -> @out:List<&2, String> -> @s:String -> @+w:String -> @+width:U32 -> Wr
def step2 source · line 711 · raw
@st:Wr -> @+w:String -> @+width:U32 -> Wr
def run2 source · line 718 · raw
@ws:List<&2, String> -> @+width:U32 -> @st:Wr -> Wr
def wrap.lines source · line 725 · raw
@st:Wr -> List<&2, String>
def wrap source · line 735 · raw
@text:String -> @+width:U32 -> List<&2, String>
Greedy word wrap to width display columns. Whitespace runs separate words; lines join words with one space. Each candidate line is measured whole, so a cluster that spans a join counts right. A word wider than width breaks between grapheme clusters; a cluster wider than width stays whole on its own line.
Templates
template same.b source · line 108 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @x:A -> @a:Array<A> -> @r:Pair(Array<A>, A) -> Pair(Array<A>, Pair(Array<A>, Bool))
template same.a source · line 112 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @r:Pair(Array<A>, A) -> @b:Array<A> -> @+j:U32 -> Pair(Array<A>, Pair(Array<A>, Bool))
template probe.in source · line 116 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @inr:Bool -> @a:Array<A> -> @b:Array<A> -> @+rev:Bool -> @+g:Geo -> @+x:U32 -> @+y:U32 -> Pair(Array<A>, Pair(Array<A>, Bool))
template probe source · line 126 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @a:Array<A> -> @b:Array<A> -> @+rev:Bool -> @+g:Geo -> @+x:U32 -> @+y:U32 -> Pair(Array<A>, Pair(Array<A>, Bool))
Whether item x of the segment of a matches item y of the segment of b. rev counts from the segment end.
template snake source · line 129 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @fuel:Nat -> @r:Pair(Array<A>, Pair(Array<A>, Bool)) -> @+rev:Bool -> @+g:Geo -> @+x:U32 -> @+y:U32 -> Pair(Array<A>, Pair(Array<A>, U32))
template slide source · line 143 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @a:Array<A> -> @b:Array<A> -> @+rev:Bool -> @+g:Geo -> @+x:U32 -> @+y:U32 -> Pair(Array<A>, Pair(Array<A>, U32))
The x where the run of matches from (x, y) ends.
template ctx.mk source · line 147 · raw
@-A:Data -> @rev:Bool -> @a:Array<A> -> @b:Array<A> -> @v:Array<U32> -> @w:Array<U32> -> Ctx<A>
v is the frontier that moves, w the other one.
template at.hit source · line 154 · raw
@-A:Data -> @r:Pair(Array<U32>, U32) -> @a:Array<A> -> @b:Array<A> -> @v:Array<U32> -> @+rev:Bool -> @+g:Geo -> @+k:U32 -> @+x:U32 -> @+x0:U32 -> Pair(Ctx<A>, Maybe<&2, Pt>)
template at.chk source · line 159 · raw
@-A:Data -> @on:Bool -> @a:Array<A> -> @b:Array<A> -> @v:Array<U32> -> @w:Array<U32> -> @+rev:Bool -> @+g:Geo -> @+k:U32 -> @+x:U32 -> @+x0:U32 -> Pair(Ctx<A>, Maybe<&2, Pt>)
template at.snk source · line 167 · raw
@-A:Data -> @r:Pair(Array<A>, Pair(Array<A>, U32)) -> @v:Array<U32> -> @w:Array<U32> -> @+rev:Bool -> @+g:Geo -> @+d:U32 -> @+k:U32 -> @+x0:U32 -> Pair(Ctx<A>, Maybe<&2, Pt>)
Myers' overlap test: forward d-paths meet reverse (d-1)-paths when n - m is odd, reverse d-paths when even.
template at.hi source · line 178 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @r:Pair(Array<U32>, U32) -> @+lo:U32 -> @a:Array<A> -> @b:Array<A> -> @w:Array<U32> -> @+rev:Bool -> @+g:Geo -> @+d:U32 -> @+k:U32 -> Pair(Ctx<A>, Maybe<&2, Pt>)
Myers' rule for diagonal k: go down from k+1 when V[k-1] < V[k+1], else right from k-1.
template at.lo source · line 184 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @r:Pair(Array<U32>, U32) -> @a:Array<A> -> @b:Array<A> -> @w:Array<U32> -> @+rev:Bool -> @+g:Geo -> @+d:U32 -> @+k:U32 -> Pair(Ctx<A>, Maybe<&2, Pt>)
template at source · line 189 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @rev:Bool -> @c:Ctx<A> -> @+g:Geo -> @+d:U32 -> @+k:U32 -> Pair(Ctx<A>, Maybe<&2, Pt>)
One diagonal of one sweep.
template sweep source · line 197 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @fuel:Nat -> @r:Pair(Ctx<A>, Maybe<&2, Pt>) -> @+rev:Bool -> @+g:Geo -> @+d:U32 -> @+k:U32 -> Pair(Ctx<A>, Maybe<&2, Pt>)
Diagonals k, k + 2, ... until fuel runs out or a sweep meets the other frontier.
template round.back source · line 210 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @r:Pair(Ctx<A>, Maybe<&2, Pt>) -> @+g:Geo -> @+d:U32 -> Pair(Ctx<A>, Maybe<&2, Pt>)
template round source · line 218 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @c:Ctx<A> -> @+g:Geo -> @+d:U32 -> Pair(Ctx<A>, Maybe<&2, Pt>)
template middle.go source · line 221 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @fuel:Nat -> @r:Pair(Ctx<A>, Maybe<&2, Pt>) -> @+g:Geo -> @+d:U32 -> Pair(Ctx<A>, Maybe<&2, Pt>)
template middle source · line 235 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @c:Ctx<A> -> @+g:Geo -> Pair(Ctx<A>, Maybe<&2, Pt>)
Myers' middle snake: grow d-paths from both ends of the segment until they meet. The meeting point is on a shortest path.
template plan.split source · line 250 · raw
@-A:Data -> @r:Pair(Ctx<A>, Maybe<&2, Pt>) -> @+os:U32 -> @+oe:U32 -> @+ns:U32 -> @+ne:U32 -> Pair(Ctx<A>, List<&2, Job>)
template plan.mid source · line 258 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @ea:Bool -> @eb:Bool -> @c:Ctx<A> -> @+os:U32 -> @+oe:U32 -> @+ns:U32 -> @+ne:U32 -> @+off:U32 -> Pair(Ctx<A>, List<&2, Job>)
template plan.wrap source · line 267 · raw
@-A:Data -> @r:Pair(Ctx<A>, List<&2, Job>) -> @+p:U32 -> @+s:U32 -> Pair(Ctx<A>, List<&2, Job>)
template plan.suf source · line 272 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @r:Pair(Array<A>, Pair(Array<A>, U32)) -> @vf:Array<U32> -> @vb:Array<U32> -> @+p:U32 -> @+os:U32 -> @+oe:U32 -> @+ns:U32 -> @+ne:U32 -> @+off:U32 -> Pair(Ctx<A>, List<&2, Job>)
template plan.pre source · line 281 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @r:Pair(Array<A>, Pair(Array<A>, U32)) -> @vf:Array<U32> -> @vb:Array<U32> -> @+os:U32 -> @+oe:U32 -> @+ns:U32 -> @+ne:U32 -> @+off:U32 -> Pair(Ctx<A>, List<&2, Job>)
template plan source · line 288 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @c:Ctx<A> -> @+os:U32 -> @+oe:U32 -> @+ns:U32 -> @+ne:U32 -> @+off:U32 -> Pair(Ctx<A>, List<&2, Job>)
Jobs for one segment, last first: the common suffix, the middle, then the common prefix.
template plan.then source · line 293 · raw
@-A:Data -> @r:Pair(Ctx<A>, List<&2, Job>) -> @t:List<&2, Job> -> Pair(Ctx<A>, List<&2, Job>)
template solve source · line 298 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @fuel:Nat -> @r:Pair(Ctx<A>, List<&2, Job>) -> @+off:U32 -> @acc:List<&2, Move> -> List<&2, Move>
Runs jobs from the end of the script back to its start, so each emit prepends.
template fill source · line 328 · raw
@-A:Data -> @xs:List<&2, A> -> @arr:Array<A> -> @+i:U32 -> Array<A>
template array source · line 336 · raw
@-A:Data -> @+n:U32 -> @+v:A -> Array<A>
An array of n slots or more.
template moves.go source · line 340 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @+x:A -> @+y:A -> @+a:List<&2, A> -> @+b:List<&2, A> -> List<&2, Move>
Each split leaves two smaller segments, and each segment plans at most 4 jobs.
template moves source · line 348 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @+a:List<&2, A> -> @+b:List<&2, A> -> List<&2, Move>
template diff source · line 360 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @+a:List<&2, A> -> @+b:List<&2, A> -> List<&2, Edit<A>>
A shortest edit script from a to b. eq decides which items match.
template lev.row source · line 548 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @b:List<&2, A> -> @up:List<&2, U32> -> @+x:A -> @+diag:U32 -> @+left:U32 -> List<&2, U32>
template lev.go source · line 570 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @a:List<&2, A> -> @+b:List<&2, A> -> @prev:List<&2, U32> -> @+i:U32 -> U32
template edit_distance source · line 579 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @a:List<&2, A> -> @+b:List<&2, A> -> U32
Fewest inserts, deletes, and substitutions that turn a into b.