std.algorithm.startsWith with maximal matching

H. S. Teoh hsteoh at
Fri Jan 13 18:47:19 PST 2012

On Fri, Jan 13, 2012 at 09:36:07PM -0500, Jonathan M Davis wrote:
> On Friday, January 13, 2012 16:48:00 H. S. Teoh wrote:
> > Hi all,
> > 
> > I'm reading the docs for startsWith(A,B...) with multiple ranges in B,
> > and it seems that it will always match the *shortest* range whenever
> > more than one range in B matches. Is there a way to make it always match
> > the *longest* range instead? Or do I have to write my own function for
> > that?
> It doesn't have a way to tell it which one to match if multiple match. It just 
> takes the range that you're looking at and the list of elements and/or ranges 
> that the first range might start with. It has to have a way to decide which one 
> to match when multiple match, and the most efficient (and easiest) way is to 
> match the shortest. So, that's what it does.

I suppose that's reasonable.

But what I really want to accomplish is to parse a string containing
multiple words; at each point I have a list of permitted words that need
to be matched against the string; substring matches don't count. I
already have a way of skipping over spaces; so for medial words, I can
simulate this by appending a space to the end of the word list passed to
startsWith(). However, this doesn't work when the word being matched is
at the very end of the string, or if it is followed by punctuation.

Is there another library function that can do this, or do I just have to
roll my own?


Philosophy: how to make a career out of daydreaming.

More information about the Digitalmars-d-learn mailing list