Re: Aplescript for changing case (again)
Re: Aplescript for changing case (again)
- Subject: Re: Aplescript for changing case (again)
- From: "Arthur J Knapp" <email@hidden>
- Date: Tue, 09 Jan 2001 16:54:29 -0500
>
Subject: Re: Aplescript for changing case (again)
>
Date: Tue, 9 Jan 2001 10:40:06 -0800
>
From: Richard 23 <email@hidden>
>
this one might be just a tiny bit more efficient.
Yeah. The "Essential Routines" module from Apple is nice, and
very clearly written, but Richard 23's implementation contains
all the current *best thinking* on vanilla case conversion.
>
-- Preprocessed by Convert Script 1.0d2
>
ToLower("HEllO DoodS!")
>
property ABC : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
>
property xyz : "abcdefghijklmnopqrstuvwxyz"
Many other case-conversion scripts will use from 2 to 3
calls to the ASCII osaxen commands for every character in
a string. By using hard-coded characters and the text item
delimiters, we can avoid the ASCII commands, (which can be
quite *expensive* for large strings).
>
on ToLower(theStr)
>
tell (a reference to AppleScript's text item delimiters)
>
set contents to ""
For those who are confused, the above syntax is just a means
of saving some typing later on. Using the text item delimiters
in a tell statement is not more effeicent or "better" than
writing them out each time, it just makes some things easier to
read. The tell target means that we can type "contents", and the
tell-target ensures that we are actually saying "contents of text
item delimiters of AppleScript".
>
set theStr to theStr as string
>
repeat with thePos from 1 to 26
Twenty-six letters in the alphabet.
>
if theStr contains (ABC's item thePos) then
The above innovation is neat. I used to just do the tids replacement
without first testing whether each replacement was nessasary. My thinking
was that adding twenty-six "if" tests would just slow things down, but
speed tests on large pieces of text have shown that it is faster to do
a "contains" operation first.
>
set {contents} to {ABC's item thePos}
>
set {contents} to {xyz's item thePos, theStr's text items}
>
set theStr to result's end as string
Whoops. Forget what I said about the tell statement not making things
faster. :) This use of "Nigel Garvey pattern replacement" can save any
number of useful milliseconds when used properly. ;-)
The end result is that "A" is replaced with "a", "B" with "b", etc.,
going thru the entire alphabet using simple text item delimiter replacement.
If there are any drawbacks to this method at all, it lies in the fact that
there is an upper limit on how large a list of text items AppleScript will
return from a text item coercion, but the limit is fairly large, (around
4060 some text items).
--
{
Arthur J Knapp, of STELLARViSIONs ;
http://www.STELLARViSIONs.com ;
mailto:email@hidden ;
how many?
zero.
are you sure?
i counted twice.
}