Skip to content

Commit 07d9bc6

Browse files
committed
Add Kore.Builtin.Encoding.parse8Bit
1 parent 096daa1 commit 07d9bc6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

kore/src/Kore/Builtin/Encoding.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ License : NCSA
66
module Kore.Builtin.Encoding
77
( encode8Bit
88
, decode8Bit
9+
, parse8Bit
910
, parseBase16
1011
, toBase16
1112
) where
@@ -33,6 +34,7 @@ import Data.Word
3334
)
3435
import Text.Megaparsec
3536
( Parsec
37+
, (<?>)
3638
)
3739
import qualified Text.Megaparsec as Parsec
3840

@@ -62,6 +64,23 @@ encode8Bit =
6264
, show int
6365
]
6466

67+
{- | Encode text using an 8-bit encoding.
68+
69+
Each 'Char' in the text is interpreted as a 'Data.Word.Word8'. It is an error if
70+
any character falls outside that representable range.
71+
72+
-}
73+
parse8Bit :: Parsec Void Text ByteString
74+
parse8Bit =
75+
ByteString.pack <$> many parseByte
76+
where
77+
parseByte :: Parsec Void Text Word8
78+
parseByte =
79+
fromIntegral . Char.ord <$> Parsec.satisfy is8Bit <?> "8-bit value"
80+
81+
is8Bit :: Char -> Bool
82+
is8Bit c = c < '\x100'
83+
6584
decode8Bit :: ByteString -> Text
6685
decode8Bit =
6786
ByteString.unpack

0 commit comments

Comments
 (0)