ascii
An ASCII library that provides functions for this encoding.
All functions operate on single bytes (integers in the range 0x00–0xFF), as returned by string.byte, and do not accept nil.
Functions
ascii.fromInteger | Returns the digit byte of the given numerical value (0–35), the inverse of ascii.toInteger. |
ascii.isAlnum | Checks if the byte is an ASCII alphanumeric character (A–Z, a–z, or 0–9). |
ascii.isAlpha | Checks if the byte is an ASCII alphabetic character (a–z or A–Z). |
ascii.isASCII | Checks if the byte is an ASCII character, i.e. in the range 0x00–0x7F. |
ascii.isBinaryDigit | Checks if the byte is an ASCII binary digit (0–1). |
ascii.isBlank | Checks if the byte is an ASCII blank character, i.e. space (0x20) or horizontal tab (0x09). |
ascii.isControl | Checks if the byte is an ASCII control character. |
ascii.isDigit | Checks if the byte is an ASCII decimal digit (0–9). |
ascii.isExtended | Checks if the byte is an extended (non-ASCII) byte, i.e. above 0x7F. |
ascii.isGraphic | Checks if the byte is a visible ASCII character (0x21–0x7E), i.e. printable and not a space. |
ascii.isHexDigit | Checks if the byte is a valid hexadecimal digit (0–9, A–F, a–f). |
ascii.isLower | Checks if the byte is a lowercase ASCII letter (a–z). |
ascii.isMagic | Checks if the byte is a Lua pattern magic character. |
ascii.isOctalDigit | Checks if the byte is an ASCII octal digit (0–7). |
ascii.isPrintable | Checks if the byte is a printable ASCII character (0x20–0x7E), space included. |
ascii.isSpace | Checks if the byte is an ASCII whitespace character. |
ascii.isSpecial | Checks if the byte is an ASCII special (punctuation/symbol) character. |
ascii.isUpper | Checks if the byte is an uppercase ASCII letter (A–Z). |
ascii.isWord | Checks if the byte is an ASCII word character (A–Z, a–z, 0–9 or _), i.e. a character allowed in identifiers. |
ascii.toInteger | Returns the numerical value of the given digit byte (0–9, then a–z/A–Z for 10–35). |
ascii.toLower | Converts an uppercase ASCII letter to lowercase, any other byte is returned as is. |
ascii.toUpper | Converts a lowercase ASCII letter to uppercase, any other byte is returned as is. |
function ascii.fromInteger(value: ascii.integer, uppercase?: boolean): byte | nil
Returns the digit byte of the given numerical value (0–35), the inverse of ascii.toInteger.
Parameters
| Name | Type | Description |
|---|
value | ascii.integer | The digit value to convert. |
uppercaseoptional | boolean | If true, letters are returned in uppercase. Defaults to false. |
Returns
| Type | Name | Description |
|---|
byte | nil | uint8 | The digit byte, or nil if the value is out of range. |
See also
function ascii.isAlnum(uint8: byte): boolean
Checks if the byte is an ASCII alphanumeric character (A–Z, a–z, or 0–9).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_alnum | true if the byte is an ASCII letter or decimal digit. |
function ascii.isAlpha(uint8: byte): boolean
Checks if the byte is an ASCII alphabetic character (a–z or A–Z).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_alpha | true if the byte is a lowercase or uppercase letter. |
function ascii.isASCII(uint8: byte): boolean
Checks if the byte is an ASCII character, i.e. in the range 0x00–0x7F.
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_ascii | true if the byte is not greater than 0x7F. |
function ascii.isBinaryDigit(uint8: byte): boolean
Checks if the byte is an ASCII binary digit (0–1).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_binary_digit | true if the byte is 0 or 1. |
function ascii.isBlank(uint8: byte): boolean
Checks if the byte is an ASCII blank character, i.e. space (0x20) or horizontal tab (0x09).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_blank | true if the byte is a space or a horizontal tab. |
function ascii.isControl(uint8: byte): boolean
Checks if the byte is an ASCII control character. Control characters are non-printable bytes in the range 0x00–0x1F (e.g. NUL, BEL, BS, HT, LF, CR) as well as DEL (0x7F).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_control | true if the byte is a control character. |
function ascii.isDigit(uint8: byte): boolean
Checks if the byte is an ASCII decimal digit (0–9).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_digit | true if the byte is in the range 0x30–0x39. |
function ascii.isExtended(uint8: byte): boolean
Checks if the byte is an extended (non-ASCII) byte, i.e. above 0x7F. These bytes appear in multi-byte UTF-8 sequences and Latin-1 supplements.
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_extended | true if the byte is greater than 0x7F. |
function ascii.isGraphic(uint8: byte): boolean
Checks if the byte is a visible ASCII character (0x21–0x7E), i.e. printable and not a space.
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_graphic | true if the byte has a visible glyph. |
function ascii.isHexDigit(uint8: byte): boolean
Checks if the byte is a valid hexadecimal digit (0–9, A–F, a–f).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_hex_digit | true if the byte is a decimal digit or a hex letter. |
function ascii.isLower(uint8: byte): boolean
Checks if the byte is a lowercase ASCII letter (a–z).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_lower | true if the byte is in the range 0x61–0x7A. |
function ascii.isMagic(uint8: byte): boolean
Checks if the byte is a Lua pattern magic character. Magic characters have special meaning inside Lua string patterns and must be escaped with % when used as literals. The magic set is: ^ $ ( ) % . [ ] * + - ?
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_magic | true if the byte is a Lua pattern magic character. |
function ascii.isOctalDigit(uint8: byte): boolean
Checks if the byte is an ASCII octal digit (0–7).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_octal_digit | true if the byte is in the range 0x30–0x37. |
function ascii.isPrintable(uint8: byte): boolean
Checks if the byte is a printable ASCII character (0x20–0x7E), space included.
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_printable | true if the byte is a printable character. |
function ascii.isSpace(uint8: byte): boolean
Checks if the byte is an ASCII whitespace character. Matches: space (0x20), horizontal tab (0x09), line feed (0x0A), vertical tab (0x0B), form feed (0x0C), and carriage return (0x0D).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_space | true if the byte is a whitespace character, false otherwise. |
function ascii.isSpecial(uint8: byte): boolean
Checks if the byte is an ASCII special (punctuation/symbol) character. Covers the following ranges: ! – / (0x21–0x2F), : – @ (0x3A–0x40), `[ – ` (0x5B–0x60), { – ~` (0x7B–0x7E).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_special | true if the byte is a punctuation or symbol character. |
function ascii.isUpper(uint8: byte): boolean
Checks if the byte is an uppercase ASCII letter (A–Z).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_upper | true if the byte is in the range 0x41–0x5A. |
function ascii.isWord(uint8: byte): boolean
Checks if the byte is an ASCII word character (A–Z, a–z, 0–9 or _), i.e. a character allowed in identifiers.
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to check. |
Returns
| Type | Name | Description |
|---|
boolean | is_word | true if the byte is an ASCII letter, decimal digit or underscore. |
function ascii.toInteger(uint8: byte): ascii.integer | nil
Returns the numerical value of the given digit byte (0–9, then a–z/A–Z for 10–35).
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to convert. |
Returns
| Type | Name | Description |
|---|
ascii.integer | nil | number | The digit value, or nil if the byte is not a valid digit. |
See also
function ascii.toLower(uint8: byte): byte
Converts an uppercase ASCII letter to lowercase, any other byte is returned as is.
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to convert. |
Returns
| Type | Name | Description |
|---|
byte | lower_uint8 | The lowercase byte. |
function ascii.toUpper(uint8: byte): byte
Converts a lowercase ASCII letter to uppercase, any other byte is returned as is.
Parameters
| Name | Type | Description |
|---|
uint8 | byte | The byte to convert. |
Returns
| Type | Name | Description |
|---|
byte | upper_uint8 | The uppercase byte. |