std.algorithm.startsWith with maximal matching

Jonathan M Davis jmdavisProg at
Fri Jan 13 21:30:35 PST 2012

On Friday, January 13, 2012 18:47:19 H. S. Teoh wrote:
> 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?

Use std.array.split. It will split a string into an array of strings using 
whitespace as the delimiter. And if you want a lazy solution (one which avoids 
allocating another array), then using std.algorithm.splitter, and give it 
std.ascii.whitespace as its delimiter. You don't need a custom solution to 
split strings along whitespace. And if you need to compare entire words rather 
than just their beginning, once you have the word split out, you can just use 
== instead of startsWith.

- Jonathan M Davis

More information about the Digitalmars-d-learn mailing list