Remove filename from path
monarch_dodra via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Sep 25 02:05:14 PDT 2014
On Wednesday, 24 September 2014 at 17:15:39 UTC, Ali Çehreli
wrote:
> find() and friends can be used:
>
> import std.algorithm;
>
> void main()
> {
> string path = "myFile.doc";
> string extension = ".doc";
>
> path = findSplitBefore(path, extension)[0];
> assert(path == "myFile");
> }
I had thought of that, but then you might get in trouble with
something like:
string path = "myPath.doc.old"
> And three retro()s make one modern(): :p
>
> import std.algorithm;
> import std.range;
>
> void main()
> {
> string path = "myFile.doc";
> string extension = ".doc";
>
> path = findSplitAfter(path.retro,
> extension.retro)
> [1].retro;
>
> assert(path == "myFile");
> }
>
> Ali
That was the next one I had. Except here:
- You still run into issues if the extension is *not* .doc (and
there happens to be a .doc somewhere in there).
- You are paying for a search, when you are only interested in
testing a prefix.
I had thought of this though:
void main()
{
string path = "myFile.doc";
string extension = ".doc";
auto rpath = path.retro();
skipOver(rpath, extension.retro);
path = rpath.retro();
assert(path == "myFile");
}
The "issue" though is that skipOver modifies an rvalue, so it's
not as "functional-style" as I would have liked it.
Anyways, the conclusion here (IMO), is that to manipulate paths,
use std.path.
More information about the Digitalmars-d-learn
mailing list