Character literals
Character literals are enclosed in single quotes ‘’ and can contain the same escape sequences as strings - with one exception: the platform dependent newline (\p) is not allowed as it may be wider than one character (it can be the pair CR/LF). Here are the valid escape sequences for character literals:
Escape sequence | Meaning |
---|---|
\r, \c | carriage return |
\n, \l | line feed |
\f | form feed |
\t | tabulator |
\v | vertical tabulator |
\ | backslash |
\” | quotation mark |
\’ | apostrophe |
\ ‘0’..’9’+ | character with decimal value d; all decimal digits directly following are used for the character |
\a | alert |
\b | backspace |
\e | escape [ESC] |
\x HH | character with hex value HH; exactly two hex digits are allowed |
A character is not a Unicode character but a single byte.
Rationale: It enables the efficient support of array[char, int] or set[char].
The Rune type can represent any Unicode character. Rune is declared in the unicode module.
A character literal that does not end in ‘ is interpreted as ‘ if there is a preceding backtick token. There must be no whitespace between the preceding backtick token and the character literal. This special case ensures that a declaration like proc `‘customLiteral`(s: string) is valid. proc `‘customLiteral`(s: string) is the same as proc `‘\‘’customLiteral`(s: string).
See also custom numeric literals.