bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] funcsub func needed


From: Andrew J. Schorr
Subject: Re: [bug-gawk] funcsub func needed
Date: Mon, 8 Dec 2014 10:43:32 -0500
User-agent: Mutt/1.5.23 (2014-03-12)

Hi,

On Mon, Dec 08, 2014 at 09:13:29AM +0100, Kjetil Flovild-Midtlie wrote:
> # kind of like gensub(global flag on)
> # but expands grp-values
> # ( userData is also a new feature )
> #
> # 60 TIMES slower than gensub !!!!!!!!!!
> #
> # .. I need a C version of this

Are you certain that a C version would be much faster?  It would still need to
call out to an awk function to process each match.  That might be where the
performance problem occurs.  I have not profiled this, so I am just guessing.
Have you profiled it?

> # replaceFn has this signature:: (arr,userData) returns string
> 
> function funcsub(regex,replaceFn,userData,target){
>   newVal = target
> 
>   while (match(target,regex,grps)) {
>     singleHit = substr(target, RSTART, RLENGTH);
>     sub(singleHit, @replaceFn(grps,userData),newVal)

How does this actually work?  If "grps" contains multiple matches, how does it
condense that into a single string that can work with sub?

Can you show us an example of a real problem that you are trying to solve?

Also, my guess is that "sub" is not the fastest way to do this.  Have you
considered using substr to reassemble the string instead?

I haven't tested it, but maybe something like:

function funcsub(regex,replaceFn,userData,target,   newVal){
   while (match(target,regex,grps)) {
     newVal = (newVal substr(target, 1, RSTART-1) @replaceFn(grps,userData))
     target = substr(target, RSTART + RLENGTH);
   }
   return (newVal target)
}

But I'm not sure I understand exactly how this is all supposed to work,
so this may be wrong...

Regards,
Andy



reply via email to

[Prev in Thread] Current Thread [Next in Thread]