Re: Reformatting a string
Re: Reformatting a string
- Subject: Re: Reformatting a string
- From: Axel Luttgens <email@hidden>
- Date: Mon, 30 Jan 2012 12:29:06 +0100
Le 30 janv. 2012 à 04:44, Jon Pugh a écrit :
> On Jan 29, 2012, at 4:59 PM, Shane Stanley wrote:
>> I then tried some tests with the SmartString vanilla method. With a short string, say a few characters, it was super fast. But as the length of the string went up, the time taken increased exponentially -- for a 128-byte string it took about 6 seconds, for 256, about 26 seconds, and for 512, 126 seconds. So it started off more than competitive, but got left behind very quickly. A 256-512 characters is not a large string in the real world.
>
> Yeah, there’s a double loop in there that’s likely the culprit. I just recently fixed an issue with a missing “exit repeat” to short circuit the loop when it finds a match, however I’m sure there’s a bunch of special case optimizations that could occur. Especially on strings that don’t need case operations. The current algorithm isn’t designed for speed, it’s designed for flexibility.
>
> C has a lot more capability for speed when doing string manipulation. AppleScript tends to bog down when doing anything involving iterating over strings.
Hello,
I quickly checked whether playing with lists (references to lists or lists as properties) would be of some help:
script CuttedSmartString
property theString : ""
property lowers : "œæáàâäãåéèêëóòôöõúùûüíìîïñçøÿπß"
property uppers : "ŒÆÁÀÂÄÃÅÉÈÊËÓÒÔÖÕÚÙÛÜÍÌÎÏÑÇØŸ∏ẞ"
on lowercase()
set theString to characters of theString
repeat with i from 1 to length of theString
set c to item i of theString
set a to id of c
if a > 64 and a < 91 then
set item i of theString to character id (a + 32)
else if a > 128 then
set o to offset of c in uppers
if o > 0 then set item i of theString to character o of lowers
end if
end repeat
set theString to theString as text
end lowercase
end script
And indeed, it seems to make things much speedier when applied on a long string; perhaps could this be applied to SmartString as a whole, by internally keeping theString as a list?
WRT the use of "offset of": this should be fine with Mac OS X 10.6 and later. Should one want to play on the safe side with 10.5.x as well:
set ncl to number of characters of uppers
[...]
else if a > 128 and c is in uppers then
repeat with lc from 1 to ncl
if c is character lc of uppers then
set item i of theString to character lc of lowers
exit repeat
end if
end repeat
end if
But, as I said, this was a quick trial, and I may have overlooked something...
Axel
_______________________________________________
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