nibblestring-0.0.1: Packed, strict nibble arrays with a list interface (ByteString for nibbles)

Safe HaskellNone
LanguageHaskell98

Data.NibbleString

Description

NibbleString is the "ByteString" of nibbles (4 bit values). It is intended to be used in the same way that ByteString is used, although, currently, only a subset of the functions have been implemented.

Internally, a NibbleString stores data similarly to ByteString, however, as per design:

For the most part, NibbleString is a convenience wrapper around ByteString

Synopsis

Documentation

type Nibble = Word8 Source

Nibbles are stored as the low four bits of a Word8.

Nothing prevents storing 1s in the upper four bits, but the functions here will (do their best to) ignore this data, or complain when it is set.

data NibbleString Source

This is the basic NibbleString data type.

A NibbleString is just a ByteString internally, but with a spot to store the extra Nibble for odd length strings.

empty :: NibbleString Source

O(1) Returns the NibbleString of length 0.

singleton :: Nibble -> NibbleString Source

O(1) Creates a NibbleString of length 1 using the given Nibble.

null :: NibbleString -> Bool Source

O(1) Returns True if the NibbleString has size 0.

length :: NibbleString -> Int Source

O(1) Returns the number of Nibbles stored in the NibbleString.

The value returned will be double that stored in an identical ByteString.

pack :: [Nibble] -> NibbleString Source

O(n) Convert a [Nibble] into a NibbleString.

unpack :: NibbleString -> [Nibble] Source

O(n) Convert a NibbleString into a [Nibble].

byte2Nibbles :: Word8 -> [Nibble] Source

O(1) Convert one byte to a list of 2 Nibbles.

This can be useful in converting lists of bytes to a NibbleString.

nibbleString = pack $ byte2Nibbles =<< listOfBytes

isPrefixOf :: NibbleString -> NibbleString -> Bool Source

O(n) Returns True if the first NibbleString is a prefix of the second.

head :: NibbleString -> Nibble Source

O(1) Returns the first Nibble in a NibbleString.

tail :: NibbleString -> NibbleString Source

O(1) Returns the NibbleString remaining after removing the head Nibble.

drop :: Int -> NibbleString -> NibbleString Source

O(1) drop n returns a new NibbleString by dropping the first n Nibbles from the given NibbleString.

append :: NibbleString -> NibbleString -> NibbleString Source

O(n) Creates a new NibbleString by appending one to another.