~/bend-docscommunity

src/pct.bend checks

raw source on the hub · import emerging-ezhttp@0.14.0.0/src/pct.bend as Pct

ezhttp/src/pct: percent-encoding (RFC 3986 §2.1), the way Python's urllib.parse.quote does it. Unreserved characters (§2.3) are written as themselves; the path form also keeps "/"; every other character is written as the bytes of its UTF-8 encoding (RFC 3629 §3), each as "%" and two upper-case hex digits. SPEC.md HTTP-URL lists what is guaranteed.

1 import
import Base

Definitions

def unreserved source · line 9 · raw

@+cp:U32 -> Bool

RFC 3986 §2.3 unreserved: ALPHA / DIGIT / "-" / "." / "_" / "~"

def safe source · line 19 · raw

@slash:Bool -> @+cp:U32 -> Bool

a character a form writes as itself: unreserved, or "/" when slash (the path form) keeps it

def hex source · line 23 · raw

@+nib:U32 -> Char

one hex digit, upper case: 0-9 then A-F ('A' is 55 + 10)

def pct source · line 27 · raw

@bytes:List<&2, U32> -> @rest:String -> String

bytes as %XX triples, high nibble first, followed by rest

def cont source · line 36 · raw

@+cp:U32 -> @shift:Nat -> U32

a continuation byte: 10xxxxxx from the six bits at shift

def utf8.four source · line 40 · raw

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

four bytes: U+10000 to U+10FFFF

def utf8.three source · line 44 · raw

@+cp:U32 -> @fits:Bool -> List<&2, U32>

three bytes below U+10000, else four

def utf8.two source · line 52 · raw

@+cp:U32 -> @fits:Bool -> List<&2, U32>

two bytes below U+0800, else more

def utf8.one source · line 60 · raw

@+cp:U32 -> @fits:Bool -> List<&2, U32>

one byte below U+0080, else more

def utf8 source · line 68 · raw

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

the UTF-8 bytes of one code point, in order (RFC 3629 §3)

def one.of source · line 72 · raw

@+cp:U32 -> @rest:String -> @kept:Bool -> String

one character, once it is known whether the form keeps it

def one source · line 80 · raw

@slash:Bool -> @+cp:U32 -> @rest:String -> String

one character as the form writes it, followed by rest

def quote source · line 84 · raw

@+slash:Bool -> @shift:String -> String

a string as the form writes it: each character in turn