microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
styled.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
17#include "../microfmt.hpp"
18#include <cstddef>
19#include <cstdint>
20#include <string_view>
21#include <type_traits>
22
23namespace microfmt {
24
26enum class text_case : uint8_t { none = 0, upper, lower, title };
27
29enum class text_align : uint8_t { left = 0, center, right };
30
32enum class quote_style : uint8_t {
33 none = 0,
34 double_quotes, // "text"
35 single_quotes, // 'text'
36 brackets, // [text]
37 parens, // (text)
38 angle_brackets // <text>
39};
40
46struct MICROFMT_API_CLASS RELOCO_POINTER styled_str_view {
47 microfmt::string_view text{};
48 size_t width{0};
49 char fill_char{' '};
50 text_align align{text_align::left};
51 text_case casing{text_case::none};
52 quote_style quote{quote_style::none};
53 size_t max_len{0}; // 0 = no truncation
54 bool ellipsis{true};
55};
56
58[[nodiscard]] constexpr styled_str_view pad(microfmt::string_view s, size_t width, text_align a = text_align::left,
59 char fill = ' ') noexcept {
60 return {s, width, fill, a, text_case::none, quote_style::none, 0, false};
61}
62
64[[nodiscard]] constexpr styled_str_view pad_center(microfmt::string_view s, size_t width, char fill = ' ') noexcept {
65 return pad(s, width, text_align::center, fill);
66}
67
69[[nodiscard]] constexpr styled_str_view pad_right(microfmt::string_view s, size_t width, char fill = ' ') noexcept {
70 return pad(s, width, text_align::right, fill);
71}
72
74[[nodiscard]] constexpr styled_str_view to_upper(microfmt::string_view s) noexcept {
75 return {s, 0, ' ', text_align::left, text_case::upper, quote_style::none, 0, false};
76}
77
79[[nodiscard]] constexpr styled_str_view to_lower(microfmt::string_view s) noexcept {
80 return {s, 0, ' ', text_align::left, text_case::lower, quote_style::none, 0, false};
81}
82
85[[nodiscard]] constexpr styled_str_view truncate(microfmt::string_view s, size_t max_chars,
86 bool use_ellipsis = true) noexcept {
87 return {s, 0, ' ', text_align::left, text_case::none, quote_style::none, max_chars, use_ellipsis};
88}
89
91[[nodiscard]] constexpr styled_str_view quoted(microfmt::string_view s,
93 return {s, 0, ' ', text_align::left, text_case::none, q, 0, false};
94}
95
104template <> struct formatter<styled_str_view> {
106 bool align_set{false};
107 bool fill_set{false};
108
110
111 MICROFMT_API void format(const styled_str_view &input, const sink &out) const noexcept;
112};
113
114#if MICROFMT_SHARED_PROVIDE_DEFINITIONS
115#include "styled.ipp"
116#endif
117
118} // namespace microfmt
Definition microfmt.hpp:881
#define MICROFMT_API_CLASS
Definition compat.hpp:181
#define MICROFMT_API
Definition compat.hpp:135
#define MICROFMT_API_CONSTEXPR
Definition compat.hpp:163
Definition assert.hpp:19
text_align
Definition styled.hpp:29
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
text_case
Definition styled.hpp:26
constexpr styled_str_view pad_center(microfmt::string_view s, size_t width, char fill=' ') noexcept
Definition styled.hpp:64
constexpr styled_str_view pad_right(microfmt::string_view s, size_t width, char fill=' ') noexcept
Definition styled.hpp:69
quote_style
Definition styled.hpp:32
constexpr styled_str_view pad(microfmt::string_view s, size_t width, text_align a=text_align::left, char fill=' ') noexcept
Definition styled.hpp:58
constexpr styled_str_view quoted(microfmt::string_view s, quote_style q=quote_style::double_quotes) noexcept
Definition styled.hpp:91
constexpr styled_str_view truncate(microfmt::string_view s, size_t max_chars, bool use_ellipsis=true) noexcept
Definition styled.hpp:85
MICROFMT_API_CONSTEXPR void parse(format_parse_context &ctx) noexcept
MICROFMT_API void format(const styled_str_view &input, const sink &out) const noexcept
Definition microfmt.hpp:985
Definition styled.hpp:46