RE: Looking for the best "Parse"...

new topic     » goto parent     » topic index » view thread      » older message » newer message

Igor and Cassidy,

To summarise reverse slices, if you have a sequence s and a position p 
in that sequence, then a slice s[p..p-1] produces an empty sequence {}.  
This is handy when doing operations such as inserting a string into 
another string, since it works regardless of the insertion point.  You 
don't have to make a special case for p = 1 or p = length(s)+1.

For example, you can insert t into s at p by the general formula:

    s = s[1..p-1] & t & s[p..length(s)]

this will work for any value of p from 1 to length(s)+1.  If p = 1 -- 
ie. prepend t to s -- the formula resolves to:

    s = s[1..0] & t & s[1..length(s)]  --> s = t & s

if p = length(s)+1 -- ie. append t to s -- then the formula resolves to:

    s = s[1..length(s)] & t & s[length(s)+1..length(s)]  --> s = s & t

So, Igor's code can be reduced to:

--[begin code]
include wildcard.e

global function parseUserText(sequence u, sequence e, sequence r)
    integer v
    v = match(lower(e), lower(u))
    if not v then
        return u
    end if
    u = u[1..v-1] & r & u[v+length(e)..length(u)]
    return parseUserText(u,e,r)
end function
--[end code]

This routine is short, but will be very slow.  Jiri's approach should be 
used if speed is an issue (which it usually is).

- Colin

Cassidy Napoli wrote:
> Thanks everyone.  I'm still studying the different methods Jiri, Igor, 
> and Colin posted.  But I'll probably uses Igor's slightly improved 
> version of my method because I understand it the most clearly.
> 
> Jiri - Could you explain more about what is happening using your method? 
> 
>  I'm not exactly sure I follow it, what makes it better than recursion?  
> I'm willing to take it as a matter of faith that it's faster; even 
> though I'm not concerned with marginal speed differences for this 
> particular program, I'll store the knowledge away for when I AM 
> concerned with speed.
> 
> Igor, What's "double delete"?
> 
> Thanks Again,
> Cassidy
> 
>

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu