Re: can I do this better?
Re: can I do this better?
- Subject: Re: can I do this better?
- From: Ric Phillips <email@hidden>
- Date: Thu, 13 Jun 2002 11:43:07 +1000
On 13/6/02 3:47 AM, "garrett" <email@hidden> wrote:
>
hey!
>
>
I wrote this little routine to trim off any trailing or beginning spaces or
>
returns. would any of u pros do it differently or better? if so, how and
>
why?
>
Well, a great deal depends on what you consider to be 'better'. For some
that is purely a matter of speed, for others readability of the source - and
then there is memory usage, and / or just plain elegance!
Below is my (humble) offering. As to why.....
It's vanilla apple script (though you could do this a lot faster with
regular expressions, as a shell script in OS X, or with regex OSAX in pre OS
X)
It only uses 2 variables, and both are local to the handler.
It uses direct white space arguments in the conditionals and each
conditional de-references only one variable. Also there are no nested
conditions. (This should provide adequate execution speed.)
I can't see a way to remove the need for two loops, but given that, each
loop stops when it hits the first wanted character, and each only executes
the conditional operation once. (I think this is one case where negative
logic - usually unadvisable - is warranted by the problem).
Tracking the index of the characters in a separate variable and later using
those values to rebuild the input string is not required. So given the
once-only execution of each stripping action, and the simplification of the
code, I suspect this will be more efficient overall.
It treats null characters in the same way it treats other white space
characters - so the logic is a tad simpler. (Completely blank strings on the
other hand, for which my handler will return an empty string, would usually
be a special case in the parent logic, and I would handle that separately,
after using this handler.)
It's reasonably easy to read.
It handles the case of input strings that consist of only white space
characters, once, on exit, without complicating the looping structures.
And whether this is 'better' than your code, or that of others on the list
is up to you to decide.
---------------------------------------------------------------------------
---The Code: (watch out for line breaks / wrapping that would be illegal
--- in AppleScript code.
---------------------------------------------------------------------------
set y to clipWhitespace(y)
on clipWhitespace(s)
set l to length of s -- Required for case of white only string
-- Clip leading
repeat with i from 1 to l
if ({space, return, tab, ""} does not contain item i of (every
character of s)) then
set s to (characters i thru l of s) as string
exit repeat
end if
end repeat
-- Clip trailing
repeat with i from length of s to 1 by -1
if ({space, return, tab, ""} does not contain item i of (every
character of s)) then
set s to (characters 1 thru i of s) as string
exit repeat
end if
end repeat
-- handles case of white space only strings
if l = length of s then
return ""
else
return s
end if
end clipWhitespace
------------------------------------------------------------------------
Ric Phillips
Faculty Web Coordinator
Faculty of Humanities and Social Sciences
Latrobe University
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.