Safe Haskell | None |
---|---|
Language | Haskell2010 |
Deprecated: This module is no longer used by the rest of the proto3-wire package.
This module extends the Data.ByteString.Builder module by memoizing the
resulting length of each Builder
Example use:
>>>
Data.ByteString.Lazy.unpack (toLazyByteString (word32BE 42 <> charUtf8 'λ'))
[0,0,0,42,206,187]
Synopsis
- data Builder
- byteString :: ByteString -> Builder
- lazyByteString :: ByteString -> Builder
- shortByteString :: ShortByteString -> Builder
- word8 :: Word8 -> Builder
- word16BE :: Word16 -> Builder
- word16LE :: Word16 -> Builder
- word32BE :: Word32 -> Builder
- word32LE :: Word32 -> Builder
- word64BE :: Word64 -> Builder
- word64LE :: Word64 -> Builder
- word64Base128LEVar :: Word64 -> Builder
- int8 :: Int8 -> Builder
- int16BE :: Int16 -> Builder
- int16LE :: Int16 -> Builder
- int32BE :: Int32 -> Builder
- int32LE :: Int32 -> Builder
- int64BE :: Int64 -> Builder
- int64LE :: Int64 -> Builder
- floatBE :: Float -> Builder
- floatLE :: Float -> Builder
- doubleBE :: Double -> Builder
- doubleLE :: Double -> Builder
- char7 :: Char -> Builder
- string7 :: String -> Builder
- char8 :: Char -> Builder
- string8 :: String -> Builder
- charUtf8 :: Char -> Builder
- stringUtf8 :: String -> Builder
- builderLength :: Builder -> Word
- rawBuilder :: Builder -> Builder
- toLazyByteString :: Builder -> ByteString
- hPutBuilder :: Handle -> Builder -> IO ()
- unsafeMakeBuilder :: Word -> Builder -> Builder
Builder
type
A Builder
is like a Data.ByteString.Builder.
, but also
memoizes the resulting length so that we can efficiently encode nested
embedded messages.Builder
You create a Builder
by using one of the primitives provided in the
"Create Builder
s" section.
You combine Builder
s using the Monoid
and Semigroup
instances.
You consume a Builder
by using one of the utilities provided in the
"Consume Builder
s" section.
Create Builder
s
byteString :: ByteString -> Builder Source #
Convert a strict ByteString
to a Builder
byteString (x <> y) = byteString x <> byteString y byteString mempty = mempty
>>>
byteString "ABC"
Proto3.Wire.Builder.lazyByteString "ABC"
lazyByteString :: ByteString -> Builder Source #
Convert a lazy ByteString
to a Builder
Warning: evaluating the length will force the lazy ByteString
's chunks,
and they will remain allocated until you finish using the builder.
lazyByteString (x <> y) = lazyByteString x <> lazyByteString y lazyByteString mempty = mempty
lazyByteString . toLazyByteString = id toLazyByteString . lazyByteString = id
>>>
lazyByteString "ABC"
Proto3.Wire.Builder.lazyByteString "ABC"
shortByteString :: ShortByteString -> Builder Source #
Convert a ShortByteString
to a Builder
shortByteString (x <> y) = shortByteString x <> shortByteString y shortByteString mempty = mempty
>>>
shortByteString "ABC"
Proto3.Wire.Builder.lazyByteString "ABC"
word64Base128LEVar :: Word64 -> Builder Source #
Convert a Word64
to a Builder
using this variable-length encoding:
- Convert the given value to a base 128 representation without unnecessary digits (that is, omit zero digits unless they are less significant than nonzero digits).
- Present those base-128 digits in order of increasing significance (that is, in little-endian order).
- Add 128 to every digit except the most significant digit, yielding a sequence of octets terminated by one that is <= 127.
This encoding is used in the wire format of Protocol Buffers version 3.
string7 :: String -> Builder Source #
Convert an ASCII
String
to a Builder
Careful: If you provide a Unicode String
that has non-ASCII
characters then this will only encode the lowest 7 bits of each character
string7 (x <> y) = string7 x <> string7 y string7 mempty = mempty
>>>
string7 "ABC"
Proto3.Wire.Builder.lazyByteString "ABC">>>
string7 "←↑→↓" -- Example of truncation
Proto3.Wire.Builder.lazyByteString "\DLE\DC1\DC2\DC3"
char8 :: Char -> Builder Source #
Convert an ISO/IEC 8859-1
Char
to a Builder
Careful: If you provide a Unicode character that is not part of the
ISO/IEC 8859-1
alphabet then this will only encode the lowest 8 bits
>>>
char8 ';'
Proto3.Wire.Builder.lazyByteString ";">>>
char8 'λ' -- Example of truncation
Proto3.Wire.Builder.lazyByteString "\187"
string8 :: String -> Builder Source #
Convert an ISO/IEC 8859-1
String
to a Builder
Careful: If you provide a Unicode String
that has non-ISO/IEC 8859-1
characters then this will only encode the lowest 8 bits of each character
string8 (x <> y) = string8 x <> string8 y string8 mempty = mempty
>>>
string8 "ABC"
Proto3.Wire.Builder.lazyByteString "ABC">>>
string8 "←↑→↓" -- Example of truncation
Proto3.Wire.Builder.lazyByteString "\144\145\146\147"
stringUtf8 :: String -> Builder Source #
Convert a Unicode String
to a Builder
using a UTF-8
encoding
stringUtf8 (x <> y) = stringUtf8 x <> stringUtf8 y stringUtf8 mempty = mempty
>>>
stringUtf8 "ABC"
Proto3.Wire.Builder.lazyByteString "ABC">>>
stringUtf8 "←↑→↓"
Proto3.Wire.Builder.lazyByteString "\226\134\144\226\134\145\226\134\146\226\134\147">>>
hPutBuilder System.IO.stdout (stringUtf8 "←↑→↓\n")
←↑→↓
Consume Builder
s
builderLength :: Builder -> Word Source #
Retrieve the length of a Builder
builderLength (x <> y) = builderLength x + builderLength y builderLength mempty = 0
>>>
builderLength (word32BE 42)
4>>>
builderLength (stringUtf8 "ABC")
3
rawBuilder :: Builder -> Builder Source #
Retrieve the underlying Data.ByteString.Builder.
Builder
rawBuilder (x <> y) = rawBuilder x <> rawBuilder y rawBuilder mempty = mempty
>>>
Data.ByteString.Builder.toLazyByteString (rawBuilder (stringUtf8 "ABC"))
"ABC"
toLazyByteString :: Builder -> ByteString Source #
Create a lazy ByteString
from a Builder
toLazyByteString (x <> y) = toLazyByteString x <> toLazyByteString y toLazyByteString mempty = mempty
>>>
toLazyByteString (stringUtf8 "ABC")
"ABC"
Internal API
unsafeMakeBuilder :: Word -> Builder -> Builder Source #
Create a Builder
from a Data.ByteString.Builder.
and a
length. This is unsafe because you are responsible for ensuring that the
provided length value matches the length of the
Builder
Data.ByteString.Builder.
Builder
>>>
unsafeMakeBuilder 3 (Data.ByteString.Builder.stringUtf8 "ABC")
Proto3.Wire.Builder.lazyByteString "ABC"