encode

Encode and decode data across common formats: base64, hex, CSV, big-endian binary integers and floats, plus JSON and XML helpers. Import it with import encode.

encode.base64

FunctionSignatureDescription
encodeencode.base64.encode(s: string) → stringStandard base64-encode a string
decodeencode.base64.decode(s: string) → stringDecode a standard base64 string
encodeURLencode.base64.encodeURL(s: string) → stringURL-safe base64-encode a string
decodeURLencode.base64.decodeURL(s: string) → stringDecode a URL-safe base64 string

encode.hex

FunctionSignatureDescription
encodeencode.hex.encode(s: string) → stringHex-encode a string
decodeencode.hex.decode(s: string) → stringDecode a hex string

encode.csv

FunctionSignatureDescription
encodeRowsencode.csv.encodeRows(rows: string) → stringEncode CSV from a JSON string holding an array of string arrays
decodeAllencode.csv.decodeAll(data: string) → stringParse CSV text into a JSON string of an array of string arrays
encodeRowencode.csv.encodeRow(row: string[]) → stringEncode a single CSV row (no trailing newline)

encode.binary

All integers and floats use 8-byte (or 4-byte) big-endian encoding, stored as the bytes of a string.

FunctionSignatureDescription
encodeInt32encode.binary.encodeInt32(n: int) → stringEncode an int as 4 big-endian bytes
encodeInt64encode.binary.encodeInt64(n: int) → stringEncode an int as 8 big-endian bytes
decodeInt32encode.binary.decodeInt32(s: string) → intDecode 4 big-endian bytes (0 if too short)
decodeInt64encode.binary.decodeInt64(s: string) → intDecode 8 big-endian bytes (0 if too short)
encodeFloat64encode.binary.encodeFloat64(f: float) → stringEncode a float as 8-byte IEEE 754 big-endian
decodeFloat64encode.binary.decodeFloat64(s: string) → floatDecode 8-byte IEEE 754 big-endian (0 if too short)

encode.json

FunctionSignatureDescription
parseencode.json.parse(s: string) → anyParse JSON into a navigable value tree
stringifyencode.json.stringify(v: any, indent?: string) → stringSerialize a value to JSON, optionally indented
prettyencode.json.pretty(s: string) → stringRe-indent a JSON string with two-space indentation
validencode.json.valid(s: string) → boolReport whether the string is valid JSON
keysencode.json.keys(v: any) → string[]Sorted field names of an object or JSON object string

encode.xml

FunctionSignatureDescription
validencode.xml.valid(s: string) → boolReport whether the string is well-formed XML
prettyencode.xml.pretty(s: string) → stringReturn the XML string (pass-through)

Example

import encode

// base64 round-trip
let enc = encode.base64.encode("hello")
print(encode.base64.decode(enc))   // hello

// build CSV from rows
let csv = encode.csv.encodeRows("[[\"name\",\"age\"],[\"ada\",\"36\"]]")
print(csv)

// JSON helpers
print(encode.json.valid("{\"a\":1}"))   // true
let obj = encode.json.parse("{\"a\":1,\"b\":2}")
print(encode.json.keys(obj))            // ["a", "b"]
Standard library · View as Markdown · llms-full.txt