packages feed

crockford (empty) → 0.1

raw patch · 5 files changed

+180/−0 lines, 5 filesdep +QuickCheckdep +basedep +digitsbuild-type:Customsetup-changed

Dependencies added: QuickCheck, base, digits, safe

Files

+ LICENSE view
@@ -0,0 +1,11 @@+Copyright (c) 2009, Henry Bucklow+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.+    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.+    * Neither the name of the N-Sim Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.+
+ Setup.lhs view
@@ -0,0 +1,7 @@+#! /usr/bin/env runhaskell++> import Distribution.Simple+> import System.Cmd+> tests _ _ _ _ = system "runhaskell src/Tests.hs" >> return ()+> main = defaultMainWithHooks (simpleUserHooks {runTests = tests})+
+ crockford.cabal view
@@ -0,0 +1,20 @@+Build-Type: Custom+Name: crockford+Category: Codec+Version: 0.1+Cabal-Version: >= 1.2+Synopsis: An implementation of Douglas Crockford's base32 encoding.+Description:+    An implementation of Douglas Crockford's base32 encoding.+License: BSD3+License-File: LICENSE+Copyright: (c) 2009 Henry Bucklow+Author: Henry Bucklow+Maintainer: Henry Bucklow <henry@elsie.org.uk>+Tested-With: GHC==6.10+Build-Depends: base>=3 && < 4, digits, safe, QuickCheck+Exposed-Modules: Codec.Crockford+Hs-Source-Dirs: src+Extra-Source-Files: src/Tests.hs+GHC-Options: -Wall+
+ src/Codec/Crockford.hs view
@@ -0,0 +1,129 @@+module Codec.Crockford (encode, decode, prop_crockfordRoundTrip) where++import Control.Monad+import Data.Char+import Data.Digits (digits, unDigits)+import Data.Maybe+import Test.QuickCheck++{-+0  	0 O o  	0+1 	1 I i L l 	1+2 	2 	2+3 	3 	3+4 	4 	4+5 	5 	5+6 	6 	6+7 	7 	7+8 	8 	8+9 	9 	9+10 	A a 	A+11 	B b 	B+12 	C c 	C+13 	D d 	D+14 	E e 	E+15 	F f 	F+16 	G g 	G+17 	H h 	H+18 	J j 	J+19 	K k 	K+20 	M m 	M+21 	N n 	N+22 	P p 	P+23 	Q q 	Q+24 	R r 	R+25 	S s 	S+26 	T t 	T+27 	V v 	V+28 	W w 	W+29 	X x 	X+30 	Y y 	Y+31 	Z z 	Z+-}++decode :: Integral i => String -> Maybe i+decode s = mapM decodeChar s >>= return . unDigits 32++encode :: Integral i => i -> String+encode = fromJust . mapM encodeChar . digits 32++decodeChar :: Integral i => Char -> Maybe i+decodeChar c = case toUpper c of+    '0' -> Just 0+    'O' -> Just 0+    '1' -> Just 1+    'I' -> Just 1+    'L' -> Just 1+    '2' -> Just 2+    '3' -> Just 3+    '4' -> Just 4+    '5' -> Just 5+    '6' -> Just 6+    '7' -> Just 7+    '8' -> Just 8+    '9' -> Just 9+    'A' -> Just 10+    'B' -> Just 11+    'C' -> Just 12+    'D' -> Just 13+    'E' -> Just 14+    'F' -> Just 15+    'G' -> Just 16+    'H' -> Just 17+    'J' -> Just 18+    'K' -> Just 19+    'M' -> Just 20+    'N' -> Just 21+    'P' -> Just 22+    'Q' -> Just 23+    'R' -> Just 24+    'S' -> Just 25+    'T' -> Just 26+    'V' -> Just 27+    'W' -> Just 28+    'X' -> Just 29+    'Y' -> Just 30+    'Z' -> Just 31+    _ -> Nothing++encodeChar :: Integral i => i -> Maybe Char+encodeChar i = case i of+    0  -> Just '0'+    1  -> Just '1'+    2  -> Just '2'+    3  -> Just '3'+    4  -> Just '4'+    5  -> Just '5'+    6  -> Just  '6'+    7  -> Just '7'+    8  -> Just '8'+    9  -> Just '9'+    10 -> Just 'A'+    11 -> Just 'B'+    12 -> Just 'C'+    13 -> Just 'D'+    14 -> Just 'E'+    15 -> Just 'F'+    16 -> Just 'G'+    17 -> Just 'H'+    18 -> Just 'J'+    19 -> Just 'K'+    20 -> Just 'M'+    21 -> Just 'N'+    22 -> Just 'P'+    23 -> Just 'Q'+    24 -> Just 'R'+    25 -> Just 'S'+    26 -> Just 'T'+    27 -> Just 'V'+    28 -> Just 'W'+    29 -> Just 'X'+    30 -> Just 'Y'+    31 -> Just 'Z'+    _ -> Nothing++prop_crockfordRoundTrip+    :: Integer -- ^ The integer to test.+    -> Property+prop_crockfordRoundTrip i = i > 0 ==> Just i == (decode . encode) i+
+ src/Tests.hs view
@@ -0,0 +1,13 @@+module Main where++import Codec.Crockford+import Test.QuickCheck+import Text.Printf++tests :: [(String, IO ())]+tests = [+    ("crockfordRoundTrip", quickCheck prop_crockfordRoundTrip)]++main :: IO ()+main = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests+