File: String.hs

package info (click to toggle)
haskell-intern 0.9.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 172 kB
  • sloc: haskell: 952; makefile: 2; sh: 1
file content (49 lines) | stat: -rw-r--r-- 1,295 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE TypeFamilies, FlexibleInstances #-}
module Data.Interned.Internal.String
  ( InternedString(..)
  ) where

import Data.String
import Data.Interned
import Data.Hashable
import qualified Data.Foldable as F

data InternedString = IS
  { internedStringId :: {-# UNPACK #-} !Id
  , uninternString :: String
  }

instance IsString InternedString where
  fromString = intern

instance Eq InternedString where
  IS i _ == IS j _ = i == j

instance Ord InternedString where
  compare (IS i _) (IS j _) = compare i j

instance Show InternedString where
  showsPrec d (IS _ b) = showsPrec d b

instance Hashable InternedString where
  hashWithSalt s (IS i _) = hashWithSalt s i

instance Interned InternedString where
  type Uninterned InternedString = String
  data Description InternedString = Cons {-# UNPACK #-} !Char String | Nil
    deriving (Eq)
  describe (c:cs) = Cons c cs
  describe []     = Nil
  identify = IS
  cache = stringCache

instance Uninternable InternedString where
  unintern = uninternString

instance Hashable (Description InternedString) where
  hashWithSalt s (Cons c cs) = F.foldl' hashWithSalt (hashWithSalt s c) cs
  hashWithSalt s Nil         = s `hashWithSalt` (0 :: Int)

stringCache :: Cache InternedString
stringCache = mkCache
{-# NOINLINE stringCache #-}