NAME
std/string/encode - Character-encoding conversions between Strings and BinaryStrings.
SYNOPSIS
from std/string/encode import *;
let bytes := encode( "héllo", ENCODING_UTF16 );
let text := decode( bytes, "UTF-16" );
IMPLEMENTATION SUPPORT
This module is supported by all implementations of ZuzuScript.
DESCRIPTION
This module converts between text (String) and encoded bytes (BinaryString).
All implementations support UTF-8, UTF-16, UTF-32, and ISO-8859-1 (Latin-1). Implementations are encouraged to support additional encodings where the host platform makes that practical; programs that need to run on every implementation should restrict themselves to the four required encodings.
Encoding names are matched case-insensitively, so "utf-8" and "UTF-8" are equivalent.
For UTF-16 and UTF-32, encode produces the canonical form: big-endian with no byte order mark. This is deterministic and identical across implementations. decode honours a leading byte order mark (consuming it and switching to little-endian where it says so) and otherwise assumes big-endian input.
Invalid input raises an exception: unknown encoding names, bytes that do not form valid text in the requested encoding, and characters that the target encoding cannot represent (for example, encoding "😀" as ISO-8859-1) all throw.
EXPORTS
Functions
encode(String text, String encoding)Parameters:
textis the text to encode;encodingnames the target encoding and defaults to"UTF-8". Returns:BinaryString. Encodestextas bytes. Throws a TypeException iftextis not aString, and an exception if the encoding is unknown or cannot represent a character intext.decode(BinaryString bytes, String encoding)Parameters:
bytesis the encoded input;encodingnames the source encoding and defaults to"UTF-8". Returns:String. Decodesbytesinto text. Throws a TypeException ifbytesis not aBinaryString, and an exception if the encoding is unknown or the bytes are not valid for it.
Constants
ENCODING_UTF8Type:
String. The value"UTF-8".ENCODING_UTF16Type:
String. The value"UTF-16".ENCODING_UTF32Type:
String. The value"UTF-32".ENCODING_LATINType:
String. The value"ISO-8859-1".
COPYRIGHT AND LICENCE
std/string/encode is copyright Toby Inkster.
It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.