blob: 58c90f9596d0fbde97d4888e79ad8eefaa94b4f9 [file] [log] [blame]
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -07001.. _module-pw_varint:
Wyatt Hepler3c4e5de2020-03-03 14:37:52 -08002
3---------
4pw_varint
5---------
Armando Montanez0054a9b2020-03-13 13:06:24 -07006The ``pw_varint`` module provides functions for encoding and decoding variable
Wyatt Hepler3c4e5de2020-03-03 14:37:52 -08007length 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
Alexei Frolov388d4b92020-05-20 13:30:07 -070019API
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
Alexei Frolov6fe8ccf2021-03-30 13:19:40 -070031.. 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
Wyatt Hepler3c4e5de2020-03-03 14:37:52 -080036Dependencies
37============
Armando Montanez0054a9b2020-03-13 13:06:24 -070038* ``pw_span``