Skip to content

Commit

Permalink
Add IsList instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodigrim committed Jul 2, 2020
1 parent 76f56d2 commit ad79d55
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.10.10.2 –

* Add `takeWhileEnd`, `dropWhileEnd` and `strip` for strict bytestrings
* Add `IsList` instances

0.10.10.1 – June 2020

Expand Down
12 changes: 12 additions & 0 deletions Data/ByteString/Internal.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE CPP, ForeignFunctionInterface, BangPatterns #-}
{-# LANGUAGE UnliftedFFITypes, MagicHash,
UnboxedTuples, DeriveDataTypeable #-}
{-# LANGUAGE TypeFamilies #-}
#if __GLASGOW_HASKELL__ >= 703
{-# LANGUAGE Unsafe #-}
#endif
Expand Down Expand Up @@ -117,6 +118,10 @@ import Data.Data (Data(..), mkNoRepType)

import GHC.Base (nullAddr#,realWorld#,unsafeChr)

#if MIN_VERSION_base(4,7,0)
import GHC.Exts (IsList(..))
#endif

#if MIN_VERSION_base(4,4,0)
import GHC.CString (unpackCString#)
#else
Expand Down Expand Up @@ -182,6 +187,13 @@ instance Show ByteString where
instance Read ByteString where
readsPrec p str = [ (packChars x, y) | (x, y) <- readsPrec p str ]

#if MIN_VERSION_base(4,7,0)
instance IsList ByteString where
type Item ByteString = Word8
fromList = packBytes
toList = unpackBytes
#endif

instance IsString ByteString where
fromString = packChars

Expand Down
14 changes: 13 additions & 1 deletion Data/ByteString/Lazy/Internal.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TypeFamilies #-}
#if __GLASGOW_HASKELL__ >= 703
{-# LANGUAGE Unsafe #-}
#endif
Expand All @@ -13,7 +14,7 @@
-- Maintainer : dons00@gmail.com, duncan@community.haskell.org
-- Stability : unstable
-- Portability : non-portable
--
--
-- A module containing semi-public 'ByteString' internals. This exposes
-- the 'ByteString' representation and low level construction functions.
-- Modules which extend the 'ByteString' system will need to use this module
Expand Down Expand Up @@ -68,6 +69,10 @@ import Data.String (IsString(..))
import Data.Typeable (Typeable)
import Data.Data (Data(..), mkNoRepType)

#if MIN_VERSION_base(4,7,0)
import GHC.Exts (IsList(..))
#endif

-- | A space-efficient representation of a 'Word8' vector, supporting many
-- efficient operations.
--
Expand Down Expand Up @@ -110,6 +115,13 @@ instance Show ByteString where
instance Read ByteString where
readsPrec p str = [ (packChars x, y) | (x, y) <- readsPrec p str ]

#if MIN_VERSION_base(4,7,0)
instance IsList ByteString where
type Item ByteString = Word8
fromList = packBytes
toList = unpackBytes
#endif

instance IsString ByteString where
fromString = packChars

Expand Down
12 changes: 10 additions & 2 deletions Data/ByteString/Short/Internal.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE DeriveDataTypeable, CPP, BangPatterns, RankNTypes,
ForeignFunctionInterface, MagicHash, UnboxedTuples,
UnliftedFFITypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
#if __GLASGOW_HASKELL__ >= 703
{-# LANGUAGE Unsafe #-}
Expand All @@ -15,7 +16,7 @@
-- Maintainer : duncan@community.haskell.org
-- Stability : stable
-- Portability : ghc only
--
--
-- Internal representation of ShortByteString
--
module Data.ByteString.Short.Internal (
Expand Down Expand Up @@ -170,6 +171,13 @@ instance Show ShortByteString where
instance Read ShortByteString where
readsPrec p str = [ (packChars x, y) | (x, y) <- readsPrec p str ]

#if MIN_VERSION_base(4,7,0)
instance GHC.Exts.IsList ShortByteString where
type Item ShortByteString = Word8
fromList = packBytes
toList = unpackBytes
#endif

instance IsString ShortByteString where
fromString = packChars

Expand Down Expand Up @@ -198,7 +206,7 @@ length (SBS _ len) = len
null :: ShortByteString -> Bool
null sbs = length sbs == 0

-- | /O(1)/ 'ShortByteString' index (subscript) operator, starting from 0.
-- | /O(1)/ 'ShortByteString' index (subscript) operator, starting from 0.
index :: ShortByteString -> Int -> Word8
index sbs i
| i >= 0 && i < length sbs = unsafeIndex sbs i
Expand Down

0 comments on commit ad79d55

Please sign in to comment.