TRIM command?

Ken Ray kray at sonsothunder.com
Sat May 20 00:38:34 EDT 2006


On 5/19/06 4:15 PM, "Gary S." <garyshow at verizon.net> wrote:

> I don't claim to be a regular expression expert (just a long time
> list lurker), but I recently had the same need for a trim command and
> came up with this function:
> 
> function trimWS pText
>    -- Remove all leading and trailing whitespace characters.
>    -- I.E., space, tab, carriage return, line feed, form feed.
> 
>    return replaceText(pText,"(^\s+)|(\s+$)",empty)
> 
> end trimWS

Yes, this works for everything except hard spaces (i.e. nonbreaking spaces),
which are not seen as whitespace by the regex engine.  Admittedly this
should be extremely rare that you might find one of these, but if you do, it
can ruin your day. Here's a version that takes this into account
(unfortunately it is different per platform):

function trimWS pText
   -- Remove all leading and trailing whitespace characters.
   -- I.E., space, tab, carriage return, line feed, form feed, hard space
  if the platform is "MacOS" then
    return replaceText(pWhat,"(^(\s|\xca)+)|((\s|\xca)+$)",empty)
  else
    return replaceText(pWhat,"(^(\s|\xa0)+)|((\s|\xa0)+$)",empty)
  end if
end trimWS

For those of you interested, I've been keeping track of the various forms of
trimming, and have (with RevBench's help) shown that although the above uses
less code, it is not as efficient as a much more wordy function (the
function above comes in at 0.0257 ticks).

The following trim function is so far the fastest, most comprehensive
version (0.0004 ticks):

function trim what
  if the platform is "MacOS" then put numToChar(202) into tHardSpc
  else put numToChar(160) into tHardSpc
  put (space & tab & cr & linefeed & formfeed & tHardSpc) into tWhiteSpc
  repeat
    if char 1 of what is not in tWhiteSpc then exit repeat
    else delete char 1 of what
  end repeat
  repeat
    if char -1 of what is not in tWhiteSpc then exit repeat
    else delete char -1 of what
  end repeat
  return what
end trim

However the approach provided by Jacque Gay (and others) is the simplest and
is fastest (0.0002 ticks):

function trim what
  return (word 1 to -1 of what)
end trim

but doesn't handle hard spaces and is tripped up if there is a lone double
quote in a string:

  put quote & "   this is a test   " into tString
  put trim(tString)

will *not* trim the spaces from the end of the string.

Take your pick...

:-)
   

Ken Ray
Sons of Thunder Software
Web site: http://www.sonsothunder.com/
Email: kray at sonsothunder.com





More information about the use-livecode mailing list