~/bend-docscommunity

src/str.bend checks

raw source on the hub · import ezsys@0.2.0.0/src/str.bend as Str

ezsys/src/str: text helpers that Python's str gives a service for free: strip with Python's whitespace, replace, split and find on a text separator, lower and upper case for ASCII, Latin-1, Latin Extended-A, Greek and Cyrillic, padding and truncation by code point, and the numeric order of digit strings. Pure; every function walks its text once, left to right, one code point a step.

1 import
import Base

Types

type Rule source · line 220 · raw

Data

a run of code points lo to hi, every step-th one from lo, mapped in order onto the code points from to

type Multi source · line 224 · raw

Data

a code point that maps to more than one

Definitions

def text.spaces source · line 14 · raw

List<&2, U32>

Python's whitespace (str.isspace, str.strip with no argument): the code points 9 to 13, 28 to 32, 133, 160, 5760, 8192 to 8202, 8232, 8233, 8239, 8287 and 12288

def text.space source · line 20 · raw

@chr:Char -> Bool

whether a character is Python whitespace

def text.lstrip.if source · line 27 · raw

@sp:Bool -> @hd:Char -> @tl:String -> @more:(@_:Unit -> String) -> String

a character at the start: dropped when it is whitespace, and the rest stripped too; else the text is kept from it

def text.lstrip source · line 40 · raw

@txt:String -> String

the text with its leading whitespace removed

def text.rstrip.lone source · line 48 · raw

@sp:Bool -> @hd:Char -> String

a lone last character: gone when it is whitespace

def text.rstrip.put source · line 57 · raw

@+hd:Char -> @rule:String -> String

a character before the stripped rest: when nothing is left after it, it goes too if it is whitespace

def text.rstrip source · line 65 · raw

@txt:String -> String

the text with its trailing whitespace removed

def text.strip source · line 73 · raw

@txt:String -> String

the text with whitespace removed from both ends

def text.after source · line 79 · raw

@sep:String -> Nat

how many characters of a separator follow its first

def text.replace.at source · line 88 · raw

@hit:Bool -> @hd:Char -> @+new:String -> @+left:Nat -> @next:(@_:Nat -> String) -> String

a nonempty old at this character, or not: write new and pass over the rest of old, or write the character

def text.replace.go source · line 103 · raw

@txt:String -> @skip:Nat -> @+old:String -> @+new:String -> String

the scan for a nonempty old; skip characters of a found old are still to pass over

def text.replace.gaps source · line 120 · raw

@txt:String -> @+new:String -> String

an empty old: new after every character

def text.replace source · line 129 · raw

@txt:String -> @+old:String -> @+new:String -> String

every old replaced by new, left to right, never overlapping; an empty old puts new before every character and at the end

def text.split.push source · line 139 · raw

@hd:Char -> @ps:List<&2, String> -> List<&2, String>

a character on the front of the first piece

def text.split.at source · line 147 · raw

@hit:Bool -> @hd:Char -> @+left:Nat -> @next:(@_:Nat -> List<&2, String>) -> List<&2, String>

a separator at this character ends a piece; else the character starts it

def text.split.go source · line 161 · raw

@txt:String -> @skip:Nat -> @+sep:String -> List<&2, String>

the scan for a nonempty separator; skip characters of a found separator are still to pass over

def text.split source · line 174 · raw

@txt:String -> @+sep:String -> List<&2, String>

the pieces between the separators, in order; an empty separator splits nothing

def text.find.inc source · line 184 · raw

@got:Maybe<&2, U32> -> Maybe<&2, U32>

a place one character further on

def text.find.at source · line 192 · raw

@here:Bool -> @rest:(@_:Unit -> Maybe<&2, U32>) -> Maybe<&2, U32>

the needle here, or further on

def text.find.end source · line 200 · raw

@needle:String -> Maybe<&2, U32>

at the end of the text only an empty needle is found

def text.find source · line 208 · raw

@txt:String -> @+needle:String -> Maybe<&2, U32>

the index, in code points, of the first place the needle starts

def text.case.covers source · line 228 · raw

@rule:Rule -> @+cp:U32 -> Bool

whether a rule maps the code point

def text.case.target source · line 235 · raw

@rule:Rule -> @+cp:U32 -> U32

where a rule maps a code point it covers

def text.case.pick source · line 241 · raw

@hit:Bool -> @+rule:Rule -> @+cp:U32 -> @rest:(@_:Unit -> U32) -> U32

the first rule's mapping, or the rest's

def text.case.apply source · line 255 · raw

@rules:List<&2, Rule> -> @+cp:U32 -> U32

the code point the first rule that covers it maps it to; itself when none does

def text.case.found source · line 264 · raw

@hit:Bool -> @to:String -> @rest:(@_:Unit -> Maybe<&2, String>) -> Maybe<&2, String>

this entry's text, or the rest's

def text.case.multi source · line 276 · raw

@ms:List<&2, Multi> -> @+cp:U32 -> Maybe<&2, String>

the text a code point that maps to more than one maps to

def text.case.one source · line 285 · raw

@got:Maybe<&2, String> -> @+cp:U32 -> @rules:List<&2, Rule> -> String

a multiple mapping's text, or the rules' one code point

def text.case.of source · line 297 · raw

@ms:List<&2, Multi> -> @rules:List<&2, Rule> -> @+cp:U32 -> String

a code point mapped by a table: its multiple mapping, else the rules

def text.lower.rules source · line 306 · raw

List<&2, Rule>

Unicode 13's lowercase mappings (Python's str.lower) of U+0000 to U+017F and U+0370 to U+04FF that are one code point

def text.lower.multi source · line 343 · raw

List<&2, Multi>

the lowercase mapping of U+0130 is two code points

def text.upper.rules.latin source · line 347 · raw

List<&2, Rule>

the uppercase rules of U+0000 to U+017F

def text.upper.rules.greek source · line 364 · raw

List<&2, Rule>

the uppercase rules of U+0370 to U+04FF, Greek and Cyrillic

def text.upper.rules source · line 400 · raw

List<&2, Rule>

Unicode 13's uppercase mappings (Python's str.upper) of U+0000 to U+017F and U+0370 to U+04FF that are one code point

def text.upper.multi source · line 405 · raw

List<&2, Multi>

the uppercase mappings of U+00DF, U+0149, U+0390 and U+03B0 are more than one code point

def text.lower.char source · line 414 · raw

@chr:Char -> String

one character in lowercase

def text.upper.char source · line 420 · raw

@chr:Char -> String

one character in uppercase

def text.lower source · line 426 · raw

@txt:String -> String

the text in lowercase, character by character

def text.upper source · line 434 · raw

@txt:String -> String

the text in uppercase, character by character

def text.pad_right source · line 445 · raw

@txt:String -> @width:Nat -> String

the text with spaces after it up to n code points; a longer text is kept whole

def text.truncate source · line 455 · raw

@txt:String -> @width:Nat -> String

the first n code points of the text, or all of it when it is shorter

def text.digits.zero.if source · line 467 · raw

@zero:Bool -> @hd:Char -> @tl:String -> @more:(@_:Unit -> String) -> String

a leading '0' is dropped, and the zeros after it

def text.digits.trim source · line 480 · raw

@txt:String -> String

the text with its leading '0's removed

def text.digits.by source · line 490 · raw

@chr:Cmp -> @+lhs:String -> @+rhs:String -> Cmp

the longer text is the greater; texts of one length compare code point by code point

def text.digits.cmp source · line 501 · raw

@lhs:String -> @rhs:String -> Cmp

two digit strings in numeric order: leading zeros dropped, then the longer is the greater, and two of one length compare digit by digit