Re: Simple rot-13 encoding script
Re: Simple rot-13 encoding script
- Subject: Re: Simple rot-13 encoding script
- From: Nigel Smith <email@hidden>
- Date: Fri, 23 May 2003 12:15:00 +0100
On 21/5/03 19:44, "Marc K. Myers" <email@hidden> wrote:
>
What's wrong has been pretty well answered. Here's a ROT13 routine
>
which is much faster and more efficient:
>
>
set the clipboard to ROT13(the clipboard as string)
>
>
on ROT13(theText)
>
-- the "plaintext" characters for comparison
>
set compStrng to
>
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
>
-- the "encrypted" characters
>
set replStrng to
>
"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
>
-- turn the strings into lists
>
set compList to characters of compStrng
>
set replList to characters of replStrng
>
-- a never-used character used to tag each plaintext character.
>
Only tagged
>
-- characters will be converted. After conversion the tag is
>
removed so
>
-- the character will not get re-converted in subsequent passes
>
set theFlag to ASCII character 215
>
>
-- convert the plaintext to a list
>
set theText to characters of theText
>
set AppleScript's text item delimiters to {theFlag}
>
-- convert the plaintext back to text but with each character
>
tagged with theFlag
>
set theText to theFlag & (theText as text) -- flag each character
>
-- repeat for each character (upper and lowercase) of the alphabet
>
repeat with i from 1 to count compList
>
-- check for combination of the flag & alpha character
>
set AppleScript's text item delimiters to theFlag & (item i of
>
compList)
>
set theText to text items of theText
>
-- replace with new alpha character w/o flag
>
set AppleScript's text item delimiters to (item i of replList)
>
set theText to (theText as string)
>
end repeat
>
-- remove flags from non-alphabetic characters
>
set AppleScript's text item delimiters to {theFlag}
>
set theText to text items of theText
>
set AppleScript's text item delimiters to {""}
>
set theText to theText as text
>
>
return theText
>
end ROT13
This is a lot longer and 1/5th as fast as my hastily knocked-together
version, below, so I am obviously missing something! What?
return rot("Hello World") of me
on rot(inString)
set outString to ""
set plainChars to "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
set rotChars to "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
repeat with x from 1 to count of inString
set offsetNum to offset of (character x of inString) in plainChars
if offsetNum > 0 then
set outString to outString & character offsetNum of rotChars
else
set outString to outString & character x of inString
end if
end repeat
return outString
end rot
Nigel
_______________________________________________
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.