On 9/28/03 1:39 PM, Alex Rice wrote:

Here are two trim whitespace functions that had written. To my surprise the regex variant is way, way faster!

--
-- regex method
--
function trim pText
  get replaceText(pText, "^\s+", empty)
  return replaceText(it, "\s+$", empty)
end trim

--
-- non regex method --
local lWhitespaceChars

on startup
 -- prepare list of characters to be used by trim()
 -- CRLF = ascii 13+10
 put tab & space & CRLF into lWhitespaceChars
end startup

function trim pText
  repeat while char 1 of pText is in lWhitespaceChars
    delete char 1 of pText
  end repeat
  repeat while char -1 of pText is in lWhitespaceChars
    delete char -1 of pText
  end repeat
  return pText
end trim


For an easy "trim" function that is just as fast as regex, try this:


return word 1 to -1 of pText

In a test using text with 1000 leading white space characters and 500 trailing characters, the difference between the regex method and the above one-liner was one millisecond.

--
Jacqueline Landman Gay         |     [EMAIL PROTECTED]
HyperActive Software           |     http://www.hyperactivesw.com

_______________________________________________
use-revolution mailing list
[EMAIL PROTECTED]
http://lists.runrev.com/mailman/listinfo/use-revolution

Reply via email to