Encodings
Source: lib/encoding.x
Guide: Time, Random Values, and Encodings
UTF-8 code points and base64, hex, and percent encodings.
Include it with #include "encoding.x".
Primary API
| Function | Summary |
|---|---|
Block.base64_encode | Returns the bytes of bytes in standard base64 with = padding; each element contributes all of its bytes. |
Block.base64url_encode | Returns the bytes of bytes in URL-safe base64 without padding. |
Block.hex_encode | Returns the bytes of b in lowercase hexadecimal; each element contributes all of its bytes. |
Buffer.write_code_point | Appends the UTF-8 encoding of point to out and returns out. |
String.base64_decode | Returns the text that the padded standard base64 text encodes. |
String.base64_decode_bytes | Returns the bytes that the padded standard base64 text encodes, as a Block of 1-byte elements owned by the active Scope. |
String.base64_encode | Returns the bytes of text in standard base64, from RFC 4648 section 4, with = padding. |
String.base64url_decode | Returns the text that the URL-safe base64 text encodes; = padding is optional. |
String.base64url_decode_bytes | Returns the bytes that the URL-safe base64 text encodes, as a Block of 1-byte elements owned by the active Scope. |
String.base64url_encode | Returns the bytes of text in the URL-safe base64 alphabet of RFC 4648 section 5, which writes - and _ for + and /, without padding. |
String.code_point_count | Returns the number of code points that text encodes in UTF-8. |
String.code_points | Initializes dest as a lazy iterator over the code points of s, each yielded as an int, so foreach (int point in s.code_points()) walks them. |
String.from_code_point | Returns the String that encodes the one code point point in UTF-8. |
String.hex_decode | Returns the text that the hexadecimal text encodes, in digits of either case. |
String.hex_decode_bytes | Returns the bytes that the hexadecimal text encodes, as a Block of 1-byte elements owned by the active Scope. |
String.hex_encode | Returns the bytes of s in lowercase hexadecimal, two digits per byte. |
String.is_utf8 | Reports whether text is well-formed UTF-8, with no overlong form, surrogate, or code point above U+10FFFF. |
String.percent_decode | Returns text with each % and the two hexadecimal digits after it replaced by the byte they name. |
String.percent_encode | Returns s with each byte outside the RFC 3986 unreserved set A-Z a-z 0-9 - . _ ~ written as % and two uppercase hexadecimal digits, so the result can be one path segment, query name, or query value. |
Block
Block.base64_encode
String Block.base64_encode(Block bytes)
Returns the bytes of bytes in standard base64 with = padding; each
element contributes all of its bytes.
Source: lib/encoding.x
Block.base64url_encode
String Block.base64url_encode(Block bytes)
Returns the bytes of bytes in URL-safe base64 without padding.
Source: lib/encoding.x
Block.hex_encode
String Block.hex_encode(Block b)
Returns the bytes of b in lowercase hexadecimal; each element
contributes all of its bytes.
Source: lib/encoding.x
Buffer
Buffer.write_code_point
Buffer Buffer.write_code_point(Buffer out, int point)
Appends the UTF-8 encoding of point to out and returns out.
Raises: <bad-arg> when point is a surrogate, above U+10FFFF, or not
positive; a String cannot hold U+0000.
Source: lib/encoding.x
String
String.base64_decode
String String.base64_decode(String text)
Returns the text that the padded standard base64 text encodes.
Raises: <malformed> when text is not padded standard base64 or
decodes to a NUL byte; String.base64_decode_bytes returns such bytes.
Source: lib/encoding.x
String.base64_decode_bytes
Block String.base64_decode_bytes(String text)
Returns the bytes that the padded standard base64 text encodes, as a
Block of 1-byte elements owned by the active Scope.
Raises: <malformed> when text is not padded standard base64.
Source: lib/encoding.x
String.base64_encode
String String.base64_encode(String text)
Returns the bytes of text in standard base64, from RFC 4648 section
4, with = padding.
Source: lib/encoding.x
String.base64url_decode
String String.base64url_decode(String text)
Returns the text that the URL-safe base64 text encodes; = padding
is optional.
Raises: <malformed> when text is not URL-safe base64 or decodes to
a NUL byte.
Source: lib/encoding.x
String.base64url_decode_bytes
Block String.base64url_decode_bytes(String text)
Returns the bytes that the URL-safe base64 text encodes, as a Block
of 1-byte elements owned by the active Scope.
Raises: <malformed> when text is not URL-safe base64.
Source: lib/encoding.x
String.base64url_encode
String String.base64url_encode(String text)
Returns the bytes of text in the URL-safe base64 alphabet of RFC 4648
section 5, which writes - and _ for + and /, without padding.
Source: lib/encoding.x
String.code_point_count
int String.code_point_count(String text)
Returns the number of code points that text encodes in UTF-8.
Raises: <malformed> when text is not well-formed UTF-8.
Source: lib/encoding.x
String.code_points
Iter String.code_points(String s, Iter dest)
Initializes dest as a lazy iterator over the code points of s,
each yielded as an int, so foreach (int point in s.code_points())
walks them. The iterator borrows s.
Raises: <malformed> from the pull that reaches bytes that are not
well-formed UTF-8.
Source: lib/encoding.x
String.from_code_point
String String.from_code_point(int point)
Returns the String that encodes the one code point point in UTF-8.
Raises: the causes of Buffer.write_code_point.
Source: lib/encoding.x
String.hex_decode
String String.hex_decode(String text)
Returns the text that the hexadecimal text encodes, in digits of
either case.
Raises: <malformed> when text has an odd length or a character that
is not a hexadecimal digit, or decodes to a NUL byte.
Source: lib/encoding.x
String.hex_decode_bytes
Block String.hex_decode_bytes(String text)
Returns the bytes that the hexadecimal text encodes, as a Block of
1-byte elements owned by the active Scope.
Raises: <malformed> when text has an odd length or a character that
is not a hexadecimal digit.
Source: lib/encoding.x
String.hex_encode
String String.hex_encode(String s)
Returns the bytes of s in lowercase hexadecimal, two digits per
byte.
Source: lib/encoding.x
String.is_utf8
int String.is_utf8(String text)
Reports whether text is well-formed UTF-8, with no overlong form,
surrogate, or code point above U+10FFFF. The empty String is.
Source: lib/encoding.x
String.percent_decode
String String.percent_decode(String text)
Returns text with each % and the two hexadecimal digits after it
replaced by the byte they name. Every other byte, + included, is
kept.
Raises: <malformed> for a % without two hexadecimal digits after
it, or for %00.
Source: lib/encoding.x
String.percent_encode
String String.percent_encode(String s)
Returns s with each byte outside the RFC 3986 unreserved set
A-Z a-z 0-9 - . _ ~ written as % and two uppercase hexadecimal
digits, so the result can be one path segment, query name, or query
value.
Source: lib/encoding.x
Advanced and interop API
| Function | Summary |
|---|---|
String.try_next_code_point | Decodes the code point that starts at byte cursor of text into point, advances cursor past it, and returns one. |
String
String.try_next_code_point
int String.try_next_code_point(String text, int &cursor, int &point)
Decodes the code point that starts at byte cursor of text into
point, advances cursor past it, and returns one. At the end of
text, or for a negative cursor, it returns zero and changes
nothing.
#include "encoding.x"
int main(void) {
String text = "na\xc3\xafve";
int cursor = 0, point;
while (text.try_next_code_point(cursor, point))
printf("U+%04X ends at byte %d\n", point, cursor);
return 0;
}
Raises: <malformed> with cursor as the offset when the bytes there
are not a well-formed UTF-8 sequence.
Source: lib/encoding.x
Design notes
A String holds bytes. The UTF-8 operations read and write the code
points those bytes encode, and the byte encodings turn any bytes into
ASCII text and back. A String cannot hold a NUL byte, so a decoder
that returns a String raises <malformed> for one, and its _bytes
form returns a Block of 1-byte elements instead. Invalid input raises
<malformed> with the operation, a reason, and the input byte offset.