RegEx Replace Text -- strip leading and trailing spaces
Alex Tweedly
alex at tweedly.net
Wed Apr 13 18:55:59 EDT 2005
Trevor DeVore wrote:
> This will strip leading and trailing whitespace from a multiline
> string. But given that you say that word 1 to -1 is that much faster
> I am going to switch to that. Those of us still trying to break away
> from our previous languages like to complicate things on occasion ;)
>
> Is there a way to do ltrim (trim whitespace on left of string) in
> plain transcript? Right now I just use:
>
> function str_lTrim pString
> get replaceText(pString, "^[ \t\r\n]+", "")
> return it
> end str_lTrim
>
>
function str_lTrim pString
if word 1 of pString is empty then return empty -- deal with case
of all whitespace
return char (offset(word 1 of pString, pString)) to -1 of pString
end pString
The timings are fairly data dependent, but this is about 20% faster for
most cases, and not slower for any I found.
If you know you aren't dealing with all-whitespace strings, you can
remove that test line, and get another 10% improvement.
You can take any of those and improve by another 5% by passing the
parameter by reference; avoids a data copy, and since you don't modify
it, there's no downside.
BUT - in all those cases, the overhead of the function call is far more
significant than the actual operation, so you can get another 50% saving
by doing the replaceText (or chunk test) in-line rather than as a
function. You may prefer to use the function anyway for the sake of
code that is easier to read - but if you have one or two crucial spots,
it may be worth in-lining the operation..
--
Alex Tweedly http://www.tweedly.net
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.9.7 - Release Date: 12/04/2005
More information about the use-livecode
mailing list