text.bend checks
raw source on the hub · import bend-kit-text@0.2.0.0/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 Tip source · line 73 · raw
@-A:Data -> Data
The furthest point reached on one diagonal: x and y items consumed, a and b the rest.
Dead@-A:Data -> Tip<A>
Tip@-A:Data -> @x:U32 -> @y:U32 -> @a:List<&2, A> -> @b:List<&2, A> -> Tip<A>
type Split source · line 157 · raw
@-A:Data -> Data
A point on a shortest path, and the items after it.
Split@-A:Data -> @x:U32 -> @y:U32 -> @a:List<&2, A> -> @b:List<&2, A> -> Split<A>
type Dec source · line 160 · raw
Data
AdvFDec
AdvRDec
HitDec
EndDec
type Job source · line 262 · raw
@-A:Data -> Data
A segment still to diff, or moves ready to emit.
JSolve@-A:Data -> @a:List<&2, A> -> @b:List<&2, A> -> Job<A>
JEmit@-A:Data -> @ms:List<&2, Move> -> Job<A>
type Row source · line 387 · 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 403 · raw
Data
Hunk@o:U32 -> @n:U32 -> @ol:U32 -> @nl:U32 -> @body:List<&2, String> -> Hunk
type Wr source · line 566 · 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 tip.del source · line 99 · raw
@-A:Data -> @tip:Tip<A> -> Tip<A>
def tip.ins source · line 106 · raw
@-A:Data -> @tip:Tip<A> -> Tip<A>
def tip.reach source · line 113 · raw
@-A:Data -> @+tip:Tip<A> -> @+step:U32 -> U32
def choose.or source · line 121 · raw
@-A:Data -> @ins:Bool -> @first:Tip<A> -> @other:Tip<A> -> Tip<A>
The first tip, or the other one moved when the first is Dead.
def choose.pick source · line 130 · raw
@-A:Data -> @down:Bool -> @prev:Tip<A> -> @next:Tip<A> -> Tip<A>
def choose source · line 138 · raw
@-A:Data -> @+prev:Tip<A> -> @+next:Tip<A> -> Tip<A>
Myers' rule for diagonal k: go down from next (k+1) when V[k-1] < V[k+1], else right from prev (k-1).
def decide.cmp source · line 166 · raw
@+l:U32 -> @+r:U32 -> @+reach:Bool -> Dec
def decide source · line 171 · raw
@-A:Data -> @+f:List<&2, Tip<A>> -> @+r:List<&2, Tip<A>> -> @+n:U32 -> @+m:U32 -> Dec
f holds forward tips by ascending k = x - y. r holds reverse tips by ascending forward diagonal N - M - k'. Both diagonals match when x1 - y1 = (n - m) - (x2 - y2), written without subtraction.
def overlap.hit source · line 182 · raw
@-A:Data -> @+f:List<&2, Tip<A>> -> Maybe<&2, Split<A>>
def overlap.go source · line 189 · raw
@-A:Data -> @fuel:Nat -> @d:Dec -> @+f:List<&2, Tip<A>> -> @+r:List<&2, Tip<A>> -> @+n:U32 -> @+m:U32 -> Maybe<&2, Split<A>>
def overlap source · line 203 · raw
@-A:Data -> @+fuel:Nat -> @+f:List<&2, Tip<A>> -> @+r:List<&2, Tip<A>> -> @+n:U32 -> @+m:U32 -> Maybe<&2, Split<A>>
The forward tip whose diagonal the reverse frontier has passed, if any.
def overlap.pick source · line 206 · raw
@-A:Data -> @odd:Bool -> @+fuel:Nat -> @+f:List<&2, Tip<A>> -> @r0:List<&2, Tip<A>> -> @r1:List<&2, Tip<A>> -> @+n:U32 -> @+m:U32 -> Maybe<&2, Split<A>>
def plan.split source · line 266 · raw
@-A:Data -> @s:Maybe<&2, Split<A>> -> @+a:List<&2, A> -> @+b:List<&2, A> -> List<&2, Job<A>>
def canon.flush source · line 304 · raw
@d:Nat -> @i:Nat -> @rest:List<&2, Move> -> List<&2, Move>
def canon source · line 308 · 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 328 · raw
@c:Char -> @ls:List<&2, String> -> List<&2, String>
def lines.pick source · line 335 · raw
@nl:Bool -> @c:Char -> @ls:List<&2, String> -> List<&2, String>
def lines source · line 343 · 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 diff_lines source · line 350 · raw
@a:String -> @b:String -> List<&2, Edit<String>>
def diff_chars source · line 353 · raw
@a:String -> @b:String -> List<&2, Edit<Char>>
def far source · line 358 · raw
U32
def ahead.next source · line 361 · raw
@+r:List<&2, U32> -> U32
def ahead.cons source · line 368 · raw
@+change:Bool -> @+r:List<&2, U32> -> List<&2, U32>
def is_change source · line 371 · raw
@-A:Data -> @e:Edit<A> -> Bool
def ahead source · line 379 · 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 391 · raw
@es:List<&2, Edit<String>> -> @ds:List<&2, U32> -> @+ctx:U32 -> @+back:U32 -> List<&2, Row>
def range.pick source · line 407 · 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 415 · raw
@+start:U32 -> @+len:U32 -> String
def hunk.show source · line 418 · raw
@h:Hunk -> String
def close source · line 424 · raw
@cur:Maybe<&2, Hunk> -> @out:List<&2, String> -> List<&2, String>
def open source · line 431 · raw
@cur:Maybe<&2, Hunk> -> @+o:U32 -> @+n:U32 -> Hunk
def eol source · line 439 · 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 442 · raw
@h:Hunk -> @+text:String -> @+dold:U32 -> @+dnew:U32 -> Hunk
def emit source · line 448 · 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 457 · raw
@hs:List<&2, String> -> @old_name:String -> @new_name:String -> String
def unified source · line 466 · 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 479 · raw
@xs:List<&2, U32> -> @+d:U32 -> U32
def lev.init source · line 486 · raw
@n:Nat -> @+i:U32 -> List<&2, U32>
def distance source · line 506 · raw
@a:String -> @b:String -> U32
Levenshtein distance by code point.
def words.flush source · line 511 · raw
@cur:String -> @out:List<&2, String> -> List<&2, String>
def words.step source · line 519 · 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 528 · raw
@s:String -> @st:Pair(String, List<&2, String>) -> List<&2, String>
def words source · line 537 · raw
@s:String -> List<&2, String>
Runs of non-space chars.
def fits source · line 540 · raw
@+line:String -> @+width:U32 -> Bool
def single.of source · line 543 · raw
@gs:List<&2, String> -> Bool
def single source · line 551 · raw
@+s:String -> Bool
One grapheme cluster: the only kind of line that may be wider than the width.
def ok.pick source · line 554 · raw
@f:Bool -> @+s:String -> Bool
def ok source · line 562 · 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 569 · raw
@f:Bool -> @out:List<&2, String> -> @s:String -> @+c:Char -> Wr
def step0 source · line 576 · raw
@st:Wr -> @+c:Char -> @+width:U32 -> Wr
def run0 source · line 584 · 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 591 · raw
@ok:Bool -> @out:List<&2, String> -> @+g:String -> @+width:U32 -> Wr
def step1.pick source · line 598 · raw
@f:Bool -> @out:List<&2, String> -> @s:String -> @+g:String -> @+width:U32 -> Wr
def step1 source · line 605 · raw
@st:Wr -> @+g:String -> @+width:U32 -> Wr
def run1 source · line 613 · raw
@gs:List<&2, String> -> @+width:U32 -> @st:Wr -> Wr
Grapheme clusters, for a word wider than the line.
def start2 source · line 620 · raw
@ok:Bool -> @out:List<&2, String> -> @+w:String -> @+width:U32 -> Wr
def step2.pick source · line 627 · raw
@f:Bool -> @out:List<&2, String> -> @s:String -> @+w:String -> @+width:U32 -> Wr
def step2 source · line 634 · raw
@st:Wr -> @+w:String -> @+width:U32 -> Wr
def run2 source · line 641 · raw
@ws:List<&2, String> -> @+width:U32 -> @st:Wr -> Wr
def wrap.lines source · line 648 · raw
@st:Wr -> List<&2, String>
def wrap source · line 658 · 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 heads source · line 77 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @+a:List<&2, A> -> @+b:List<&2, A> -> Bool
template snake source · line 85 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @a:List<&2, A> -> @more:Bool -> @b:List<&2, A> -> @+x:U32 -> @+y:U32 -> Tip<A>
more says whether the heads of a and b match.
template slide source · line 92 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @tip:Tip<A> -> Tip<A>
template step.mid source · line 141 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @ts:List<&2, Tip<A>> -> @+prev:Tip<A> -> List<&2, Tip<A>>
template step source · line 149 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @ts:List<&2, Tip<A>> -> List<&2, Tip<A>>
Diagonals -d..d, low k first, become -(d+1)..d+1.
template step.desc.mid source · line 213 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @ts:List<&2, Tip<A>> -> @+prev:Tip<A> -> List<&2, Tip<A>>
template step.desc source · line 221 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @ts:List<&2, Tip<A>> -> List<&2, Tip<A>>
step for a frontier kept high k first, so the reverse frontier lines up with the forward one.
template bisect source · line 230 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @fuel:Nat -> @found:Maybe<&2, Split<A>> -> @+odd:Bool -> @+of:Nat -> @f:List<&2, Tip<A>> -> @+r:List<&2, Tip<A>> -> @+n:U32 -> @+m:U32 -> Maybe<&2, Split<A>>
Myers' middle snake: grow d-paths from both ends. When N - M is odd, a forward d-path is checked against the reverse (d-1)-paths; when even, against the reverse d-paths.
template middle source · line 241 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @+a:List<&2, A> -> @+b:List<&2, A> -> Maybe<&2, Split<A>>
template common source · line 251 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @a:List<&2, A> -> @more:Bool -> @b:List<&2, A> -> @+k:Nat -> Nat
template prefix source · line 258 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @+a:List<&2, A> -> @+b:List<&2, A> -> Nat
template plan.mid source · line 274 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @+a:List<&2, A> -> @+b:List<&2, A> -> List<&2, Job<A>>
template plan.trim source · line 283 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @+p:Nat -> @+a:List<&2, A> -> @+b:List<&2, A> -> List<&2, Job<A>>
template plan source · line 290 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @+a:List<&2, A> -> @+b:List<&2, A> -> List<&2, Job<A>>
Jobs for one segment, last first: the common suffix, the middle, then the common prefix.
template solve source · line 295 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @fuel:Nat -> @js:List<&2, Job<A>> -> @acc:List<&2, Move> -> List<&2, Move>
Runs jobs from the end of the script back to its start, so each emit prepends.
template moves source · line 320 · raw
@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @+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 diff source · line 325 · 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 471 · 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 493 · 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 502 · 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.