1. Errors in function trim() (ESL0.01)
I couldn't find a more recent version of ESL but I found errors both in syntax
and algorithm in the trim() function (string.e).
In ESL0.01 appears as:
-- Routine: trim
-- Syntax: s = trim(s)
-- Description: Takes a string and returns the string minus leading and trailing
spaces.
global function trim(sequence s)
atom trim1
sequence s2
s2 = s
for i = 1 to length(s) do
if (s[i] != " ") then
trim1 = i + 1
exit
end if
end for
s2 = s2[trim1..$]
trim1 = 0
for i = length(s) to 1 by -1 do
if s[i] != " " then
trim1 = i + 1
exit
end if
end for
s2 = s2[1..$-trim1]
return s2
end function
It should be:
global function xtrim(sequence s)
atom trim1
sequence s2
s2 = s
trim1 = 0
for i = 1 to length(s) do
if not find(s2[i], " ") then
trim1 = i
exit
end if
end for
s2 = s2[trim1..length(s2)]
trim1 = 0
for i = length(s2) to 1 by -1 do
if not find(s2[i], " ") then
trim1 = i
exit
end if
end for
s2 = s2[1..trim1]
return s2
end function
Or even better:
global function trim(sequence s)
sequence s2
s2 = trim_left(s)
s2 = trim_right(s2)
return s2
end function
JG
2. Re: Errors in function trim() (ESL0.01)
Julio C. Galaret Viera wrote:
<snip>
> Or even better:
>
> }}}
<eucode>
> global function trim(sequence s)
> sequence s2
> s2 = trim_left(s)
> s2 = trim_right(s2)
> return s2
> end function
> </eucode>
{{{
Or maybe:
global function trim(sequence s)
return trim_left(trim_right(s))
end function
Regards,
Juergen
--
Have you read a good program lately?
3. Re: Errors in function trim() (ESL0.01)
Juergen Luethje wrote:
>
> Or maybe:
>
> }}}
<eucode>
> global function trim(sequence s)
> return trim_left(trim_right(s))
> end function
> </eucode>
{{{
>
>
>
> Regards,
> Juergen
>
> --
> Have you read a good program lately?
Gut!
JG
4. Re: Errors in function trim() (ESL0.01)
Hi there,
I did one for WinClass too, and it came out something like this:
global function trim(sequence s)
s=trim_left(s)
s=trim_right(s)
return s
end function
except of course i used *much* longer names like:
RemoveLeadingWhiteSpace()
I believe in being super descriptive.
Take care,
Al
And, good luck with your Euphoria programming!
My bumper sticker: "I brake for LED's"
From "Black Knight":
"I can live with losing the good fight,
but i can not live without fighting it".
"Well on second thought, maybe not."
5. Re: Errors in function trim() (ESL0.01)
This one of the first routes I wrote when I started Euphoria a long time ago.
</eucode>
{{{
global function del_trail(object line) -- delete trailing whitespace
if length(line)<2 then
return " "
end if
while find(line[length(line)], " \t\n" ) do--WHITE_SPACE
line = line[1..length(line)-1]
end while
return line
end function
global function trim(object line)
if sequence(line) and length(line)>0 then
-- delete any leading whitespace
while find(line[1], " \t\n") do
line = line[2..length(line)]
if length(line) = 0 then
exit
end if
end while
end if
if sequence(line) and length(line)>0 then
-- delete trailing whitespace
while find(line[length(line)], " \t\n") do
line = line[1..length(line)-1]
end while
end if
return line
end function
</eucode>
{{{
Don Cole
6. Re: Errors in function trim() (ESL0.01)
- Posted by Pete Lomax <petelomax at blueyonder.co.uk>
Mar 28, 2006
-
Last edited Mar 29, 2006
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 " ":
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
Regards,
Pete
7. Re: Errors in function trim() (ESL0.01)
- Posted by Juergen Luethje <j.lue at gmx.de>
Mar 28, 2006
-
Last edited Mar 29, 2006
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:
global function trim_chars(sequence str, sequence charsToTrim)
...
end function
So someone can use this function for removing leading and trailing dots
or whatever. The normal trim() function then could be implemented like
this:
constant WSP = " \t\r\n"
global function trim(sequence str)
return trim_chars(str, WSP)
end function
Regards,
Juergen
--
Have you read a good program lately?
8. Re: Errors in function trim() (ESL0.01)
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
9. Re: Errors in function trim() (ESL0.01)
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.
10. 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
11. Re: Errors in function trim() (ESL0.01)
I made a mistake on my last post on this subject.
global function trim(object line)-- In case a decimal is sent the error will not
show up in the
-- .e where trim() is kept (which is not
causing the problem
-- anyway)
if sequence(line) and length(line)>0 then -- eniminates sending in nothing
-- in any case whatever is sent in
is returned*,
while find(line[1], " \t\n") do
line = line[2..length(line)] -- delete any leading whitespace
if length(line) = 0 then --if there is nothing but spaces get out
exit
end if
end while
end if
if sequence(line) and length(line)>0 then
while find(line[length(line)], " \t\n") do
line = line[1..length(line)-1] -- delete trailing whitespace
end while
end if
return line -- *here
end function
Don Cole