[Issue 7002] std.path needs a isValidFilePath function

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Nov 27 23:28:51 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=7002



--- Comment #9 from Jonathan M Davis <jmdavisProg at gmx.com> 2011-11-27 23:27:50 PST ---
> Wrong. A forward slash is a valid directory separator on Windows, your assert
> will pass for this path even though it *can't* be a file path specifier on
> windows

Then use

assert(isValidPath(path) && !isDirSeparator(path[$-]);

> Of course I don't do it with *just* a syntactical check, but if I can possibly
> reject a string as syntactically invalid, I'd rather do that than wait for the
> OS to tell me if it's a file or directory. 

> This could be a useful optimization technique, e.g. if you're working on a
> large list of paths and you need to quickly verify that they're all
> syntactically file paths.

I'd argue that std.file.isFile is fast enough given that you're going to be
doing I/O anyway and that you're going to need to use it regardless if you want
to verify that a path is a valid file, since the syntactic check is not enough.
So, the extra benefit of verifying that the path doesn't end with a directory
separator is minimal. And if it matters, then do the check that I suggested
above. Personally, I just always use std.file.exists and std.file.isFile. If
you want to avoid trying to open invalid files, you always have to call them
before opening any file anyway.

I seriously question that needing to verify whether a path ends in a directory
separator deserves its own function - even when coupled with verifying that the
path doesn't contain any invalid characters. It's simple enough to do with two
checks if you actually need it, and in the general case, it should be
completely unnecessary, since there are other checks that you need to done
anyway which will catch it. It strikes me as a minor optimization which is
completely unnecessary in the general case.

There's nothing wrong with an enhancement request for it, but I don't think
that such a function merits being added to std.path.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list