Characters
A character, char
, is an unsigned 32-bit integer representing a Unicode code point.
-
$char(arg: i64) -> char
-
Converts
arg
to achar
. Panics if the argument is out of range.
Character Literals
Character literals use single quotes:
var c1 = 'a'; var c2 = '€'; var c3 = '🔥';
A character literal should contain either a single UTF-8 encoded code point or a backslashed escape sequence representing a code point.
assert '\x61' == 'a'; assert '\u20AC' == '€';
Character literals support the same backslashed escape sequences as strings.
Conversions
You can convert a character to a string using the $str()
function — this returns a string containing the UTF-8 encoded representation of the codepoint:
assert $str('a') == "a"; assert $str('🔥') == "🔥"; assert $str('🔥') == "\xF0\x9F\x94\xA5";
You can convert a character to an integer using the $i64()
function or an integer to a character using the $char()
function:
assert $i64('a') == 97; assert $char(97) == 'a';
Concatenating
If you add two characters together using the +
operator, the result will be a UTF-8 encoded string:
assert 'x' + 'y' == "xy";
Similarly, you can prepend or append a character to a string using the +
operator:
assert 'x' + "yz" == "xyz"; assert "xy" + 'z' == "xyz";
You can multiply a character by an integer n
to produce a string with n
UTF-8 encoded copies of the character:
assert 'x' * 3 == "xxx"; assert '🔥' * 3 == "🔥🔥🔥";
Methods
-
:is_ascii() -> bool
-
Returns
true
if the character is in the ASCII range. -
:is_ascii_decimal() -> bool
-
Returns
true
if the character is an ASCII decimal digit. -
:is_ascii_hex() -> bool
-
Returns
true
if the character is an ASCII hexadecimal digit. -
:is_ascii_octal() -> bool
-
Returns
true
if the character is an ASCII octal digit. -
:is_ascii_ws() -> bool
-
Returns
true
if the character is an ASCII whitespace character. -
:is_unicode_ws() -> bool
-
Returns
true
if the character is a Unicode whitespace character.