~/bend-docscommunity

text.bend checks

raw source on the hub · import bend-kit-text@0.1.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.

type Move source · line 14 · raw

Data

type Tip source · line 73 · raw

@-A:Data -> Data

One furthest-reaching path on a diagonal: x old items consumed, a and b the rest, path newest first.

type Row source · line 229 · raw

Data

A Gap is a Same line outside every hunk. dold and dnew count the old and new lines a row consumes.

type Hunk source · line 245 · raw

Data

type Wrap source · line 379 · raw

Data

The line being built, its display width, and the finished lines, newest first.

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 choose.pick source · line 113 · raw

@-A:Data -> @right:Bool -> @l:Tip<A> -> @r:Tip<A> -> Tip<A>

def choose source · line 121 · raw

@-A:Data -> @l:Tip<A> -> @r:Tip<A> -> Tip<A>

Myers' rule: go down from k+1 when V[k-1] < V[k+1]; l is already one step right of V[k-1].

def find source · line 145 · raw

@-A:Data -> @ts:List<&2, Tip<A>> -> Maybe<&2, List<&2, Move>>

def lines.fin source · line 172 · raw

@empty:Bool -> @+h:String -> List<&2, String>

def lines.go source · line 179 · raw

@xs:List<&2, String> -> List<&2, String>

def lines source · line 189 · raw

@s:String -> List<&2, String>

Lines split on "\n". A final "\n" ends the last line; it does not start an empty one.

def diff_lines source · line 192 · raw

@a:String -> @b:String -> List<&2, Edit<String>>

def diff_chars source · line 195 · raw

@a:String -> @b:String -> List<&2, Edit<Char>>

def far source · line 200 · raw

U32

def ahead.next source · line 203 · raw

@+r:List<&2, U32> -> U32

def ahead.cons source · line 210 · raw

@+change:Bool -> @+r:List<&2, U32> -> List<&2, U32>

def is_change source · line 213 · raw

@-A:Data -> @e:Edit<A> -> Bool

def ahead source · line 221 · 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 233 · raw

@es:List<&2, Edit<String>> -> @ds:List<&2, U32> -> @+ctx:U32 -> @+back:U32 -> List<&2, Row>

def range.pick source · line 249 · 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 257 · raw

@+start:U32 -> @+len:U32 -> String

def hunk.show source · line 260 · raw

@h:Hunk -> String

def close source · line 266 · raw

@cur:Maybe<&2, Hunk> -> @out:List<&2, String> -> List<&2, String>

def open source · line 273 · raw

@cur:Maybe<&2, Hunk> -> @+o:U32 -> @+n:U32 -> Hunk

def hunk.add source · line 280 · raw

@h:Hunk -> @text:String -> @+dold:U32 -> @+dnew:U32 -> Hunk

def emit source · line 286 · 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 295 · raw

@hs:List<&2, String> -> @old_name:String -> @new_name:String -> String

def unified source · line 304 · 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. Lines carry no newline, so a missing final newline is not marked.

def lev.last source · line 317 · raw

@xs:List<&2, U32> -> @+d:U32 -> U32

def lev.init source · line 324 · raw

@n:Nat -> @+i:U32 -> List<&2, U32>

def distance source · line 344 · raw

@a:String -> @b:String -> U32

Levenshtein distance by code point.

def words.flush source · line 349 · raw

@cur:String -> @out:List<&2, String> -> List<&2, String>

def words.step source · line 357 · 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 366 · raw

@s:String -> @st:Pair(String, List<&2, String>) -> List<&2, String>

def words source · line 375 · raw

@s:String -> List<&2, String>

Runs of non-space chars.

def wrap.flush source · line 382 · raw

@cur:String -> @out:List<&2, String> -> List<&2, String>

def chop.pick source · line 389 · raw

@push:Bool -> @st:Wrap -> @g:String -> @+gw:U32 -> Wrap

def chop.one source · line 400 · raw

@st:Wrap -> @+g:String -> @+width:U32 -> Wrap

def chop source · line 407 · raw

@gs:List<&2, String> -> @+width:U32 -> @st:Wrap -> Wrap

Breaks a word wider than the line at grapheme boundaries.

def place source · line 414 · raw

@fits:Bool -> @out:List<&2, String> -> @+word:String -> @+ww:U32 -> @+width:U32 -> Wrap

def wrap.word.pick source · line 421 · raw

@join:Bool -> @st:Wrap -> @word:String -> @+ww:U32 -> @+width:U32 -> Wrap

def wrap.word source · line 432 · raw

@st:Wrap -> @+word:String -> @+width:U32 -> Wrap

def wrap.go source · line 438 · raw

@ws:List<&2, String> -> @+width:U32 -> @st:Wrap -> List<&2, String>

def wrap source · line 450 · 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. A word wider than width is broken between grapheme clusters. Only a single cluster wider than width makes a line wider than width.

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 -> @p:List<&2, Move> -> 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 130 · raw

@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @ts:List<&2, Tip<A>> -> @+prev:Tip<A> -> List<&2, Tip<A>>

template step source · line 138 · 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 run source · line 154 · raw

@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @fuel:Nat -> @found:Maybe<&2, List<&2, Move>> -> @ts:List<&2, Tip<A>> -> List<&2, Move>

template moves source · line 164 · raw

@-A:Data -> @-eq:(@_:A -> @_:A -> Bool) -> @+a:List<&2, A> -> @+b:List<&2, A> -> List<&2, Move>

template diff source · line 169 · 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 309 · 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 331 · 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 340 · 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.