packages feed

nibblestring 0.0.1 → 0.0.3

raw patch · 2 files changed

+37/−3 lines, 2 filesdep +ansi-wl-pprintPVP ok

version bump matches the API change (PVP)

Dependencies added: ansi-wl-pprint

API changes (from Hackage documentation)

+ Data.NibbleString: cons :: Nibble -> NibbleString -> NibbleString
+ Data.NibbleString: instance Pretty NibbleString
+ Data.NibbleString: take :: Int -> NibbleString -> NibbleString

Files

nibblestring.cabal view
@@ -1,5 +1,5 @@ name: nibblestring-version: 0.0.1+version: 0.0.3 cabal-version: >=1.10 build-type: Simple author: Jamshid@@ -22,7 +22,7 @@   type:     git   location: https://github.com/jamshidh/nibblestring   branch:   master-  tag:      v0.0.2+  tag:      v0.0.3   library     default-language: Haskell98@@ -30,6 +30,7 @@                    base >= 4 && < 5                  , bytestring -any                  , base16-bytestring+                 , ansi-wl-pprint     exposed-modules:                    Data.NibbleString     ghc-options: -Wall
src/Data/NibbleString.hs view
@@ -24,20 +24,24 @@   isPrefixOf,   head,   tail,+  cons,+  take,   drop,   append   ) where -import Prelude hiding (head, tail, length, drop, null)+import Prelude hiding (head, tail, length, take, drop, null) import qualified Prelude  import Data.Bits import qualified Data.ByteString as B import qualified Data.ByteString.Base16 as B16 import qualified Data.ByteString.Char8 as BC+import qualified Data.List as L (take) import Data.String import Data.Word import Numeric+import Text.PrettyPrint.ANSI.Leijen hiding ((<$>), empty)  -- | Nibbles are stored as the low four bits of a Word8. --@@ -49,6 +53,12 @@ -- A 'NibbleString' is just a 'ByteString' internally, but with a spot to store the extra Nibble for odd length strings. data NibbleString = EvenNibbleString B.ByteString | OddNibbleString Nibble B.ByteString deriving (Show, Eq, Ord) +instance Pretty NibbleString where+  pretty (EvenNibbleString s) = blue $ text $ BC.unpack (B16.encode s)+  pretty (OddNibbleString c s) =+    blue $ text $ showHex c "" ++ BC.unpack (B16.encode s)++ instance IsString NibbleString where   fromString "" = EvenNibbleString B.empty   fromString s | even $ Prelude.length s = case B16.decode $ BC.pack s of@@ -149,7 +159,14 @@     where       c1 = B.head s1 `shiftR` 4 +-- | /O(n)/ @cons n s@ returns a new 'NibbleString' by prepending n to the given 'NibbleString'.+--+-- For $s$ of even length, the operation occurs in /O(1)/, however for odd length, the underlying bytearray needs to be copied.+cons::Nibble->NibbleString->NibbleString+cons n (EvenNibbleString s) = OddNibbleString n s+cons n1 (OddNibbleString n2 s) = EvenNibbleString ((n1 `shiftL` 4 + n2) `B.cons` s) + -- | /O(1)/ @drop n@ returns a new 'NibbleString' by dropping the first n Nibbles from the given 'NibbleString'. drop::Int->NibbleString->NibbleString drop 0 s = s@@ -159,3 +176,19 @@ drop n (EvenNibbleString s) = drop 1 $ EvenNibbleString (B.drop ((n - 1) `shiftR` 1) s) drop n (OddNibbleString _ s) | even n = drop (n-1) $ EvenNibbleString s drop n (OddNibbleString _ s) = drop (n - 1) $ EvenNibbleString s++-- | /O(n)/ @take n@ returns a new 'NibbleString' by dropping the first n Nibbles from the given 'NibbleString'.+--+-- Note- This works similarly to the ByteString version of take, although it runs at (worst case) in O(n).+-- The reason for this, is, because if the even-odd nibbles are misaligned after the take, the whole array needs to+-- be copied to shift things over correctly.+take::Int->NibbleString->NibbleString+--Fast /O(1)/ stuff+take 0 _ = empty+take 1 s = singleton $ head s+take n s | n > length s = s+take n (EvenNibbleString s) | even n = EvenNibbleString (B.take (n `shiftR` 1) s)+take n (OddNibbleString c s) | odd n = OddNibbleString c (B.take ((n-1) `shiftR` 1) s)+--Slow /O(n)/ stuff+take n s = pack $ L.take n $ unpack s+