Skip to content

Commit 8727ac2

Browse files
committed
Made package stack compatible
Created and added stack.yaml and .gitignore files. Relaxed the version dependency on 'binary' package in cabal file. Is that OK? Also brought the minimum cabal version to >=1.8, so I could add a test target that pulls in the library. Changed all tabs to spaces - I don't know when the Haskell compiler started giving warnings about that.
1 parent 6284c1a commit 8727ac2

File tree

15 files changed

+478
-345
lines changed

15 files changed

+478
-345
lines changed

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### Haskell ###
2+
dist
3+
dist-*
4+
cabal-dev
5+
*.o
6+
*.hi
7+
*.chi
8+
*.chs.h
9+
*.dyn_o
10+
*.dyn_hi
11+
.hpc
12+
.hsenv
13+
.cabal-sandbox/
14+
cabal.sandbox.config
15+
*.prof
16+
*.aux
17+
*.hp
18+
*.eventlog
19+
.stack-work/
20+
cabal.project.local
21+
.HTF/
22+

Data/Encoding.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,6 @@ encodingFromStringExplicit codeName = case (normalizeEncoding codeName) of
359359
-- | Takes the name of an encoding and creates a dynamic encoding from it.
360360
encodingFromString :: String -> DynEncoding
361361
encodingFromString str = maybe
362-
(error $ "Data.Encoding.encodingFromString: Unknown encoding: "++show str)
363-
id
364-
(encodingFromStringExplicit str)
362+
(error $ "Data.Encoding.encodingFromString: Unknown encoding: "++show str)
363+
id
364+
(encodingFromStringExplicit str)

Data/Encoding/BootString.hs

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{- | This implements BootString en- and decoding, the foundation of Punycode
33
-}
44
module Data.Encoding.BootString
5-
(BootString(..)
6-
,punycode) where
5+
(BootString(..)
6+
,punycode) where
77

88
import Data.Encoding.Base
99
import Data.Encoding.Exception
@@ -17,77 +17,77 @@ import Data.Typeable
1717
import Control.Monad (when)
1818

1919
data BootString = BootString
20-
{base :: Int
21-
,tmin :: Int
22-
,tmax :: Int
23-
,skew :: Int
24-
,damp :: Int
25-
,init_bias :: Int
26-
,init_n :: Int
27-
}
28-
deriving (Show,Eq,Typeable)
20+
{base :: Int
21+
,tmin :: Int
22+
,tmax :: Int
23+
,skew :: Int
24+
,damp :: Int
25+
,init_bias :: Int
26+
,init_n :: Int
27+
}
28+
deriving (Show,Eq,Typeable)
2929

3030
punycode :: BootString
3131
punycode = BootString
32-
{base = 36
33-
,tmin = 1
34-
,tmax = 26
35-
,skew = 38
36-
,damp = 700
37-
,init_bias = 72
38-
,init_n = 0x80
39-
}
32+
{base = 36
33+
,tmin = 1
34+
,tmax = 26
35+
,skew = 38
36+
,damp = 700
37+
,init_bias = 72
38+
,init_n = 0x80
39+
}
4040

4141
punyValue :: ByteSource m => Word8 -> m Int
4242
punyValue c
43-
| n < 0x30 = norep
44-
| n <= 0x39 = return $ n-0x30+26
45-
| n < 0x41 = norep
46-
| n <= 0x5A = return $ n-0x41
47-
| n < 0x61 = norep
48-
| n <= 0x7A = return $ n-0x61
49-
| otherwise = norep
50-
where
51-
n = fromIntegral c
52-
norep = throwException (IllegalCharacter c)
43+
| n < 0x30 = norep
44+
| n <= 0x39 = return $ n-0x30+26
45+
| n < 0x41 = norep
46+
| n <= 0x5A = return $ n-0x41
47+
| n < 0x61 = norep
48+
| n <= 0x7A = return $ n-0x61
49+
| otherwise = norep
50+
where
51+
n = fromIntegral c
52+
norep = throwException (IllegalCharacter c)
5353

5454
punyChar :: ByteSink m => Int -> m Word8
5555
punyChar c
56-
| c < 0 = norep
57-
| c < 26 = return $ fromIntegral $ 0x61+c
58-
| c < 36 = return $ fromIntegral $ 0x30+c-26
59-
| otherwise = norep
60-
where
61-
norep = throwException (HasNoRepresentation (chr c))
56+
| c < 0 = norep
57+
| c < 26 = return $ fromIntegral $ 0x61+c
58+
| c < 36 = return $ fromIntegral $ 0x30+c-26
59+
| otherwise = norep
60+
where
61+
norep = throwException (HasNoRepresentation (chr c))
6262

6363
getT :: BootString -> Int -> Int -> Int
6464
getT bs k bias
65-
| k <= bias + (tmin bs) = tmin bs
66-
| k >= bias + (tmax bs) = tmax bs
67-
| otherwise = k-bias
65+
| k <= bias + (tmin bs) = tmin bs
66+
| k >= bias + (tmax bs) = tmax bs
67+
| otherwise = k-bias
6868

6969
adapt :: BootString -> Int -> Int -> Bool -> Int
7070
adapt bs delta numpoints firsttime = let
71-
delta1 = if firsttime
72-
then delta `div` (damp bs)
73-
else delta `div` 2
74-
delta2 = delta1 + (delta1 `div` numpoints)
75-
(rd,rk) = head
76-
$ filter ((<=((base bs - tmin bs) * (tmax bs)) `div` 2).fst)
77-
$ iterate (\(d,k) -> (d `div` (base bs - tmin bs),k+(base bs))) (delta2,0)
78-
in rk + (((base bs - tmin bs +1) * rd) `div` (rd + skew bs))
71+
delta1 = if firsttime
72+
then delta `div` (damp bs)
73+
else delta `div` 2
74+
delta2 = delta1 + (delta1 `div` numpoints)
75+
(rd,rk) = head
76+
$ filter ((<=((base bs - tmin bs) * (tmax bs)) `div` 2).fst)
77+
$ iterate (\(d,k) -> (d `div` (base bs - tmin bs),k+(base bs))) (delta2,0)
78+
in rk + (((base bs - tmin bs +1) * rd) `div` (rd + skew bs))
7979

8080
decodeValue :: ByteSource m => BootString -> Int -> Int -> Int -> Int -> [Int] -> m (Int,[Int])
8181
decodeValue bs bias i k w (x:xs)
82-
| x >= base bs = throwException OutOfRange
83-
| x > (maxBound - i) `div` w = throwException OutOfRange
84-
| x < t = return (ni,xs)
85-
| w > maxBound `div` (base bs - t) = throwException OutOfRange
82+
| x >= base bs = throwException OutOfRange
83+
| x > (maxBound - i) `div` w = throwException OutOfRange
84+
| x < t = return (ni,xs)
85+
| w > maxBound `div` (base bs - t) = throwException OutOfRange
8686
| null xs = throwException OutOfRange
87-
| otherwise = decodeValue bs bias ni (k+base bs) (w*(base bs - t)) xs
88-
where
89-
ni = i + x*w
90-
t = getT bs k bias
87+
| otherwise = decodeValue bs bias ni (k+base bs) (w*(base bs - t)) xs
88+
where
89+
ni = i + x*w
90+
t = getT bs k bias
9191

9292
decodeValues :: ByteSource m => BootString -> Int -> [Int] -> m [(Char,Int)]
9393
decodeValues bs len xs = decodeValues' bs (init_n bs) 0 (init_bias bs) len xs
@@ -108,8 +108,8 @@ decodeValues' bs n i bias len xs = do
108108
insertDeltas :: [(a,Int)] -> [a] -> [a]
109109
insertDeltas [] str = str
110110
insertDeltas ((c,p):xs) str = let
111-
(l,r) = splitAt p str
112-
in insertDeltas xs (l++[c]++r)
111+
(l,r) = splitAt p str
112+
in insertDeltas xs (l++[c]++r)
113113

114114
punyDecode :: ByteSource m => [Word8] -> [Word8] -> m String
115115
punyDecode base ext = do
@@ -119,32 +119,32 @@ punyDecode base ext = do
119119

120120
encodeValue :: BootString -> Int -> Int -> Int -> Int -> [Int]
121121
encodeValue bs bias delta n c = unfoldr (\(q,k,out) -> let
122-
t = getT bs k bias
123-
(nq,dc) = (q-t) `divMod` (base bs - t)
124-
in if out
125-
then Nothing
126-
else (if q < t
127-
then Just (q,(q,k+base bs,True))
128-
else Just (t + dc,(nq,k+base bs,False)))
129-
) (delta,base bs,False)
122+
t = getT bs k bias
123+
(nq,dc) = (q-t) `divMod` (base bs - t)
124+
in if out
125+
then Nothing
126+
else (if q < t
127+
then Just (q,(q,k+base bs,True))
128+
else Just (t + dc,(nq,k+base bs,False)))
129+
) (delta,base bs,False)
130130

131131
encodeValues' :: BootString -> Int -> Int -> Int -> Int -> Int -> [Int] -> (Int,Int,Int,[Int])
132132
encodeValues' _ _ h bias delta _ [] = (delta,h,bias,[])
133133
encodeValues' bs b h bias delta n (c:cs) = case compare c n of
134-
LT -> encodeValues' bs b h bias (delta+1) n cs
135-
GT -> encodeValues' bs b h bias delta n cs
136-
EQ -> let
137-
(ndelta,nh,nbias,rest) = encodeValues' bs b (h+1) (adapt bs delta (h+1) (h==b)) 0 n cs
138-
xs = encodeValue bs bias delta n c
139-
in (ndelta,nh,nbias,xs++rest)
134+
LT -> encodeValues' bs b h bias (delta+1) n cs
135+
GT -> encodeValues' bs b h bias delta n cs
136+
EQ -> let
137+
(ndelta,nh,nbias,rest) = encodeValues' bs b (h+1) (adapt bs delta (h+1) (h==b)) 0 n cs
138+
xs = encodeValue bs bias delta n c
139+
in (ndelta,nh,nbias,xs++rest)
140140

141141
encodeValues :: BootString -> Int -> Int -> Int -> Int -> Int -> Int -> [Int] -> [Int]
142142
encodeValues bs b l h bias delta n cps
143-
| h == l = []
144-
| otherwise = outp++encodeValues bs b l nh nbias (ndelta+1) (m+1) cps
145-
where
146-
m = minimum (filter (>=n) cps)
147-
(ndelta,nh,nbias,outp) = encodeValues' bs b h bias (delta + (m - n)*(h + 1)) m cps
143+
| h == l = []
144+
| otherwise = outp++encodeValues bs b l nh nbias (ndelta+1) (m+1) cps
145+
where
146+
m = minimum (filter (>=n) cps)
147+
(ndelta,nh,nbias,outp) = encodeValues' bs b h bias (delta + (m - n)*(h + 1)) m cps
148148

149149
breakLast :: (a -> Bool) -> [a] -> Maybe ([a],[a])
150150
breakLast p xs = do
@@ -166,7 +166,7 @@ instance Encoding BootString where
166166
encodeChar _ c = error "Data.Encoding.BootString.encodeChar: Please use 'encode' for encoding BootStrings"
167167
decodeChar _ = error "Data.Encoding.BootString.decodeChar: Please use 'decode' for decoding BootStrings"
168168
encode bs str = let (base,nbase) = partition (\c -> ord c < init_n bs) str
169-
b = length base
169+
b = length base
170170
in do
171171
res <- mapM punyChar $ encodeValues bs b (length str) b (init_bias bs) 0 (init_n bs) (map ord str)
172172
when (not $ null base) $ do
@@ -178,8 +178,8 @@ instance Encoding BootString where
178178
let m = fromIntegral $ ord '-'
179179
case breakLast (==m) wrds of
180180
Just ([],_) -> throwException (IllegalCharacter m)
181-
Just (base,_:nbase) -> case find (\w -> fromIntegral w > init_n bs) base of
182-
Nothing -> punyDecode base nbase
183-
Just ww -> throwException (IllegalCharacter ww)
184-
Nothing -> punyDecode [] wrds
181+
Just (base,_:nbase) -> case find (\w -> fromIntegral w > init_n bs) base of
182+
Nothing -> punyDecode base nbase
183+
Just ww -> throwException (IllegalCharacter ww)
184+
Nothing -> punyDecode [] wrds
185185
encodeable bs c = True -- XXX: hm, really?

Data/Encoding/Exception.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import Control.Monad.Identity
99
-- | This exception type is thrown whenever something went wrong during the
1010
-- encoding-process.
1111
data EncodingException
12-
= HasNoRepresentation Char -- ^ Thrown if a specific character
13-
-- is not representable in an encoding.
12+
= HasNoRepresentation Char -- ^ Thrown if a specific character
13+
-- is not representable in an encoding.
1414
deriving (Eq,Ord,Show,Read,Typeable)
1515

1616
instance Exception EncodingException
1717

1818
-- | This exception type is thrown whenever something went wrong during the
1919
-- decoding-process.
2020
data DecodingException
21-
= IllegalCharacter Word8 -- ^ The sequence contained an illegal
22-
-- byte that couldn't be decoded.
23-
| UnexpectedEnd -- ^ more bytes were needed to allow a
24-
-- successfull decoding.
25-
| OutOfRange -- ^ the decoded value was out of the unicode range
26-
| IllegalRepresentation [Word8] -- ^ The character sequence encodes a
27-
-- character, but is illegal.
21+
= IllegalCharacter Word8 -- ^ The sequence contained an illegal
22+
-- byte that couldn't be decoded.
23+
| UnexpectedEnd -- ^ more bytes were needed to allow a
24+
-- successfull decoding.
25+
| OutOfRange -- ^ the decoded value was out of the unicode range
26+
| IllegalRepresentation [Word8] -- ^ The character sequence encodes a
27+
-- character, but is illegal.
2828
deriving (Eq,Ord,Show,Read,Typeable)
2929

3030
instance Exception DecodingException

Data/Encoding/KOI8R.hs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
See <http://en.wikipedia.org/wiki/KOI8-R> for more information.
44
-}
55
module Data.Encoding.KOI8R
6-
(KOI8R(..)) where
6+
(KOI8R(..)) where
77

88
import Control.Throws
99
import Data.Array.Unboxed
@@ -28,23 +28,23 @@ koi8rMap = fromList (zip koi8rList [128..])
2828

2929
koi8rList :: [Char]
3030
koi8rList =
31-
['\x2500','\x2502','\x250c','\x2510','\x2514','\x2518','\x251c','\x2524'
32-
,'\x252c','\x2534','\x253c','\x2580','\x2584','\x2588','\x258c','\x2590'
33-
,'\x2591','\x2592','\x2593','\x2320','\x25a0','\x2219','\x221a','\x2248'
34-
,'\x2264','\x2265','\x00a0','\x2321','\x00b0','\x00b2','\x00b7','\x00f7'
35-
,'\x2550','\x2551','\x2552','\x0451','\x2553','\x2554','\x2555','\x2556'
36-
,'\x2557','\x2558','\x2559','\x255a','\x255b','\x255c','\x255d','\x255e'
37-
,'\x255f','\x2560','\x2561','\x0401','\x2562','\x2563','\x2564','\x2565'
38-
,'\x2566','\x2567','\x2568','\x2569','\x256a','\x256b','\x256c','\x00a9'
39-
,'\x044e','\x0430','\x0431','\x0446','\x0434','\x0435','\x0444','\x0433'
40-
,'\x0445','\x0438','\x0439','\x043a','\x043b','\x043c','\x043d','\x043e'
41-
,'\x043f','\x044f','\x0440','\x0441','\x0442','\x0443','\x0436','\x0432'
42-
,'\x044c','\x044b','\x0437','\x0448','\x044d','\x0449','\x0447','\x044a'
43-
,'\x042e','\x0410','\x0411','\x0426','\x0414','\x0415','\x0424','\x0413'
44-
,'\x0425','\x0418','\x0419','\x041a','\x041b','\x041c','\x041d','\x041e'
45-
,'\x041f','\x042f','\x0420','\x0421','\x0422','\x0423','\x0416','\x0412'
46-
,'\x042c','\x042b','\x0417','\x0428','\x042d','\x0429','\x0427','\x042a'
47-
]
31+
['\x2500','\x2502','\x250c','\x2510','\x2514','\x2518','\x251c','\x2524'
32+
,'\x252c','\x2534','\x253c','\x2580','\x2584','\x2588','\x258c','\x2590'
33+
,'\x2591','\x2592','\x2593','\x2320','\x25a0','\x2219','\x221a','\x2248'
34+
,'\x2264','\x2265','\x00a0','\x2321','\x00b0','\x00b2','\x00b7','\x00f7'
35+
,'\x2550','\x2551','\x2552','\x0451','\x2553','\x2554','\x2555','\x2556'
36+
,'\x2557','\x2558','\x2559','\x255a','\x255b','\x255c','\x255d','\x255e'
37+
,'\x255f','\x2560','\x2561','\x0401','\x2562','\x2563','\x2564','\x2565'
38+
,'\x2566','\x2567','\x2568','\x2569','\x256a','\x256b','\x256c','\x00a9'
39+
,'\x044e','\x0430','\x0431','\x0446','\x0434','\x0435','\x0444','\x0433'
40+
,'\x0445','\x0438','\x0439','\x043a','\x043b','\x043c','\x043d','\x043e'
41+
,'\x043f','\x044f','\x0440','\x0441','\x0442','\x0443','\x0436','\x0432'
42+
,'\x044c','\x044b','\x0437','\x0448','\x044d','\x0449','\x0447','\x044a'
43+
,'\x042e','\x0410','\x0411','\x0426','\x0414','\x0415','\x0424','\x0413'
44+
,'\x0425','\x0418','\x0419','\x041a','\x041b','\x041c','\x041d','\x041e'
45+
,'\x041f','\x042f','\x0420','\x0421','\x0422','\x0423','\x0416','\x0412'
46+
,'\x042c','\x042b','\x0417','\x0428','\x042d','\x0429','\x0427','\x042a'
47+
]
4848

4949
instance Encoding KOI8R where
5050
decodeChar _ = do
@@ -53,8 +53,8 @@ instance Encoding KOI8R where
5353
then return $ chr $ fromIntegral w
5454
else return $ koi8rArr!w
5555
encodeChar _ ch
56-
| ch < '\128' = pushWord8 $ fromIntegral $ ord ch
57-
| otherwise = case lookup ch koi8rMap of
58-
Just w -> pushWord8 w
59-
Nothing -> throwException (HasNoRepresentation ch)
56+
| ch < '\128' = pushWord8 $ fromIntegral $ ord ch
57+
| otherwise = case lookup ch koi8rMap of
58+
Just w -> pushWord8 w
59+
Nothing -> throwException (HasNoRepresentation ch)
6060
encodeable _ c = member c koi8rMap

0 commit comments

Comments
 (0)