Skip to content

Commit

Permalink
Split out basic ByteString stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
augustss committed Sep 11, 2024
1 parent 3704ae8 commit 0cbaa2b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 84 deletions.
84 changes: 1 addition & 83 deletions lib/Data/ByteString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -167,94 +167,18 @@ import Foreign.C.String(CString, CStringLen)
import System.IO(Handle, IOMode(..), stdin, stdout)
import qualified System.IO as P
import Foreign.ForeignPtr
import Data.ByteString.Internal

data ByteString -- primitive type
type StrictByteString = ByteString

primBSappend :: ByteString -> ByteString -> ByteString
primBSappend = primitive "bs++"
primBSappend3 :: ByteString -> ByteString -> ByteString -> ByteString
primBSappend3 = primitive "bs+++"
primBSEQ :: ByteString -> ByteString -> Bool
primBSEQ = primitive "bs=="
primBSNE :: ByteString -> ByteString -> Bool
primBSNE = primitive "bs/="
primBSLT :: ByteString -> ByteString -> Bool
primBSLT = primitive "bs<"
primBSLE :: ByteString -> ByteString -> Bool
primBSLE = primitive "bs<="
primBSGT :: ByteString -> ByteString -> Bool
primBSGT = primitive "bs>"
primBSGE :: ByteString -> ByteString -> Bool
primBSGE = primitive "bs>="
primBScmp :: ByteString -> ByteString -> Ordering
primBScmp = primitive "bscmp"
primBSpack :: [Word8] -> ByteString
primBSpack = primitive "bspack"
primBSunpack :: ByteString -> [Word8]
primBSunpack = primitive "bsunpack"
primBSlength :: ByteString -> Int
primBSlength = primitive "bslength"
primBSsubstr :: ByteString -> Int -> Int -> ByteString
primBSsubstr = primitive "bssubstr"
primBS2FPtr :: ByteString -> ForeignPtr Char
primBS2FPtr = primitive "I" -- same representation

-----------------------------------------

instance Eq ByteString where
(==) = primBSEQ
(/=) = primBSNE

instance Ord ByteString where
compare = primBScmp
(<) = primBSLT
(<=) = primBSLE
(>) = primBSGT
(>=) = primBSGE

instance Show ByteString where
showsPrec p bs = showsPrec p (toString bs)

instance IsString ByteString where
fromString = pack . P.map (toEnum . fromEnum)

instance Semigroup ByteString where
(<>) = append

instance Monoid ByteString where
mempty = empty

substr :: ByteString -> Int -> Int -> ByteString
substr bs offs len
| offs < 0 || offs > sz = bsError "substr bad offset"
| len < 0 || len > sz-offs = bsError "substr bad length"
| otherwise = primBSsubstr bs offs len
where sz = length bs

bsError :: String -> a
bsError s = P.error $ "Data.ByteString." P.++ s

bsUnimp :: String -> a
bsUnimp s = P.error $ "Data.ByteString." P.++ s P.++ " unimplemented"

toString :: ByteString -> String
toString = P.map (toEnum . fromEnum) . unpack

-----------------------------------------

empty :: ByteString
empty = pack []

singleton :: Word8 -> ByteString
singleton c = pack [c]

pack :: [Word8] -> ByteString
pack = primBSpack

unpack :: ByteString -> [Word8]
unpack = primBSunpack

fromStrict = bsUnimp "fromStrict"
toStrict = bsUnimp "toStrict"

Expand Down Expand Up @@ -300,12 +224,6 @@ unsnoc bs | null bs = Nothing
null :: ByteString -> Bool
null bs = length bs == 0

length :: ByteString -> Int
length = primBSlength

append :: ByteString -> ByteString -> ByteString
append = primBSappend

map :: (Word8 -> Word8) -> ByteString -> ByteString
map f = pack . P.map f . unpack

Expand Down
89 changes: 89 additions & 0 deletions lib/Data/ByteString/Internal.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
module Data.ByteString.Internal(module Data.ByteString.Internal) where
import Prelude hiding(length)
import Data.Word(Word8)

data ByteString -- primitive type

primBSappend :: ByteString -> ByteString -> ByteString
primBSappend = primitive "bs++"
primBSappend3 :: ByteString -> ByteString -> ByteString -> ByteString
primBSappend3 = primitive "bs+++"
primBSEQ :: ByteString -> ByteString -> Bool
primBSEQ = primitive "bs=="
primBSNE :: ByteString -> ByteString -> Bool
primBSNE = primitive "bs/="
primBSLT :: ByteString -> ByteString -> Bool
primBSLT = primitive "bs<"
primBSLE :: ByteString -> ByteString -> Bool
primBSLE = primitive "bs<="
primBSGT :: ByteString -> ByteString -> Bool
primBSGT = primitive "bs>"
primBSGE :: ByteString -> ByteString -> Bool
primBSGE = primitive "bs>="
primBScmp :: ByteString -> ByteString -> Ordering
primBScmp = primitive "bscmp"
primBSpack :: [Word8] -> ByteString
primBSpack = primitive "bspack"
primBSunpack :: ByteString -> [Word8]
primBSunpack = primitive "bsunpack"
primBSlength :: ByteString -> Int
primBSlength = primitive "bslength"
primBSsubstr :: ByteString -> Int -> Int -> ByteString
primBSsubstr = primitive "bssubstr"

-----------------------------------------

instance Eq ByteString where
(==) = primBSEQ
(/=) = primBSNE

instance Ord ByteString where
compare = primBScmp
(<) = primBSLT
(<=) = primBSLE
(>) = primBSGT
(>=) = primBSGE

instance Show ByteString where
showsPrec p bs = showsPrec p (toString bs)

instance IsString ByteString where
fromString = pack . map (toEnum . fromEnum)

instance Semigroup ByteString where
(<>) = append

instance Monoid ByteString where
mempty = empty

toString :: ByteString -> String
toString = map (toEnum . fromEnum) . unpack

empty :: ByteString
empty = pack []

singleton :: Word8 -> ByteString
singleton c = pack [c]

length :: ByteString -> Int
length = primBSlength

append :: ByteString -> ByteString -> ByteString
append = primBSappend

substr :: ByteString -> Int -> Int -> ByteString
substr bs offs len
| offs < 0 || offs > sz = bsError "substr bad offset"
| len < 0 || len > sz-offs = bsError "substr bad length"
| otherwise = primBSsubstr bs offs len
where sz = length bs

bsError :: String -> a
bsError s = error $ "Data.ByteString." ++ s

pack :: [Word8] -> ByteString
pack = primBSpack

unpack :: ByteString -> [Word8]
unpack = primBSunpack

2 changes: 1 addition & 1 deletion lib/Data/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Prelude hiding(head)
import Data.Monoid
import Data.Semigroup
import Data.String
import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BS

newtype Text = T BS.ByteString

Expand Down

0 comments on commit 0cbaa2b

Please sign in to comment.