50 constexpr Icon() =
default;
56 [[nodiscard]]
static constexpr Icon from(
const std::string_view
value) {
58 throw std::length_error{
"commons: Icon value exceeds capacity"};
62 throw std::invalid_argument{
"commons: invalid Icon value (expected 'set:name')"};
69 [[nodiscard]]
static constexpr Icon from(
const std::string_view
set,
70 const std::string_view
name) {
73 throw std::length_error{
"commons: Icon 'set:name' exceeds capacity"};
78 for (
const char c :
set) {
82 for (
const char c :
name) {
85 const auto parsed =
parse(std::string_view{
static_cast<const char*
>(tmp), total});
87 throw std::invalid_argument{
88 "commons: invalid Icon set/name (expected non-empty parts)"};
96 [[nodiscard]]
static constexpr std::optional<Icon>
parse(
const std::string_view
value) {
100 usize colon = std::string_view::npos;
103 if (
value[i] ==
':') {
108 if (colons != 1 || colon == 0 || colon + 1 ==
value.size()) {
113 icon.buf_[i] =
value[i];
115 icon.len_ =
static_cast<u8>(
value.size());
122 [[nodiscard]]
constexpr std::string_view
value() const noexcept {
123 return std::string_view{
static_cast<const char*
>(buf_), len_};
127 [[nodiscard]]
constexpr std::string_view
set() const noexcept {
128 const auto v =
value();
129 return v.substr(0, colon_index(v));
133 [[nodiscard]]
constexpr std::string_view
name() const noexcept {
134 const auto v =
value();
135 const auto pos = colon_index(v);
136 return pos == std::string_view::npos ? std::string_view{} : v.substr(pos + 1);
139 [[nodiscard]]
constexpr bool empty() const noexcept {
145 return std::string{
value()};
148 bool operator==(
const Icon&)
const =
default;
154 [[nodiscard]]
static constexpr usize colon_index(std::string_view v)
noexcept {
155 for (
usize i = 0; i < v.size(); ++i) {
160 return std::string_view::npos;
170template <FixedString Set>
172 static constexpr std::string_view set = Set.view();
185inline std::ostream& operator<<(std::ostream& os,
const Icon& i) {
186 return os << i.value();
203struct std::formatter<comms::Icon> {
204 constexpr auto parse(
const std::format_parse_context& ctx) {
205 const auto* it = ctx.begin();
206 if (it != ctx.end() && *it !=
'}') {
207 throw std::format_error(
"commons: Icon takes no format spec");
212 auto format(
const comms::Icon& i, std::format_context& ctx)
const {
213 return std::format_to(ctx.out(),
"{}", i.
value());
std::string to_string(const Color &c)
Color as its canonical hex string (#RRGGBB, or #RRGGBBAA when not opaque).
Definition color.hpp:1388
NTTP-friendly fixed-size string usable as a non-type template parameter.
An Iconify icon identifier — a set:name pair such as mdi:abacus.
Definition icon.hpp:43
constexpr std::string_view value() const noexcept
The whole set:name string.
Definition icon.hpp:122
static constexpr Icon from(const std::string_view set, const std::string_view name)
Build by joining set and name with a :.
Definition icon.hpp:69
constexpr std::string_view name() const noexcept
The icon name (after the :), e.g. abacus.
Definition icon.hpp:133
static constexpr Icon from(const std::string_view value)
Build from a whole set:name value, e.g.
Definition icon.hpp:56
constexpr std::string_view set() const noexcept
The set prefix (before the :), e.g. mdi.
Definition icon.hpp:127
std::string to_string() const
The canonical set:name string as a std::string.
Definition icon.hpp:144
static constexpr usize capacity
Inline buffer capacity.
Definition icon.hpp:46
static constexpr std::optional< Icon > parse(const std::string_view value)
Non-throwing validation: returns the Icon for a well-formed value (exactly one :, non-empty set and n...
Definition icon.hpp:96
Base for a predefined Iconify set: carries the set name as a string_view.
Definition icon.hpp:171
Fixed-width numeric aliases shared across the C++ libraries.
std::uint8_t u8
Unsigned 8-bit integer.
Definition types.hpp:22
std::size_t usize
Unsigned size type (std::size_t).
Definition types.hpp:43