Skip to content
FutureBlitz

    Install

    Import std.text.encoding

    std.text.encoding

    std.text.encoding: text to and from bytes in encodings other than a String's own UTF-8 (UTF-16 and UTF-32, little- and big-endian, ASCII, Latin-1, and Windows-1252), with the byte order mark (BOM) that starts some files found or written; bytes as text and back (Base64, Hex, and Base32); and text escaped for a URL, for HTML, or as a FutureBlitz string literal writes it. What decodes gives Null for input it can't decode.

    std/text/encoding.fb

    Types

    TextEncoding enum

    Public Enum TextEncoding

    The encodings Encode and Decode take, beside UTF-8.

    std/text/encoding.fb, line 20

    • UTF8 enum member #

      UTF-8.

    • UTF16_LE enum member #

      UTF-16, its lowest byte first.

    • UTF16_BE enum member #

      UTF-16, its highest byte first.

    • UTF32_LE enum member #

      UTF-32, its lowest byte first.

    • UTF32_BE enum member #

      UTF-32, its highest byte first.

    • ASCII enum member #

      ASCII, 7 bits.

    • LATIN1 enum member #

      ISO 8859-1, Latin-1.

    • WINDOWS_1252 enum member #

      Windows's code page 1252.

    Base64 struct

    Public Struct Base64

    Base64, which writes bytes as text of 64 characters and back: each 3 bytes as 4 characters of "A" to "Z", "a" to "z", "0" to "9", "+", and "/", or "-" and "_" for those two in its URL-safe form, which URLs and file names can hold, and "=" filling out the last 4.

    std/text/encoding.fb, line 216

    • Public Static Method Encode(bytes: UInt8[], urlSafe: Bool = False, padding: Bool = True): String static method #

      bytes as Base64, URL-safe when urlSafe says, and filled out with "=" when padding says.

    • Public Static Method Decode(text: String, urlSafe: Bool = False): UInt8[]? static method #

      The bytes Base64 text holds, in its URL-safe form when urlSafe says, with "=" filling out its end or not, or Null when it isn't Base64.

    Hex struct

    Public Struct Hex

    Hex, which writes each byte as two of the digits "0" to "9" and "a" to "f".

    std/text/encoding.fb, line 255

    • Public Static Method Encode(bytes: UInt8[], upper: Bool = False): String static method #

      bytes as hex, its digits from "A" to "F" when upper says.

    • Public Static Method Decode(text: String): UInt8[]? static method #

      The bytes hex text holds, two digits each, in either case, or Null when it isn't hex.

    Base32 struct

    Public Struct Base32

    Base32, as RFC 4648 has it, which writes each 5 bytes as 8 of the characters "A" to "Z" and "2" to "7", and "=" filling out the last 8, for text that's read aloud or typed, whatever its case.

    std/text/encoding.fb, line 290

    • Public Static Method Encode(bytes: UInt8[], padding: Bool = True): String static method #

      bytes as Base32, filled out with "=" when padding says.

    • Public Static Method Decode(text: String): UInt8[]? static method #

      The bytes Base32 text holds, in either case, with "=" filling out its end or not, or Null when it isn't Base32.

    Percent struct

    Public Struct Percent

    Percent-encoding, which a URL's parts are in: each byte of text's UTF-8, but for the letters, digits, "-", ".", "_", and "~", as "%" and two hex digits.

    std/text/encoding.fb, line 330

    • Public Static Method Encode(text: String, keep: String = ""): String static method #

      text percent-encoded, with each ASCII character in keep, like "/" for a path, kept as it is.

    • Public Static Method EncodeBytes(bytes: UInt8[], keep: String = ""): String static method #

      bytes percent-encoded, with each byte of an ASCII character in keep kept as it is.

    • Public Static Method Decode(text: String): String? static method #

      The text percent-encoded text stands for, or Null when a "%" isn't followed by two hex digits, or the bytes aren't UTF-8.

    • Public Static Method DecodeBytes(text: String): UInt8[]? static method #

      The bytes percent-encoded text stands for, or Null when a "%" isn't followed by two hex digits.

    Html struct

    Public Struct Html

    HTML's escaping, for text put in a page or an attribute's value.

    std/text/encoding.fb, line 394

    • Public Static Method Escape(text: String): String static method #

      text with "&", "<", ">", '"', and "'" as entities, so HTML shows it as it is.

    • Public Static Method Unescape(text: String): String static method #

      text with its entities as the characters they stand for: "&", "<", ">", """, "'", " ", and numbers like "é" and "é", one that isn't a Unicode scalar value as U+FFFD. Another "&" stays as it is.

    Functions

    Encode function

    Public Function Encode(text: String, encoding: TextEncoding, bom: Bool = False): UInt8[]?

    text in encoding, after the encoding's BOM when bom says and it has one, or Null when a character of the text isn't in the encoding: ASCII has U+0000 to U+007F, Latin-1 U+0000 to U+00FF, and Windows-1252 Latin-1's but U+0080 to U+009F, where it has others, like "€".

    std/text/encoding.fb, line 107

    Decode function

    Public Function Decode(bytes: UInt8[], encoding: TextEncoding): String?

    The text bytes hold in encoding, or Null when they aren't that: ASCII with a byte past 0x7F, or UTF-8, UTF-16, or UTF-32 that isn't well-formed, like half a surrogate pair. A BOM at their start stays, as U+FEFF; DecodeAny takes it off.

    std/text/encoding.fb, line 142

    DetectBom function

    Public Function DetectBom(bytes: UInt8[]): TextEncoding?

    The encoding a BOM at the start of bytes is for, or Null when they start with none. UTF-32 LE's BOM starts as UTF-16 LE's does, so it's looked for first.

    std/text/encoding.fb, line 195

    DecodeAny function

    Public Function DecodeAny(bytes: UInt8[], fallback: TextEncoding = TextEncoding.UTF8): String?

    The text bytes hold: in the encoding their BOM is for, without it, or else in fallback; or Null when they aren't that encoding.

    std/text/encoding.fb, line 207

    Escape function

    Public Function Escape(text: String): String

    text as a FutureBlitz string literal's contents write it, by spec 01's escapes, without the quotes: a backslash, '"', or "'" after a backslash; a tab, line break, carriage return, or NUL as "\t", "\n", "\r", or "\0"; another control character as "\x" and two hex digits, or past U+007F as "\u{...}"; and the rest as it is. A C literal reads them differently: its "\x" takes every hex digit after it, and "\0" before a digit is octal.

    std/text/encoding.fb, line 446

    Unescape function

    Public Function Unescape(text: String): String?

    The text that escaped text stands for, as Escape writes it: "\n", "\t", "\r", "\0", "\", """, and "'", "\x" and two hex digits from 00 to 7F, and "\u{...}" with a Unicode scalar value's hex digits; or Null for another backslash, or one at the end.

    std/text/encoding.fb, line 478

    Report an issue with this page or with FutureBlitz on GitHub.