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