1.. _module-pw_varint:
2
3---------
4pw_varint
5---------
6The ``pw_varint`` module provides functions for encoding and decoding variable
7length integers, or varints. For smaller values, varints require less memory
8than a fixed-size encoding. For example, a 32-bit (4-byte) integer requires 1--5
9bytes when varint-encoded.
10
11`Protocol Buffers <https://developers.google.com/protocol-buffers/docs/encoding#varints>`_
12use a variable-length encoding for integers.
13
14Compatibility
15=============
16* C
17* C++11 (with :doc:`../pw_polyfill/docs`)
18
19API
20===
21
22.. cpp:function:: size_t EncodedSize(uint64_t integer)
23
24Returns the size of an integer when encoded as a varint. Works on both signed
25and unsigned integers.
26
27.. cpp:function:: size_t ZigZagEncodedSize(int64_t integer)
28
29Returns the size of a signed integer when ZigZag encoded as a varint.
30
31.. cpp:function:: uint64_t MaxValueInBytes(size_t bytes)
32
33Returns the maximum integer value that can be encoded as a varint into the
34specified number of bytes.
35
36Dependencies
37============
38* ``pw_span``
39