Re: need something faster
Re: need something faster
- Subject: Re: need something faster
- From: kai <email@hidden>
- Date: Sat, 4 Jun 2005 23:35:24 +0100
On Saturday, June 4, 2005, at 12:49 pm, Hudson Barton wrote:
What the following handler does is clean up a text string that
contains unwanted characters and formats the result with regular line
endings. It is way too slow when processing a long string. Any
suggestions?
on cleanup(dirtytext)
set i to 1
set ct to length of dirtytext
set cleantext to ""
set r to 1
repeat with i from 1 to ct
if character i of dirtytext is in "ABCDEF1234567890" then
set cleantext to cleantext & (character i of dirtytext) as string
set i to (i + 1)
if r = 40 then
set cleantext to (cleantext & (ascii character 10)) as string
set r to 1
else
set r to (r + 1)
end if
end if
end repeat
if last character of cleantext is not (ascii character 10) then set
cleantext to cleantext & (ascii character 10)
return cleantext
end cleanup
You might like to try something like the following effort, Hudson.
Compared with the original, it's a fair bit more efficient here
(roughly 5 times faster with input strings of 1,000 characters, 14
times faster with 10,000 characters and 35 times faster with 100,000
characters).
One point: while your routine isn't case sensitive, the one below is.
(If you wanted to include lowercase characters a-f, then you'd need to
amend the third line of the 'cleanup' handler to <repeat with c in
"abcdefABCDEF1234567890">.)
------------
on textItems from t
try
t's text items
on error number -2706
tell (count t's text items) div 2 to ¬
my (textItems from (t's text 1 thru text item it)) & ¬
my (textItems from (t's text from text item (it + 1) to -1))
end try
end textItems
on cleanup(t)
set s to t
set d to text item delimiters
repeat with c in "ABCDEF1234567890"
set text item delimiters to c
set s to textItems from s
set text item delimiters to ""
set s to s as string
end repeat
repeat while (count s) > 0
set text item delimiters to s's item 1
set s to textItems from s
set t to textItems from t
set text item delimiters to ""
set s to s as string
set t to t as string
end repeat
set r to {}
set n to 0
set e to count t
repeat with n from 40 to e by 40
set r's end to t's text (n - 39) thru n
end repeat
if n is not e then set r's end to t's text (n + 1) thru -1
set text item delimiters to ASCII character 10
set r to {r, ""} as string
set text item delimiters to d
r
end cleanup
------------
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden