Re: Looking for the best "Parse"...
- Posted by Jiri Babor <jbabor at PARADISE.NET.NZ> Oct 25, 2001
- 388 views
Hi, Cassidy, the function below is probably faster, because it avoids both recursion and repeated conversions of the same text to lower case. But you can gain more by simply replacing Rob's elegant, but inefficient lower() function in wildcard.e by something like this: constant DIFF = 'a' - 'A' global function lower(object x) integer c if atom(x) then if x >= 'A' then if x <= 'Z' then return x + DIFF end if end if else for i = 1 to length(x) do c = x[i] if c >= 'A' then if c <= 'Z' then x[i] += DIFF end if end if end for end if return x end function (Note: I did not use 'short-circuit' evaluation, simply because it is much slower than its equivalent. I hope Rob can fix that soon.) function parse(sequence text, sequence esc, sequence replace) sequence e, r, s integer m, n, i, j, p e = lower(esc) n = length(esc) s = lower(text) i = 0 r = {} j = match(e, s) while j do r = (i + j) & r i += j + n - 1 s = s[j+n..length(s)] j = match(e, s) end while m = length(r) if m then for k = 1 to m do p = r[k] text = text[1..p-1] & replace & text[p+n..length(text)] end for end if return text end function --jiri ----- Original Message ----- From: "Cassidy Napoli" <gonzotek at yahoo.com> To: "EUforum" <EUforum at topica.com> Sent: Friday, 26 October 2001 03:39 Subject: Looking for the best "Parse"... > > I want to be able to let the user specify a string of text with > variables in it that my program will replace. The goal is to put the > current winamp song into a line of text. I wrote this function, which > works, but after reading the "Best Join" discussion, I realized there > might be a better way of doing it. So I am looking for any faster or > simpler methods, if there are any. I'll gladly accept any comments on > my coding style, too. I want to improve it. > > [begin code] > include wildcard.e > > global function parseUserText(sequence unParsedText, sequence escape, > sequence replace ) > sequence ParsedText > integer varPos > varPos = match(lower(escape), lower(unParsedText)) > if not varPos then > return unParsedText > elsif varPos = (length(unParsedText) - length(escape) + 1 ) then > ParsedText = unParsedText[1..(length(unParsedText) - > length(escape))] & replace > return parseUserText(ParsedText, escape, replace) > else > if varPos = 1 then > ParsedText = replace & unParsedText[(length(escape) + > 1)..length(unParsedText)] > return parseUserText(ParsedText, escape, replace) > else > ParsedText = unParsedText[1..(varPos - 1)] & replace & > unParsedText[(varPos + (length(escape)))..length(unParsedText)] > return parseUserText(ParsedText, escape, replace) > end if > end if > end function > --ex. text = parseUserText("The $A jumped over the lazy > dog.","$A","quick brown fox") > > puts (1, parseUserText("The $A jumped over the lazy dog.","$A","quick > brown fox")) > [end code] > > >