New regex: Find?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon May 4 18:56:06 PDT 2009


dsimcha wrote:
> == Quote from Joel C. Salomon (joelcsalomon at gmail.com)'s article
>> dsimcha wrote:
>>> Is there an *efficient* way to simply test whether a given string contains a
>>> given regex in the new std.regex?  Using match() and testing for empty works,
>>> but this apparently triggers a bunch of unnecessary heap allocation.  If not,
>>> is this a universal enough feature to warrant an enhancement request?
>> You mean to search for a regex match without constructing the regex
>> engine?  Good luck with that.
>> —Joel Salomon
> 
> Actually, the behavior Andrei describes is what I wanted:  One allocation to
> construct the regex engine, amortized.  However, contrary to what Andrei said,
> match() apparently allocates additional memory on each call.  The following
> program leaks memory like a sieve when the GC is disabled:
> 
> import std.regex, core.memory;
> 
> void main() {
>     string s = "This is only a test.  Repeat, this is only a test.";
>     auto r = regex("is.only");
> 
>     GC.disable;
>     while(true) {
>         auto m = match(s, r);
>     }
> }

Ah... Sigh, I meant to implement the short string optimization in the 
regex range, and put it off forever. It was about time it would come 
back to haunt me :o).

Could you please submit an enhancement request to Bugzilla? Maybe with a 
patch? :o)


Andrei



More information about the Digitalmars-d mailing list