--- Comment #8 from Andrej Mitrovic <> 2011-11-27 
23:09:18 PST ---
I think there's some confusion here. When I say "valid file path" I mean
*syntactically* valid, not whether they exist and whether something that looks
like a file path is actually a directory on a hard drive.

(In reply to comment #7)
> My point was that the lack of a directory separator on the end says _nothing_
> about whether a path refers to a directory or a file. I can do both
> /home/jmdavis and /home/jmdavis/ and _both_ refer to a directory on my system.

I would reject `/home/jmdavis/` as a valid file path on Windows only.
/home/jdavis would be accepted as a valid file path, even if it's *actually* a
directory. I could easily do a std.file.isDir check *after* that if I need to.

> And as for across OSes, I can legally put a backslash in a filename on Linux.
> It would be very wrong to say that there was anything invalid about having one
> at the end of a file name except on Windows. 

On Linux this would pass as a valid file path. Putting it in the library means
it will work in a cross-platform and platform-specific way. Forward slashes are
also *valid* on Windows, and yet dirSeparator only returns r"\" on Windows.

> path.endsWith(dirSeparator) tells
> you exactly whether the file name ends with a directory separator or not. If
> you want to check that a path is valid and does not end with a directory
> separator, then do
> assert(isValidPath(path) && !path.endsWith(dirSeparator));

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

import std.path;
import std.algorithm;

void main()
    string path = r"C:\foo\bar/";
    assert(isValidPath(path) && !path.endsWith(dirSeparator));

> I don't see why a new function is needed for this, _especially_ when it's
> perfectly legal to refer to directories _without_ a directory separator on the
> end of the path. 

The focus is on files, not directories. Read my post with the build example
I've pasted before.

> I don't see how you can really be using paths alone to try and determine
> whether a path refers to a file or not when the path doesn't give you enough
> information to do that. It would make no sense for std.path to try and
> determine that IMHO precisely because the information isn't there. You need to
> actually check the disk for that - which is what std.file.isFile is for.

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.

This could save you time compared to just blindly doing:
   foreach (path; possibleFilePaths)
   auto file = File(path, "w");  // could fail if it's a directory string
catch { }

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to