startsWith using an array of needles

Jonathan M Davis jmdavisProg at gmx.com
Tue Aug 20 09:51:20 PDT 2013


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


More information about the Digitalmars-d-learn mailing list