streaming-osm-1.0.2: A hand-written streaming byte parser for OpenStreetMap Protobuf data.

Copyright(c) Azavea 2017 - 2020
LicenseBSD3
MaintainerColin Woodbury <colin@fosskers.ca>
Safe HaskellSafe
LanguageHaskell2010

Streaming.Osm.Internal.Util

Description

 
Synopsis

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:

  1. If the MSB of a byte is 1, then expect at least the next byte to belong to this value.
  2. 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).

breakOn0 :: [Int] -> [[Int]] Source #

words for Int, where 0 is the whitespace. Implementation adapted from words.

pairs :: [a] -> [(a, a)] Source #

A sort of "self-zip", forming pairs from every two elements in a list. Assumes that the list is of even length.

both :: (a -> b) -> (a, a) -> (b, b) Source #

Apply a function to both elements of a tuple.

unzig :: Int -> Int Source #

Decode a Z-encoded Int.

undelta :: [Int] -> [Int] Source #

Restore a list of numbers that have been Delta Encoded.