isalnum
function isalnum( str ) --> res, idx
Description
Checks if all characters in the string are alphanumerical ASCII characters and there is at least one character.
Parameters
- str
-
The string that should be checked.
Return Values
- res
-
true if all characters in the string are alphabetical ASCII characters or decimal digits and ther is at least one character, otherwise false.
- idx
-
If res is false shows idx the first character that is not an alphanumerical character.
Code
--ZFUNC-isalnum-v1 local function isalnum( str ) --> res, idx local n = #str if n == 0 then return false, 0 end local b0, b9 = string.byte( "09", 1, 2 ) local ba, bz = string.byte( "az", 1, 2 ) local bA, bZ = string.byte( "AZ", 1, 2 ) for idx = 1, n do local b = string.byte( str:sub( idx, idx ) ) if b >= b0 and b <= b9 then elseif b >= ba and b <= bz then elseif b >= bA and b <= bZ then else return false, idx end end return true end return isalnum
Examples
local t = require( "taptest" ) local isalnum = require( "isalnum" ) t( isalnum( "abc123ABC" ), true ) res, pos = isalnum( "" ) t( res, false ) t( pos, 0 ) res, pos = isalnum( "abc 123 ABC" ) t( res, false ) t( pos, 4 ) res, pos = isalnum( "abc123-ABC" ) t( res, false ) t( pos, 7 ) t()