1. RE: Errors in function trim() (ESL0.01)
- Posted by Cuvier Christian <christian.cuvier at insee.fr> Mar 30, 2006
- 445 views
> > Subject: Re: Errors in function trim() (ESL0.01) > > > posted by: Evan Marshall <1evan at sbcglobal.net> > > Julio C. Galaret Viera wrote: > > > > Taking into account the failure of ESL in providing a > working trim() function > > (though efforts are very much appreciated) and the many > working versions of > > it supplied in this thread.. wouldn't it be nice to see the > trim() function > > built in version 3.0. This function is HEAVILY used by any > programmer in the > > world (or may be in the whole universe?).. > > > > JG > > > Funny, I don't remember the last time I had to use it....... > People always say that I'm in a world of my own. > You are not alone. I don't remember either, but I know it only happened once and the file is on my HD, so I could tell when was that last time. By the way, I did not perform any benvhmark, but here is a try:
constant white_spaces=" \t\n\r" global function trim_chars(sequence string,sequence chars_to_remove) integer start,end_ start=find(string[1],chars_to_remove)>0 end_=find(string[$],chars_to_remove)>0 if not (start or end_) then return string --nothing to trim end if end_=length(string)-end_ start+=1 while start<=end_ and find(string[start],chars_to_remove) do start+=1 --traverse string and stops at end_ or first nontrimmable end while if start>end_ then return "" -- string trimmed up empey end if while find(string[end_],chars_to_remove) do -- traverse backwards and stop at last non trimmable, which is start at least end_-=1 end while return string[start..end_] end function global function trim(sequence string) -- wrapper using predefined constant, assumed to be the most frequent value -- for the second argument return trim_chars(string,white_spaces) end function
Anyone cares to benchmark all the variants? CChris