buffer-builder-0.2.0.1: Library for efficiently building up buffers, one piece at a time

Data.BufferBuilder

Description

A library for efficiently building up a buffer of data. When given data known to be strict, use of BufferBuilder compiles directly into a series of efficient C function calls.

Synopsis

data BufferBuilder a Source #

BufferBuilder is the type of a monadic action that appends to an implicit, growable buffer. Use runBufferBuilder to extract the resulting buffer as a ByteString.

Instances

 Source # Methods(>>=) :: BufferBuilder a -> (a -> BufferBuilder b) -> BufferBuilder b #(>>) :: BufferBuilder a -> BufferBuilder b -> BufferBuilder b #return :: a -> BufferBuilder a # Source # Methodsfmap :: (a -> b) -> BufferBuilder a -> BufferBuilder b #(<\$) :: a -> BufferBuilder b -> BufferBuilder a # Source # Methodspure :: a -> BufferBuilder a #(<*>) :: BufferBuilder (a -> b) -> BufferBuilder a -> BufferBuilder b #(*>) :: BufferBuilder a -> BufferBuilder b -> BufferBuilder b #(<*) :: BufferBuilder a -> BufferBuilder b -> BufferBuilder a #

Run a sequence of BufferBuilder actions and extract the resulting buffer as a ByteString.

# Appending bytes and byte strings

Append a single byte to the output buffer. To append multiple bytes in sequence and avoid redundant bounds checks, consider using appendBS, appendLiteral, or unsafeAppendLiteralN.

Appends a character to the buffer, truncating it to the bottom 8 bits.

Appends a ByteString to the buffer. When appending constant, hardcoded strings, to avoid a CAF and the costs of its associated tag check and indirect jump, use appendLiteral or unsafeAppendLiteralN instead.

Appends a lazy ByteString to the buffer. This function operates by traversing the lazy ByteString chunks, appending each in turn.

Appends a zero-terminated MagicHash string literal. Use this function instead of appendBS for string constants. For example:

appendLiteral "true"#

If the length of the string literal is known, calling unsafeAppendLiteralN is faster, as unsafeAppendLiteralN avoids a strlen operation which has nontrivial cost in some benchmarks.

Appends a MagicHash string literal with a known length. Use this when the string literal's length is known. For example:

unsafeAppendLiteralN 4 "true"#

Per byte, this is the fastest append function. It amounts to a C function call with two constant arguments. The C function checks to see if it needs to grow the buffer and then it simply calls memcpy.

WARNING: passing an incorrect length value is likely to cause an access violation or worse.

# UTF-8 encoding

Appends a UTF-8-encoded Char to the buffer.

Appends a UTF-8-encoded String to the buffer. The best way to improve performance here is to use ByteString or Text instead of String.

# Printing numbers

Appends a decimal integer, just like calling printf("%d", ...)

Appends a decimal double, just like calling printf("%f", ...)