Character.st
branchjv
changeset 17728 bbc5fa73dfab
parent 17711 39faaaf888b4
child 17732 a1892eeca6c0
--- a/Character.st	Sun Aug 16 18:14:23 2009 +0100
+++ b/Character.st	Wed Aug 19 17:14:36 2009 +0100
@@ -1426,11 +1426,7 @@
       A major advantage of rot13 over rot(N) for other N is that it
       is self-inverse, so the same code can be used for encoding and decoding."
 
-    (((self >= $a) and:[self < $n]) or:[(self >= $A) and:[self < $N]])
-	ifTrue: [ ^ self + 13 ].
-    (((self > $m) and:[self <= $z]) or:[(self > $M) and:[self <= $Z]])
-	ifTrue: [ ^ self - 13 ].
-    ^ self
+    ^ self rot:13
 
     "
      $h rot13
@@ -1438,6 +1434,31 @@
      'The butler did it!!' rot13             -> 'Gur ohgyre qvq vg!!'
      'The butler did it!!' rot13 rot13       -> 'The butler did it!!'
     "
+!
+
+rot:n
+     "Usenet: from `rotate alphabet N places']
+      The simple Caesar-cypher encryption that replaces each English
+      letter with the one N places forward or back along the alphabet,
+      so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' by rot:13
+      Most Usenet news reading and posting programs include a rot13 feature.
+      It is used to enclose the text in a sealed wrapper that the reader must choose
+      to open -- e.g., for posting things that might offend some readers, or spoilers.
+      A major advantage of rot13 over rot(N) for other N is that it
+      is self-inverse, so the same code can be used for encoding and decoding."
+
+    (self isLetter) ifTrue:[
+        self isLowercase ifTrue:[
+            ^ 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' at:(self-$a+1+n)
+        ].
+        ^ 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ' at:(self-$A+1+n)
+    ].
+    ^ self
+
+    "
+     'The butler did it!!' rot:13                -> 'Gur ohgyre qvq vg!!'
+     ('The butler did it!!' rot:13) rot:13       -> 'The butler did it!!'
+    "
 ! !
 
 
@@ -2746,5 +2767,5 @@
 !Character class methodsFor:'documentation'!
 
 version
-    ^ '$Id: Character.st 10447 2009-06-14 13:09:55Z vranyj1 $'
+    ^ '$Id: Character.st 10467 2009-08-19 16:14:36Z vranyj1 $'
 ! !