~/bend-docscommunity

Text.bend source

Text.bend on the hub · documented module

import Base# Text: what the Haskell backend does with Data.Text and Data.Char, on# Bend's String (a list of code points): white space as Data.Char.isSpace# has it, strip, words, lines, breaking and splitting on a needle, prefixes# and infixes, cutting from the end.## What each means is Data.Text's function of the same name, on the same# code points; probe/Text.bend checks them on examples.## Every def is prefixed `tx.` (a program's defs share one namespace).# Data.Char.isSpace: the Latin-1 spaces and Unicode's Space_Separatordef tx.space(+c: U32) -> Bool:  U32.is_eq(c, 32) || (U32.is_ge(c, 9) && U32.is_le(c, 13)) || U32.is_eq(c, 160)    || (U32.is_ge(c, 5760) && (U32.is_eq(c, 5760) || (U32.is_ge(c, 8192) && U32.is_le(c, 8202)) || U32.is_eq(c, 8239) || U32.is_eq(c, 8287) || U32.is_eq(c, 12288)))def tx.drop.at(sp: Bool, +c: U32, t: String, more: Unit -> String) -> String:  match sp:    case True{}:      more(Unit{})    case False{}:      SCon{Chr{c}, t}def tx.drop.space(s: String) -> String:  match s:    case SNil{}:      ""    case SCon{Chr{+c}, +t}:      tx.drop.at(tx.space(c), c, t, u => tx.drop.space(t))def tx.strip.start(s: String) -> String:  tx.drop.space(s)def tx.strip.end(s: String) -> String:  String.reverse(tx.drop.space(String.reverse(s)))def tx.strip(s: String) -> String:  tx.strip.end(tx.drop.space(s))# a pair of strings (Data: a Maybe may hold one)type tx.Two is Data:  tx.Two{a: String, b: String}# Words: the runs of what is not white spacedef tx.words.push(w: String, ws: List<&2, String>) -> List<&2, String>:  match w:    case SNil{}:      ws    case SCon{h, t}:      String.reverse(SCon{h, t}) <> wsdef tx.words.at(sp: Bool, +c: U32, w: String, more: String -> List<&2, String>) -> List<&2, String>:  match sp:    case True{}:      tx.words.push(w, more(""))    case False{}:      more(SCon{Chr{c}, w})def tx.words.go(s: String, w: String) -> List<&2, String>:  match s:    case SNil{}:      tx.words.push(w, [])    case SCon{Chr{+c}, t}:      tx.words.at(tx.space(c), c, w, x => tx.words.go(t, x))def tx.words(s: String) -> List<&2, String>:  tx.words.go(s, "")def tx.unwords(ws: List<&2, String>) -> String:  String.join(ws, " ")# Lines: cut at "\n", a last empty one dropped (T.lines)def tx.lines.fix(ls: List<&2, String>) -> List<&2, String>:  match ls:    case Nil{}:      []    case Con{SNil{}, Nil{}}:      []    case Con{l, t}:      l <> tx.lines.fix(t)def tx.lines(s: String) -> List<&2, String>:  tx.lines.fix(String.split(s, '\n'))def tx.unlines(ls: List<&2, String>) -> String:  match ls:    case Nil{}:      ""    case Con{l, t}:      l ++ "\n" ++ tx.unlines(t)# From the enddef tx.take.end(+n: Nat, +s: String) -> String:  String.drop(s, Nat.sub(String.length(s), n))def tx.drop.end(+n: Nat, +s: String) -> String:  String.take(s, Nat.sub(String.length(s), n))def tx.nl.at(nl: Bool, +c: U32, more: Unit -> String) -> String:  match nl:    case True{}:      ""    case False{}:      SCon{Chr{c}, more(Unit{})}def tx.upto.nl(s: String) -> String:  match s:    case SNil{}:      ""    case SCon{Chr{+c}, +t}:      tx.nl.at(U32.is_eq(c, 10), c, u => tx.upto.nl(t))# what follows the last "\n" (all of s when none)def tx.last.line(s: String) -> String:  String.reverse(tx.upto.nl(String.reverse(s)))# Prefixes and suffixesdef tx.strip.prefix(+p: String, +s: String) -> Maybe<&2, String>:  Bool.pick(Maybe<&2, String>, String.starts_with(s, p), Some{String.drop(s, String.length(p))}, None{})def tx.strip.suffix(+p: String, +s: String) -> Maybe<&2, String>:  Bool.pick(Maybe<&2, String>, String.ends_with(s, p), Some{String.take(s, Nat.sub(String.length(s), String.length(p)))}, None{})def tx.break.cons(+h: Char, p: tx.Two) -> tx.Two:  match p:    case tx.Two{a, b}:      tx.Two{SCon{h, a}, b}def tx.break.at(hit: Bool, +h: Char, +t: String, more: Unit -> tx.Two) -> tx.Two:  match hit:    case True{}:      tx.Two{"", SCon{h, t}}    case False{}:      tx.break.cons(h, more(Unit{}))# s at its first `needle`: before it, and from it on ("" when none)def tx.break.on(+needle: String, s: String) -> tx.Two:  match s:    case SNil{}:      tx.Two{"", ""}    case SCon{+h, +t}:      tx.break.at(String.starts_with(SCon{h, t}, needle), h, t, u => tx.break.on(needle, t))def tx.split.at(hit: Bool, +h: Char, +t: String, +n: Nat, acc: String, more: String -> List<&2, String>, skip: Unit -> List<&2, String>) -> List<&2, String>:  match hit:    case True{}:      String.reverse(acc) <> skip(Unit{})    case False{}:      more(SCon{h, acc})# s cut at every `needle` (not empty), as T.splitOn: n+1 pieces for n# needles (fuel: s's length, which every step shortens)def tx.split.go(+fuel: Nat, +needle: String, s: String, acc: String) -> List<&2, String>:  match fuel s:    case 0n x:      [String.reverse(acc)]    case 1n+f SNil{}:      [String.reverse(acc)]    case 1n+f SCon{+h, +t}:      tx.split.at(String.starts_with(SCon{h, t}, needle), h, t, String.length(needle), acc,        a => tx.split.go(f, needle, t, a), u => tx.split.go(f, needle, String.drop(SCon{h, t}, String.length(needle)), ""))def tx.split.on(+needle: String, +s: String) -> List<&2, String>:  tx.split.go(String.length(s), needle, s, "")def tx.replace(+needle: String, +rep: String, s: String) -> String:  String.join(tx.split.on(needle, s), rep)def tx.digits(s: String) -> Bool:  match s:    case SNil{}:      True{}    case SCon{Chr{+c}, t}:      U32.is_ge(c, 48) && (U32.is_le(c, 57) && tx.digits(t))# T.lines, then each line stripped of its start's white spacedef tx.strip.lines(ls: List<&2, String>) -> List<&2, String>:  match ls:    case Nil{}:      []    case Con{l, t}:      tx.strip.start(l) <> tx.strip.lines(t)