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( "tapered" )
local isposixname = require( "isposixname" )

res, i = isposixname( "x" )
t.ok( res )
t.is( nil, i )

res, i = isposixname( "." )
t.ok( res )
t.is( nil, i )

res, i = isposixname( ".." )
t.ok( res )
t.is( nil, i )

res, i = isposixname( "" )
t.nok( res )
t.is( 1, i )

res, i = isposixname( " " )
t.nok( res )
t.is( 1, i )

res, i = isposixname( ":" )
t.nok( res )
t.is( 1, i )

res, i = isposixname( "-" )
t.ok( res )
t.is( nil, i )

res, i = isposixname( "foo bar" )
t.nok( res )
t.is( 4, i )

res, i = isposixname( "foo.bar" )
t.ok( res )
t.is( nil, i )

res, i = isposixname( "foo.barf" )
t.ok( res )
t.is( nil, i )

res, i = isposixname( ".foo" )
t.ok( res )
t.is( nil, i )

res, i = isposixname( "foo." )
t.ok( res )
t.is( nil, i )

t.done()

See also