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.
-
UTF-8.
-
UTF-16, its lowest byte first.
-
UTF-16, its highest byte first.
-
UTF-32, its lowest byte first.
-
UTF-32, its highest byte first.
-
ASCII, 7 bits.
-
ISO 8859-1, Latin-1.
-
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): Stringstatic method #bytesas Base64, URL-safe whenurlSafesays, and filled out with "=" whenpaddingsays. -
The bytes Base64
textholds, in its URL-safe form whenurlSafesays, 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
-
bytesas hex, its digits from "A" to "F" whenuppersays. -
The bytes hex
textholds, 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
-
bytesas Base32, filled out with "=" whenpaddingsays. -
The bytes Base32
textholds, 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
-
textpercent-encoded, with each ASCII character inkeep, like "/" for a path, kept as it is. -
bytespercent-encoded, with each byte of an ASCII character inkeepkept as it is. -
The text percent-encoded
textstands for, or Null when a "%" isn't followed by two hex digits, or the bytes aren't UTF-8. -
The bytes percent-encoded
textstands 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
-
textwith "&", "<", ">", '"', and "'" as entities, so HTML shows it as it is. -
textwith 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 "€".
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.
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.
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.
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.
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.
Report an issue with this page or with FutureBlitz on GitHub.