summaryrefslogtreecommitdiff
path: root/lib/Language/Elna/Object/StringTable.hs
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2024-10-22 01:21:02 +0200
committerEugen Wissner <belka@caraus.de>2024-10-22 01:21:02 +0200
commit57436f664e7d138bd915fb30f486e4bb802d74b6 (patch)
tree3acdc41c0166cc0d515b37169420e429ad8878df /lib/Language/Elna/Object/StringTable.hs
parentbf5ec1f3e2325e28154b9796532d37ee84753349 (diff)
downloadelna-57436f664e7d138bd915fb30f486e4bb802d74b6.tar.gz
Abstract the string table into a newtype
Diffstat (limited to 'lib/Language/Elna/Object/StringTable.hs')
-rw-r--r--lib/Language/Elna/Object/StringTable.hs44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/Language/Elna/Object/StringTable.hs b/lib/Language/Elna/Object/StringTable.hs
new file mode 100644
index 0000000..e75f2c6
--- /dev/null
+++ b/lib/Language/Elna/Object/StringTable.hs
@@ -0,0 +1,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