Base64, Hex and Utf8
import "std/data" (included in import "std") · namespace std.data
Encodings that turn bytes into text and back, and helpers for working with UTF-8 text by character.
Base64represents binary data with 64 safe characters: for embedding data in JSON, URLs, e-mail or HTTP headers.encodeUrluses the URL- and filename-safe alphabet without padding.Hexwrites each byte as two hexadecimal digits.Utf8counts, splits and slices text by character (code point) rather than by byte, which matters for anything beyond plain ASCII.
import "std"
fn main() {
let token = Base64.encode("user:secret")
print(token)
print(Base64.decode(token))
print(Base64.encodeUrl("??>>"))
print(Hex.encode("Hi!"))
print(Hex.decode("47726176"))
print(Utf8.count("naïve"))
print("naïve".length)
print(Utf8.chars("añb"))
print(Utf8.codePoints("€"))
print(Utf8.fromCodePoint(128512))
print(Utf8.reverse("añb"))
}dXNlcjpzZWNyZXQ=
user:secret
Pz8-Pg
486921
Grav
5
6
["a", "ñ", "b"]
[8364]
😀
bña- Decoding text that is not valid Base64 or hex throws
ValueError;Base64.isValidchecks without throwing. Base64 decoding accepts both alphabets, with or without padding, and ignores whitespace. decodereturns astring; for data that may contain zero bytes usedecodeBytes, which returnsBytes.Utf8.codePointsgives -1 for a byte sequence that is not valid UTF-8, andUtf8.isValidchecks a whole string.
Base32
Base32.encode(bytes) and Base32.decode(text) (RFC 4648: A-Z and 2-7), the alphabet authenticator apps use for one-time password secrets. Decoding takes either case and skips spaces, dashes and = padding; Base32.encode(bytes, true) pads to a multiple of 8.
API
class Base64
Base64: binary data as text (RFC 4648), standard or URL-safe.
Static members
static fn encode(s: string) -> string— The Base64 text of a string's bytes, with = padding.static fn encodeBytes(b: Bytes) -> string— The Base64 text of raw bytes, with = padding.static fn encodeUrl(s: string) -> string— URL- and filename-safe Base64 (- and _ instead of + and /, no padding).static fn encodeBytesUrl(b: Bytes) -> string— Raw bytes as URL- and filename-safe Base64 (- and _, no padding), as JWTs and JWKs write them.static fn decode(s: string) -> string— The text that Base64sencodes (standard or URL-safe; padding and whitespace optional); ValueError whensis not Base64. Use decodeBytes for binary data.static fn decodeBytes(s: string) -> Bytes— The bytes that Base64sencodes; ValueError whensis not Base64.static fn isValid(s: string) -> bool— Whethersis valid Base64.
class Base32
Base32 (RFC 4648: A-Z and 2-7), the alphabet authenticator apps use for secrets: case-insensitive and easy to read out or type.
Static members
static fn encode(b: Bytes, padding: bool = false) -> string— The Base32 text of raw bytes (no padding unlesspadding).static fn encodeString(s: string, padding: bool = false) -> string— The Base32 text of a string's bytes.static fn decode(s: string) -> Bytes— The bytes of Base32 text (either case; spaces, dashes and = padding ignored); ValueError when it is not Base32.static fn isValid(s: string) -> bool— Whethersis valid Base32.
class Hex
Hexadecimal: each byte as two hex digits.
Static members
static fn encode(s: string) -> string— The hex digits of a string's bytes, lowercase: Hex.encode("Hi") == "4869".static fn encodeUpper(s: string) -> string— The same in uppercase.static fn encodeBytes(b: Bytes) -> string— The hex digits of raw bytes, lowercase.static fn decode(s: string) -> string— The text the hex digits encode; ValueError for an odd length or a non-hex digit.static fn decodeBytes(s: string) -> Bytes— The bytes the hex digits encode; ValueError for an odd length or a non-hex digit.
class Utf8
UTF-8 text by character (code point) rather than by byte.
Static members
static fn count(s: string) -> int— The number of characters (code points); "héllo" has 5 (and 6 bytes).static fn chars(s: string) -> Array<string>— The characters, each as a one-character string.static fn codePoints(s: string) -> Array<int>— The Unicode code point of each character (-1 for an invalid byte sequence).static fn fromCodePoint(cp: int) -> string— The one-character string for a code point (U+FFFD for an invalid one).static fn fromCodePoints(cps: Array<int>) -> string— The string made of these code points.static fn isValid(s: string) -> bool— Whethersis well-formed UTF-8.static fn slice(s: string, start: int, end: int) -> string— The characters from indexstartup to (not including)end, counted in characters.static fn reverse(s: string) -> string— The characters in reverse order (unlike Str.reverse, which reverses bytes).