File: Rot13.hs

package info (click to toggle)
haskell-snap-server 1.1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 532 kB
  • sloc: haskell: 5,445; ansic: 4; makefile: 2
file content (24 lines) | stat: -rw-r--r-- 796 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Test.Common.Rot13 (rot13) where

----------------------------------------------------------------------------
import           Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as S
import           Data.Char( ord, isAsciiUpper, isAsciiLower, isAlpha, chr )


------------------------------------------------------------------------------
rotone :: Char -> Char
rotone x | acc x = f
         | otherwise = x
  where
    aA    = ord 'A'
    aa    = ord 'a'
    xx    = ord x
    f     = g $ if isAsciiUpper x then aA else aa
    g st  = chr $ st + (xx - st + 13) `mod` 26
    acc c = isAlpha c && (isAsciiUpper c || isAsciiLower c)


----------------------------------------------------------------------------
rot13 :: ByteString -> ByteString
rot13 = S.map rotone