Re: Errors in function trim() (ESL0.01)
Juergen Luethje wrote:
>
> Pete Lomax wrote:
>
> > On Sun, 26 Mar 2006 23:08:57 -0800, "Julio C. Galaret Viera"
> > <guest at RapidEuphoria.com> wrote:
> >
> >> I found errors both in syntax and algorithm in the trim() function
> >> (string.e).
> > <snip>
> > This should be faster (possibly by as much as a factor of 5, though in
> > most cases less) and does not crash when passed eg "" or " ":
> > }}}
<eucode>
> > global function trim(sequence str)
> > --
> > -- remove leading and trailing whitespace.
> > --
> > integer slicestart, sliceend
> > slicestart=1
> > sliceend=length(str)
> > while slicestart<=sliceend
> > and find(str[slicestart]," \t") do
> > slicestart += 1
> > end while
> > while sliceend>slicestart
> > and find(str[sliceend]," \t") do
> > sliceend -= 1
> > end while
> > -- avoid performing a slice unless we have to, since it
> > -- requires a mem alloc/copy; [improves performance]
> > if slicestart>1 or sliceend<length(str) then
> > return str[slicestart..sliceend]
> > end if
> > return str
> > end function
> > </eucode>
{{{
>
> I would replace " \t" with a variable, say 'charsToTrim', and then call
> the function like this:
>
> }}}
<eucode>
> global function trim_chars(sequence str, sequence charsToTrim)
> ...
> end function
> </eucode>
{{{
>
> So someone can use this function for removing leading and trailing dots
> or whatever. The normal trim() function then could be implemented like
> this:
>
> }}}
<eucode>
> constant WSP = " \t\r\n"
>
> global function trim(sequence str)
> return trim_chars(str, WSP)
> end function
> </eucode>
{{{
>
> Regards,
> Juergen
>
> --
> Have you read a good program lately?
For what its worth, Ricardo Forno's genfunc.e
http://www.rapideuphoria.com/genfunc.zip
has the following routines:
--Initializes vector for Trim functions.
--'p' is a string containing the characters defined to be whitespace.
function TrimInit(sequence p)
sequence s
s = repeat(1, 256)
for i = 1 to length(p) do
s[p[i] + 1] = 0
end for
return s
end function
constant NOTBLANK = TrimInit(" \t\n")
--/topic STRING / SEQUENCE
--/func TrimHead(sequence s)
--/desc Trims whitespace in front of a string
--/ret The argument without whitespace at the beginning
--Change "sequence s" to "String s" to force type checking.
--Example: TrimHead(" E u ") gives "E u "
global function TrimHead(sequence s)
integer len
len = length(s)
for i = 1 to len do
if NOTBLANK[s[i] + 1] then
return s[i..len]
end if
end for
return {}
end function
--/topic STRING / SEQUENCE
--/func TrimTail(sequence s)
--/desc Trims whitespace at end of a string
--/ret The argument without whitespace at the end
--Change "sequence s" to "String s" to force type checking.
--Example: TrimTail(" E u ") gives " E u"
global function TrimTail(sequence s)
for i = length(s) to 1 by - 1 do
if NOTBLANK[s[i] + 1] then
return s[1..i]
end if
end for
return {}
end function
--/topic STRING / SEQUENCE
--/func TrimBoth(sequence s)
--/desc Trims whitespace at both ends of a string
--/ret The argument without whitespace at both ends
--Change "sequence s" to "String s" to force type checking.
--Example: TrimBoth(" E u ") gives "E u"
global function TrimBoth(sequence s)
return TrimTail(TrimHead(s))
end function
I've always thought of Ricardo's genfunc.e as
an ESL already done.
Ken Rhodes
Folding at Home: http://folding.stanford.edu/
100% MicroSoft Free
SuSE Linux 10.0
No AddWare, SpyWare, or Viruses!
Life is Good
|
Not Categorized, Please Help
|
|