Re: Convert first char to lower case
Re: Convert first char to lower case
- Subject: Re: Convert first char to lower case
- From: Nigel Garvey <email@hidden>
- Date: Tue, 8 Jan 2002 15:52:08 +0000
Paul Berkowitz wrote on Mon, 07 Jan 2002 18:54:57 -0800:
>
On 1/7/02 6:28 PM, "Nigel Garvey" <email@hidden>
>
wrote:
>
> 'ASCII number' and 'ASCII character' can be quite slow if you have to use
>
> them a lot. In AppleScript, it's more convenient to look for the first
>
> character [o the input string] in an alphabetical string of the upper-case
characters, and to
>
> replace it with the same-numbered character from a lower-case string.
>
> if (class of Variable1 is string) and ((count Variable1) > 0) then
>
> set upperChars to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
>
> set lowerChars to "abcdefghijklmnopqrstuvwxyz"
>
> -- 'offset' is case-sensitive
>
> set alphaPos to the offset of (character 1 of Variable1) in
>
> upperChars
>
>
Nigel,
>
>
Knowing you, you've probably tested this. But is 'offset ' that much faster
>
than ASCII number?
Hi, Paul. Hmmm. I have to admit that my assertion was based on previous
experience, where almost anything that achieved the same end has been
faster than using 'ASCII number' and 'ASCII character'. But I see that
rehashing my version of the handler to use them instead of 'offset' makes
it a little over twice as fast. Apologies then for that misinformation.
>
It's also an osax. Wouldn't it be quicker yet to use text
>
item delimiters, which are also case-sensitive, and much faster?
>
>
set AppleScript's text item delimiters to {character 1 of Variable1}
>
set alphaPos to (count (text item 1 of upperChars)) + 1
>
set AppleScript's text item delimiters to {""}
Yes, that's a sensational difference! About six times as fast as what I
posted. Typical results for 1000 iterations of each method (run in Script
Editor, AS 1.3.7, on my 4400) are:
'offset': 540 ticks
'ASCII number'/'ASCII character': 250 ticks
'text item delimiters': 88 ticks
The tests also show that if you run the test script too many times,
Script Editor crashes.
It's actually possible to get away with just one alphabet string, so:
--
set myName to "This character"
-- bla bla bla
set myName to ConvertFirstCharToLowerCase(myName)
on ConvertFirstCharToLowerCase(Variable1)
if (class of Variable1 is string) and ((count Variable1) > 0) then
set alphaChars to
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
-- Text item delimiters are case-sensitive
set AppleScript's text item delimiters to {character 1 of Variable1}
set lowerPos to (count text item 1 of alphaChars) + 27
set AppleScript's text item delimiters to {""}
if (lowerPos is not greater than 52) then
if ((count Variable1) = 1) then return (character lowerPos of
alphaChars)
return (character lowerPos of alphaChars) & (text 2 thru -1 of
Variable1)
end if
end if
return Variable1
end ConvertFirstCharToLowerCase
NG