iswindowsname
function iswindowsname( name ) --> res, i
Description
Checks if name matches the following rules:
-
Does not contain one of the following reserved characters:
-
< (less than)
-
> (greater than)
-
: (colon)
-
" (double quote)
-
/ (forward slash)
-
\ (backslash)
-
| (vertical bar or pipe)
-
? (question mark)
-
* (asterisk)
-
-
Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. For more information about file streams, see File Streams. Any other character that the target file system does not allow.
-
Do not use the following reserved names for the name of a file: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended. For more information, see Namespaces.
-
Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".
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-iswindowsname-v1 local function iswindowsname( name ) --> res, i --ZFUNC-buildset-v1 local function buildset( arr ) local res = {} for _, v in ipairs( arr ) do res[ v ] = true end return res end if #name == 0 then return false, 1 end local index = string.find( name, [=[[%z<>:"/\|?*]]=] ) if not index then index = string.find( name, "^ " ) end if not index then index = string.find( name, " $" ) end if not index then index = string.find( name, "%.$" ) end if not index then local reserved = buildset{ "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" } if reserved[ name ] then return false, 1 end end if index then return false, index else return true, nil end end return iswindowsname
Examples
local t = require( "taptest" ) local iswindowsname = require( "iswindowsname" ) res, i = iswindowsname( "x" ) t( res, true ) t( i, nil ) res, i = iswindowsname( "." ) t( res, false ) t( i, 1 ) res, i = iswindowsname( ".." ) t( res, false ) t( i, 2 ) res, i = iswindowsname( "" ) t( res, false ) t( 1, i, 1 ) res, i = iswindowsname( " " ) t( res, false ) t( i, 1 ) res, i = iswindowsname( ":" ) t( res, false ) t( i, 1 ) res, i = iswindowsname( "-" ) t( res, true ) t( i, nil ) res, i = iswindowsname( "foo bar" ) t( res, true ) t( i, nil ) res, i = iswindowsname( " bar" ) t( res, false ) t( i, 1 ) res, i = iswindowsname( "foo " ) t( res, false ) t( i, 4 ) res, i = iswindowsname( "foo.bar" ) t( res, true ) t( i, nil ) res, i = iswindowsname( "foo.barf" ) t( res, true ) t( i, nil ) res, i = iswindowsname( ".foo" ) t( res, true ) t( i, nil ) res, i = iswindowsname( "foo." ) t( res, false ) t( i, 4 ) res, i = iswindowsname( "CON" ) t( res, false ) t( i, 1 ) res, i = iswindowsname( "CONTI" ) t( res, true ) t( i, nil ) t()