std.algorithm.startsWith with maximal matching

Jonathan M Davis jmdavisProg at gmx.com
Sun Jan 15 15:23:46 PST 2012


On Sunday, January 15, 2012 11:23:04 H. S. Teoh wrote:
> On Sat, Jan 14, 2012 at 07:53:10PM -0800, Jonathan M Davis wrote:
> [...]
> 
> > Actually, if the word has to match exactly, then startsWith isn't
> > going to cut it. What you need to do is outright strip the punctuation
> > from both ends.  You'd need something more like
> > 
> > word = find!(not!(std.uni.isPunctuation))(word);
> > word = array(until!(std.uni.isPunctuation)(word));
> > 
> > if(canFind(wordList, word))
> > {
> > 
> >     //...
> > 
> > }
> 
> [...]
> 
> Thanks for the info, but this method has the flaw that the original
> punctuation is lost, unless I work with a copy of the word. I was hoping
> for a nice way to do matching in-place.
> 
> But perhaps what I need is a full-fledged lexer after all. Unless
> there's a nice way of saying "match up to some predicate that determines
> the end of the word" in the current infrastructure.

Depending on what you're doing, a full-blown lexer would indeed make more 
sense. You could make splitter's predicate split on both whitespace and 
punctuation if that helps. But as for search in words, look at the various 
functions in std.range and std.algorithm. In particular, the ones listed as 
being in the "searching" category at the top of std.algorithm are likely to be 
of help. But what the exact combination of them is that will do the best job 
for you, I don't know, since I don't fully understand what your exact 
requirements are. And it's definitely possible that what you need is a function 
which doesn't exist in Phobos. What's there is quite good, but it doesn't 
cover every scenario.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list