Re: Trim space chars from start of line
Re: Trim space chars from start of line
- Subject: Re: Trim space chars from start of line
- From: "Zavatone, Alex" <email@hidden>
- Date: Mon, 25 Jul 2011 14:49:38 -0700
- Acceptlanguage: en-US
- Thread-topic: Trim space chars from start of line
Please feel free to skip to the end if this is TLDR.On Jul 25, 2011, at 4:29 PM, Mark J. Reed wrote: On Mon, Jul 25, 2011 at 5:20 PM, Mark J. Reed <email@hidden> wrote:
So much unnecessary work! Our old friend "quoted form of" will take care of all the apostrophe-munging if you don't try to turn the string into a Ruby source code literal in addition to making it through the shell:
on stripLeadingWhiteSpace(str) do shell script "ruby -pe '$_.lstrip!' <<<" & (quoted form of str)
end stripLeadingWhiteSpace
Sadly, this is not as fast as I would expect. Is it the overhead of calling the shell script that makes this baby come in at 21 seconds elapsed time? 1 second slower than my original. Of course, Ruby, at least in its pre-1.9 C-based form, is not known for its speed. Perl is probably faster:
on stripLeadingWhiteSpace(str)
do shell script "perl -pe 's/^\\s+//' <<<" & (quoted form of str) end stripLeadingWhiteSpace
Also 21 seconds. Or even good old-fashioned sed:
on stripLeadingWhiteSpace(str)
do shell script "sed 's/^ *//' <<<" & (quoted form of str) end stripLeadingWhiteSpace
Back to 20 seconds. For that matter, the shell itself automatically strips leading whitespace from input lines read with "read", though I wouldn't necessarily trust it to pass everything else through intact:
on stripLeadingWhiteSpace(str)
do shell script "(read line; echo \"$line\") <<<" & (quoted form of str) end stripLeadingWhiteSpace
20 seconds.
Waaaait. Doing nothing = 19 seconds.
It turns out that all these routines are fast enough. It's the looping through 2500 lines of source that is taking all the time.
Argh.
Thanks everyone. This email message and any attachments are for the sole use of the intended recipient(s) and may contain information that is proprietary to U.S. Foodservice, Inc and/or its subsidiaries or otherwise confidential or legally privileged. If you have received this message in error, please notify the sender by reply, and delete all copies of this message and any attachments. If you are the intended recipient you may use the information contained in this message and any files attached to this message only as authorized by U.S. Foodservice. Files attached to this message may only be transmitted using secure systems and appropriate means of encryption, and must be secured using the same level password and security protection with which the file was provided to you. Any unauthorized use, dissemination or disclosure of this message or it's attachments is strictly prohibited.
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden