Data & serialization

encode.h

Base64, Base62, URL encoding, Unicode conversion, and compact data tags.

C++23 mc/encode.h
#include <mc/encode.h>

Encoding functions operate on bytes unless the signature explicitly describes text. Decode URL escapes separately from parsing a full URL.

Jump to a declaration · 9

Free functions & types

Functions

cIsBase64

inline bool cIsBase64(unsigned char c);

Tests whether one byte is a Base64 alphabet character. The padding character = and whitespace are not alphabet characters.

cToBase64

inline cstr cToBase64(const char* buf, size_t bytes);
template<class T> inline cstr cToBase64(const T& x);

Encodes bytes with the standard Base64 alphabet and = padding, without line breaks. The object overload encodes its raw in-memory bytes rather than a portable serialized representation.

cFromBase64

inline cstr cFromBase64(const char* buf, size_t bytes);
template<class T> inline T cFromBase64(const cstr& s);

Decodes padded or unpadded Base64, rejecting invalid characters, lengths, padding, and nonzero unused bits. The typed overload requires a trivially copyable type and an exact decoded size; it reconstructs that type’s native object representation.

cTryFromBase64

template<class T> inline std::expected<T, CError> cTryFromBase64(const cstr& s);
inline std::expected<cstr, CError> cTryFromBase64(const char* buf, size_t size);

Returns std::expected containing the decoded result or a CError. The typed overload requires a trivially copyable object of the exact decoded size.

cToBase62

template<CInteger T> inline cstr cToBase62(T x);

Encodes a nonnegative integer with digits 0–9, a–z, and A–Z. Current behavior emits least-significant digits first and represents zero as an empty string, so it is not conventional positional Base62 text.

cDataTag

template<class T> inline cstr cDataTag(const T& x);

Encodes a compact printable tag from data.

cEncodeURL

template<CString T> inline cstr cEncodeURL(T&& s);

Percent-encodes bytes outside this helper’s preserved character set, stopping at the first NUL. It leaves several reserved characters unchanged, including :, =, and ?; it is not a general-purpose encoder for individual query values.

cDecodeURL

template<CString T> inline cstr cDecodeURL(T&& s);

Decodes percent escapes until the first NUL, rejecting incomplete or invalid hex pairs. A plus sign remains a plus sign rather than becoming a space.

cFromUnicode

inline cstr cFromUnicode(uint32_t u);

Encodes one Unicode scalar value as UTF-8. Surrogates and values above U+10FFFF throw CError.