startsWith using an array of needles

Jonathan M Davis jmdavisProg at gmx.com
Tue Aug 20 12:16:11 PDT 2013


On Tuesday, August 20, 2013 19:14:08 Byron wrote:
> On Tuesday, 20 August 2013 at 16:51:35 UTC, Jonathan M Davis
> 
> wrote:
> > On Tuesday, August 20, 2013 17:58:19 Byron wrote:
> >> I am trying to use startsWith with an array of needles. Failes
> >> with not being able to match any functions. Not sure if there
> >> is
> >> a work around.
> >> 
> >> const auto ignore = [".git/", ".gitignore"];
> >> 
> >> foreach(DirEntry e; getcwd.dirEntries(SpanMode.depth).filter!(a
> >> => !a.name.startsWith(ignore))) {
> >> writeln(e.name);
> >> }
> > 
> > startsWith dosen't take an array of needles. It takes a
> > variadic list of them.
> > So, it needs to be something more like
> > 
> > !a.name.startsWith(".git/", ".gitignore")
> > 
> > If you want to save the list though, you can use
> > std.typetuple.TypeTuple and
> > an alias:
> > 
> > alias TypeTuple!(".git/", ".gitignore") ignore;
> > 
> > At that point, the rest of your code should work as-is.
> > 
> > - Jonathan M Davis
> 
> What if I want to load my ignore list from a file?

Then you'll have to make separate calls to startsWith. The number of arguments 
has to be known at compile time, so unless the number of arguments from the 
file is always the same, and you have a variable per argument, you can't do it. 
But unless the needles have similar prefixes, there isn't going to be much of 
an efficiency hit from multiple calls.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list