microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
escaped.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Jarosław Pelczar <jarek@jpelczar.com>
2//
3// SPDX-License-Identifier: BSD-2-Clause
4
5#pragma once
6
9#include "../microfmt.hpp"
10#include <cstddef>
11#include <cstdint>
12#include <string_view>
13#include <type_traits>
14
15namespace microfmt {
16
17// ============================================================================
18// Escaped View Configuration & Adapter
19// ============================================================================
20
21struct MICROFMT_API_CLASS RELOCO_POINTER escaped_view {
22 span<const char> data{};
23 bool quote{true}; // Wrap string in double quotes "..."
24 bool escape_quotes{true}; // Escape inner " as \"
25};
26
27// ============================================================================
28// Factory Functions
29// ============================================================================
30
31// From microfmt::string_view
32[[nodiscard]] constexpr escaped_view escaped(microfmt::string_view sv, bool quote = true,
33 bool escape_quotes = true) noexcept {
34 return escaped_view{span<const char>(sv.data(), sv.size()), quote, escape_quotes};
35}
36
37// From raw const char* buffer / length
38// low-level formatting: raw character pointers and integer width conversions bounded by the buffer sizes checked here.
39RELOCO_BEGIN_UNSAFE_CODE
40[[nodiscard]] constexpr escaped_view escaped(const char *str, size_t len, bool quote = true,
41 bool escape_quotes = true) noexcept {
42 return escaped_view{span<const char>(str, len), quote, escape_quotes};
43}
45
46// From span of raw bytes (const uint8_t)
47// low-level formatting: raw character pointers and integer width conversions bounded by the buffer sizes checked here.
48RELOCO_BEGIN_UNSAFE_CODE
49[[nodiscard]] inline escaped_view escaped(span<const uint8_t> bytes, bool quote = true,
50 bool escape_quotes = true) noexcept {
51 return escaped_view{span<const char>(reinterpret_cast<const char *>(bytes.data()), bytes.size()), quote,
52 escape_quotes};
53}
55
56// From C-style string literals / char arrays
57template <size_t N>
58[[nodiscard]] constexpr escaped_view escaped(const char (&arr)[N], bool quote = true,
59 bool escape_quotes = true) noexcept {
60 // If null-terminated string literal, exclude trailing \0 from the slice
61 // length
62 const size_t len = (N > 0 && arr[N - 1] == '\0') ? N - 1 : N;
63 return escaped_view{span<const char>(arr, len), quote, escape_quotes};
64}
65
66// From C-style byte arrays (uint8_t[N])
67// low-level formatting: raw character pointers and integer width conversions bounded by the buffer sizes checked here.
68RELOCO_BEGIN_UNSAFE_CODE
69template <size_t N>
70[[nodiscard]] constexpr escaped_view escaped(const uint8_t (&arr)[N], bool quote = true,
71 bool escape_quotes = true) noexcept {
72 return escaped_view{span<const char>(reinterpret_cast<const char *>(arr), N), quote, escape_quotes};
73}
75
76// ============================================================================
77// Formatter Specialization for escaped_view
78// ============================================================================
79
80template <> struct formatter<escaped_view> {
81 constexpr void parse(format_parse_context &ctx) noexcept { (void)ctx; }
82
83 MICROFMT_API void format(const escaped_view &ev, const sink &out) const noexcept;
84};
85
86#if MICROFMT_SHARED_PROVIDE_DEFINITIONS
87#include "escaped.ipp"
88#endif
89
90} // namespace microfmt
Definition microfmt.hpp:881
#define MICROFMT_API_CLASS
Definition compat.hpp:181
#define MICROFMT_API
Definition compat.hpp:135
Definition assert.hpp:19
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
constexpr escaped_view escaped(microfmt::string_view sv, bool quote=true, bool escape_quotes=true) noexcept
Definition escaped.hpp:32
constexpr bytes_view bytes(uint64_t count) noexcept
Definition format_helpers.hpp:82
RELOCO_END_UNSAFE_CODE
Definition remote_object.hpp:132
Definition escaped.hpp:21
MICROFMT_API void format(const escaped_view &ev, const sink &out) const noexcept
constexpr void parse(format_parse_context &ctx) noexcept
Definition escaped.hpp:81
Definition microfmt.hpp:985