iscntrl
function iscntrl( str ) --> res, idx
Description
Checks if all characters in the string are control characters that do not occupy a printing position on a display. Control characters are the ASCII codes between 0x00(NUL) and 0x1f(US), plus 0x7f(DEL).
Parameters
- str
-
The string that should be checked.
Return Values
- res
-
true if all characters in the string are control characters and their is at least one character, otherwise false.
- idx
-
If res is false shows idx the first character that is not a control character.
Code
--ZFUNC-iscntrl-v1 local function iscntrl( str ) --> res, idx local n = #str if n == 0 then return false, 0 end for idx = 1, n do local b = string.byte( str:sub( idx, idx ) ) if b < 0x1f or b == 0x7f then else return false, idx end end return true end return iscntrl
Examples
local t = require( "taptest" ) local iscntrl = require( "iscntrl" ) local NUL = string.char( 0x00 ) local SOH = string.char( 0x01 ) local STX = string.char( 0x02 ) local ETX = string.char( 0x03 ) local EOT = string.char( 0x04 ) local ENQ = string.char( 0x05 ) local ACK = string.char( 0x06 ) local BEL = string.char( 0x07 ) local BS = string.char( 0x08 ) local HT = string.char( 0x09 ) local LF = string.char( 0x0a ) local VT = string.char( 0x0b ) local FF = string.char( 0x0c ) local CR = string.char( 0x0d ) local SO = string.char( 0x0e ) local SI = string.char( 0x0f ) local DLE = string.char( 0x10 ) local DC1 = string.char( 0x11 ) local DC2 = string.char( 0x12 ) local DC3 = string.char( 0x13 ) local DC4 = string.char( 0x14 ) local NAK = string.char( 0x15 ) local SYN = string.char( 0x16 ) local ETB = string.char( 0x17 ) local CAN = string.char( 0x18 ) local EM = string.char( 0x19 ) local SUB = string.char( 0x1a ) local ESC = string.char( 0x1b ) local FS = string.char( 0x1c ) local GS = string.char( 0x1d ) local RS = string.char( 0x1e ) local US = string.char( 0x1f ) local DEL = string.char( 0x7f ) t( iscntrl( NUL..SOH..DEL..ETB ), true ) res, pos = iscntrl( "" ) t( res, false ) t( pos, 0 ) res, pos = iscntrl( NUL..SOH.."1"..DEL ) t( res, false ) t( pos, 3 ) t()