isposixname
function isposixname( name ) --> res, i
Description
Checks if name contains only characters that are specified in the Portable Filename Character Set.
The allowed characters are:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 . _ -
Parameters
- name
-
The name that should be checked.
Return Values
- res
-
true if the value is not empty and contains only allowed characters, otherwise false.
- i
-
The position of the first not allowed character in the name, otherwise nil.
Code
--ZFUNC-isposixname-v1 local function isposixname( name ) --> res, i if #name == 0 then return false, 1 end local index = string.find( name, "[^A-Za-z0-9%._%-]" ) if index then return false, index else return true, nil end end return isposixname
Examples
local t = require( "taptest" ) local isposixname = require( "isposixname" ) res, i = isposixname( "x" ) t( res, true ) t( i, nil ) res, i = isposixname( "." ) t( res, true ) t( i, nil ) res, i = isposixname( ".." ) t( res, true ) t( i, nil ) res, i = isposixname( "" ) t( res, false ) t( i, 1 ) res, i = isposixname( " " ) t( res, false ) t( i, 1 ) res, i = isposixname( ":" ) t( res, false ) t( i, 1 ) res, i = isposixname( "-" ) t( res, true ) t( i, nil ) res, i = isposixname( "foo bar" ) t( res, false ) t( i, 4 ) res, i = isposixname( "foo.bar" ) t( res, true ) t( i, nil ) res, i = isposixname( "foo.barf" ) t( res, true ) t( i, nil ) res, i = isposixname( ".foo" ) t( res, true ) t( i, nil ) res, i = isposixname( "foo." ) t( res, true ) t( i, nil ) t()