summaryrefslogtreecommitdiff
path: root/lib/Language/Elna/Object/StringTable.hs
blob: e75f2c6351c28299f985838b8b565e7d86d7f21e (plain)
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
module Language.Elna.Object.StringTable
    ( StringTable
    , append
    , elem
    , index
    , encode
    , size
    ) where

import Data.ByteString (StrictByteString)
import qualified Data.ByteString as ByteString
import Language.Elna.Object.Elf
import Prelude hiding (elem)

newtype StringTable = StringTable StrictByteString
    deriving Eq

instance Semigroup StringTable
  where
    (StringTable x) <> (StringTable y) = StringTable $ x <> ByteString.drop 1 y

instance Monoid StringTable
  where
    mempty = StringTable "\0"

size :: StringTable -> Elf32_Word
size (StringTable container) =
    fromIntegral $ ByteString.length container

elem :: StrictByteString -> StringTable -> Bool
elem needle (StringTable container) =
    ("\0" <> needle <> "\0") `ByteString.isInfixOf` container

append :: StrictByteString -> StringTable -> StringTable
append element (StringTable container) =
    StringTable $ container <> element <> "\0"

index :: Elf32_Word -> StringTable -> StrictByteString
index stringTableIndex (StringTable stringTable)
    = ByteString.takeWhile (/= 0)
    $ ByteString.drop (fromIntegral stringTableIndex) stringTable

encode :: StringTable -> StrictByteString
encode (StringTable container) = container