pw_varint#

The pw_varint module provides functions for encoding and decoding variable length integers, or varints. For smaller values, varints require less memory than a fixed-size encoding. For example, a 32-bit (4-byte) integer requires 1–5 bytes when varint-encoded.

Protocol Buffers use a variable-length encoding for integers.

Compatibility#

Pigweed AI summary: The paragraph summarizes the compatibility of a certain software library with different programming languages. It mentions that the library is compatible with C and C++14 (with the use of a specific tool called "pw_polyfill"). It also states that the library is compatible with Rust, providing a reference to the Rust documentation for further information.

API#

constexpr size_t pw::varint::EncodedSize(uint64_t integer)#

Computes the size of an integer when encoded as a varint.

return:

The size of integer when encoded as a varint.

Parameters:

integer – The integer whose encoded size is to be computed. integer can be signed or unsigned.

constexpr size_t pw::varint::ZigZagEncodedSize(int64_t integer)#

Returns the size of a signed integer when ZigZag-encoded as a variable-length integer (varint).

return:

The size of integer when ZigZag-encoded as a varint.

Parameters:

integer – The signed integer that will be ZigZag-encoded as a varint.

constexpr uint64_t pw::varint::MaxValueInBytes(size_t bytes)#

Returns the maximum (max) integer value that can be encoded as a varint into the specified number of bytes.

The following table lists the max value for each byte size:

Bytes

Max value

1

127

2

16,383

3

2,097,151

4

268,435,455

5

34,359,738,367

6

4,398,046,511,103

7

562,949,953,421,311

8

72,057,594,037,927,935

9

9,223,372,036,854,775,807

10

(uint64 max value)

return:

The max integer value for a varint of size bytes.

Parameters:

bytes – The size of the varint, in bytes. 5 bytes are needed for the max uint32 value. 10 bytes are needed for the max uint64 value.

Stream API#

StatusWithSize pw::varint::Read(stream::Reader &reader, uint64_t *output, size_t max_size = std::numeric_limits<size_t>::max())#
StatusWithSize pw::varint::Read(stream::Reader &reader, int64_t *output, size_t max_size = std::numeric_limits<size_t>::max())#

Decodes a variable-length integer (varint) from the current position of a pw::stream. Reads a maximum of 10 bytes or max_size, whichever is smaller.

return:

The number of bytes read from the stream if successful. The value is placed in output. Returns OutOfRange if the varint does not fit into output. Also returns OutOfRange if the input is exhausted before the number terminates.

Parameters:
  • reader – The pw::stream to read from.

  • output – The integer to read into. If reading into a signed integer, the value is ZigZag-decoded.

  • max_size – The maximum number of bytes to read. The upper limit is 10 bytes.

Dependencies#

Pigweed AI summary: This section lists the dependencies of the project, with only one item being "pw_span".

  • pw_span

Zephyr#

Pigweed AI summary: The Zephyr platform can enable the use of "pw_varint" by adding "CONFIG_PIGWEED_VARINT=y" to the project's configuration.

To enable pw_varint for Zephyr add CONFIG_PIGWEED_VARINT=y to the project’s configuration.

Rust#

Pigweed AI summary: The paragraph states that the Rust API for "pw_varint" is documented in the "rustdoc API docs". However, there is a note to update the reference to point to the correct location.

pw_varint’s Rust API is documented in our rustdoc API docs.