pw_bytes#

pw_bytes is a collection of utilities for manipulating binary data.

Compatibility#

Pigweed AI summary: The paragraph under the "Compatibility" section mentions C++17.

C++17

Dependencies#

Pigweed AI summary: This section lists the dependencies of the project, which include pw_preprocessor, pw_status, and pw_span.

  • pw_preprocessor

  • pw_status

  • pw_span

Features#

pw_bytes/array.h#

Pigweed AI summary: This section describes the functions available in pw_bytes/array.h for working with byte arrays, with a focus on building fixed-size byte arrays during compilation.

Functions for working with byte arrays, primarily for building fixed-size byte arrays at compile time.

pw_bytes/byte_builder.h#

class ByteBuilder#

ByteBuilder is a class that facilitates building or reading arrays of bytes in a fixed-size buffer. ByteBuilder handles reading and writing integers with varying endianness.

template<size_t kMaxSize>
class ByteBuffer#

ByteBuilder with an internally allocated buffer.

Size report: using ByteBuffer#

Pigweed AI summary: This section provides a size report using ByteBuffer, comparing the use of ByteBuilder versus not using it. The report includes a table with columns for Label, Segment, and Delta, and shows the differences in size for various functions and methods. The report highlights the benefits of using ByteBuilder, with several functions showing significant reductions in size.

Label

Segment

Delta

Using ByteBuilder vs not using it

FLASH

+4

_ctype_

+4

quorem

+4

pw::bytes::ReadBytes()

+20

pw::bytes::PutBytes()

+2

pw_boot_PreStaticMemoryInit

+2

main

+4

p05.0

NEW

+52

memmove

NEW

+36

pw::ByteBuilder::ResizeForAppend()

NEW

+32

pw::ByteBuilder::append()

NEW

+24

_GLOBAL__sub_I__ZN2pw5bytes2bbE

NEW

+4

pw::bytes::internal::ReverseBytes<>()

+188

pw_bytes/bit.h#

Pigweed AI summary: This paragraph describes the implementation of features provided by C++20's <bit> header in the pw_bytes/bit.h section. The supported feature is pw::endian, which is an implementation of the std::endian enum. If std::endian is available, pw::endian is an alias of it.

Implementation of features provided by C++20’s <bit> header. Supported features:

  • pw::endian – Implementation of the std::endian enum. If std::endian is available, pw::endian is an alias of it.

pw_bytes/endian.h#

Pigweed AI summary: This section titled "pw_bytes/endian.h" contains functions that can be used to convert the endianness of integral values.

Functions for converting the endianness of integral values.

pw_bytes/units.h#

Pigweed AI summary: The pw_bytes/units.h library provides constants, functions, and user-defined literals for specifying a number of bytes in powers of two, as defined by IEC 60027-2 A.2 and ISO/IEC 80000:13-2008. The library supports suffixes for bytes, kibibytes, mebibytes, gibibytes, tebibytes, pebibytes, and exbibytes. To use these, a using

Constants, functions and user-defined literals for specifying a number of bytes in powers of two, as defined by IEC 60027-2 A.2 and ISO/IEC 80000:13-2008.

The supported suffixes include:

  • _B for bytes (10240)

  • _KiB for kibibytes (10241)

  • _MiB for mebibytes (10242)

  • _GiB for gibibytes (10243)

  • _TiB for tebibytes (10244)

  • _PiB for pebibytes (10245)

  • _EiB for exbibytes (10246)

In order to use these you must use a using namespace directive, for example:

#include "pw_bytes/units.h"

using namespace pw::bytes::unit_literals;

constexpr size_t kRandomBufferSizeBytes = 1_MiB + 42_KiB;

In some cases, the use of user-defined literals is not permitted because of the required using namespace directive. One example of this is in header files, where it is undesirable to pollute the namespace. For this situation, there are also similar functions:

#include "pw_bytes/units.h"

constexpr size_t kBufferSizeBytes = pw::bytes::MiB(1) + pw::bytes::KiB(42);

Zephyr#

Pigweed AI summary: To enable pw_bytes for Zephyr, CONFIG_PIGWEED_BYTES=y must be added to the project's configuration.

To enable pw_bytes for Zephyr add CONFIG_PIGWEED_BYTES=y to the project’s configuration.