std.algorithm.skipOver broken / misbehaving?
Ali Çehreli
acehreli at yahoo.com
Sat Oct 6 14:45:20 PDT 2012
On 10/05/2012 01:39 AM, Era Scarecrow wrote:
> Although this likely isn't the most efficient way to do this, it's
> cropped up and here's what I have so far. The idea is to drop all the
> unwanted pathname and only leave the filename (I'm sure there's a
> function there already, just not finding it off hand).
>
> Why is this failing?
>
> [quote]
> bool skipOver(alias pred = "a == b", R1, R2)(ref R1 r1, R2 r2);
> If startsWith(r1, r2), consume the corresponding elements off r1 and
> return true. Otherwise, leave r1 unchanged and return false.
> [/quote]
>
> [code]
> import std.algorithm;
> import std.stdio;
>
> void main() {
> string filename = r"something/long\or short";
> bool skipped;
> do {
> skipped = false;
> //try to handle either slash in this case.
> skipped |= filename.skipOver('\\');
> skipped |= filename.skipOver('/');
> skipped |= filename.skipOver(r"\");
> skipped |= filename.skipOver(r"/");
> } while (skipped);
>
> //originally was: do {} while(filename.skipOver('\\'));
>
> //asserts, filename hasn't changed.
> assert(filename == "or short", filename);
> }
> [/code]
There is the std.path module and especially the std.path.baseName
function, but it considers either '/' or '\\' depending on the platform.
The following range magic works for both '/' and '\\':
import std.algorithm;
import std.stdio;
import std.range;
void main() {
string filename = r"something/long\or short";
auto firstPart = filename.retro.findAmong("/\\");
filename = filename[firstPart.count .. $];
assert(filename == "or short", filename);
}
Ali
P.S. I too am thinking about abandoning dmd's -property switch. With the
-property switch, I would have to write retro() and count() with
parentheses.
More information about the Digitalmars-d-learn
mailing list