Copyright | (c) Azavea 2017 - 2020 |
---|---|
License | BSD3 |
Maintainer | Colin Woodbury <colin@fosskers.ca> |
Safe Haskell | Safe |
Language | Haskell2010 |
Documentation
foldBytes :: ByteString -> Word8 -> Int Source #
Discover a field's number and Wire Type. The wire type is expected to
be a value from 0 to 5. The field number itself can probably be any varint,
although in practice these are in Word8
range.
The results are left as numbers, since pattern matching on those should be faster. key :: (Num t, Bits t) => t -> (t, t) key w = (shiftR w 3, w .&. 0b00000111)
For the case when two bytes denote the field number and Wire Type. We know that for OSM data, the highest field number is 34. Encoding 34 with any wire type takes 2 bytes, so we know we'll never need to check for more. key2 :: Word8 -> Word8 -> (Word16, Word16) key2 w1 w0 = key $ shift (to16 w0) 7 .|. to16 (clearBit w1 7)
Fold a ByteString
into an Int
which was parsed with wire-type 2
(Length-delimited). These follow the pattern tagByte byteCount bytes
,
where we've parsed byteCount
and done an attoparsec take
on the bytes
.
These bytes could be a packed repeated field of varints, meaning they could
all have different byte lengths.
This function uses the rules described here for determining when to accumulate the current byte, or to consider it the first byte of the next value. In short, the rule is:
- If the MSB of a byte is 1, then expect at least the next byte to belong to this value.
- If the MSB of a byte is 0, we're at the end of the current accumulating value (or at the first byte of a single byte value).