www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Request you advise : isValidFilename

reply Vino <akashvino79 gmail.com> writes:
Hi All,

   Request you help in understanding why the below code is always 
returning true when it should return false as per the 
documentation.

Documentation
```
Checks that the given file or directory name is valid.
The maximum length of filename is given by the constant 
core.stdc.stdio.FILENAME_MAX. (On Windows, this number is defined 
as the maximum number of UTF-16 code points, and the test will 
therefore only yield strictly correct results when filename is a 
string of wchars.)
On Windows, the following criteria must be satisfied (source):
**************
filename must not contain any characters whose integer 
representation is in the range 0-31.
**************
filename must not contain any of the following reserved 
characters: <>:"/\|?*
filename may not end with a space (' ') or a period ('.').
On POSIX, filename may not contain a forward slash ('/') or the 
null character ('\0').
```

Code:
```
import std.stdio: writefln;
import std.path: baseName, isValidFilename;
import std.utf : byCodeUnit;

void main () {
string st1 = "C:\\Windows\\System32\\whoami1";
string st2 = "C:\\Windows\\System32\\whoami*";
string st3 = "C:\\Windows\\System32\\whoami.";
string st4 = "C:\\Windows\\System32\\whoami ";
writefln("(isValidFilename : %s)", 
isValidFilename(baseName(st1).byCodeUnit));
writefln("(isValidFilename : %s)", 
isValidFilename(baseName(st2).byCodeUnit));
writefln("(isValidFilename : %s)", 
isValidFilename(baseName(st3).byCodeUnit));
writefln("(isValidFilename : %s)", 
isValidFilename(baseName(st4).byCodeUnit));
}

```
Output:
```
(isValidFilename : true)  // shouldn't it be false as it 
contain's the number whoami1
(isValidFilename : false)
(isValidFilename : false)
(isValidFilename : false)

```
From,
Vino
Sep 22 2023
parent reply FeepingCreature <feepingcreature gmail.com> writes:
On Friday, 22 September 2023 at 17:44:50 UTC, Vino wrote:
 Hi All,

   Request you help in understanding why the below code is 
 always returning true when it should return false as per the 
 documentation.

 Documentation
 ```
 filename must not contain any characters whose integer 
 representation is in the range 0-31.
 ```
"Integer representation" here refers to the ANSI index or Unicode codepoint, ie. the filename must not contain `\x00` to `\x31`.
Sep 22 2023
parent FeepingCreature <feepingcreature gmail.com> writes:
On Friday, 22 September 2023 at 17:52:51 UTC, FeepingCreature 
wrote:
 "Integer representation" here refers to the ANSI index or 
 Unicode codepoint, ie. the filename must not contain `\x00` to 
 `\x31`.
Er oops, make that `\x00` to `\x1f`.
Sep 22 2023