Safe Haskell | None |
---|---|
Language | Haskell98 |
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:
- Each byte of data in a
NibbleString
contributes 2 to the length. - When unpacked, each byte of data in a
NibbleString
creates 2Nibble
items. - Unlike a
ByteString
, aNibbleString
can hold an odd number of Nibbles.
For the most part, NibbleString
is a convenience wrapper around ByteString
- type Nibble = Word8
- data NibbleString
- empty :: NibbleString
- singleton :: Nibble -> NibbleString
- null :: NibbleString -> Bool
- length :: NibbleString -> Int
- pack :: [Nibble] -> NibbleString
- unpack :: NibbleString -> [Nibble]
- byte2Nibbles :: Word8 -> [Nibble]
- isPrefixOf :: NibbleString -> NibbleString -> Bool
- head :: NibbleString -> Nibble
- tail :: NibbleString -> NibbleString
- drop :: Int -> NibbleString -> NibbleString
- append :: NibbleString -> NibbleString -> NibbleString
Documentation
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.
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.