microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
semver.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 <cstdint>
11#include <reloco/type_id.hpp>
12#include <string_view>
13
14namespace microfmt {
15
16// ============================================================================
17// Semantic Version Descriptor
18// ============================================================================
19
20struct MICROFMT_API_CLASS RELOCO_POINTER semver {
21 uint16_t major{0};
22 uint16_t minor{0};
23 uint16_t patch{0};
24 microfmt::string_view prerelease{}; // e.g. "rc.1", "alpha"
25 microfmt::string_view build{}; // e.g. "20260913", "armv7"
26 bool show_v_prefix{false}; // e.g. "v1.2.3"
27};
28
29// ============================================================================
30// Factory Helpers
31// ============================================================================
32
33// Explicit components
34[[nodiscard]] constexpr semver version(uint16_t major, uint16_t minor, uint16_t patch,
35 microfmt::string_view prerelease = "", microfmt::string_view build = "",
36 bool show_v = false) noexcept {
37 return semver{major, minor, patch, prerelease, build, show_v};
38}
39
40// Packed 32-bit integer versions (e.g. 0x010203 -> 1.2.3, 8-bit or
41// 10/10/12-bit) Standard 8-8-16 packing: (Major << 24) | (Minor << 16) | Patch
42[[nodiscard]] constexpr semver from_packed32(uint32_t packed, bool show_v = false) noexcept {
43 return semver{static_cast<uint16_t>((packed >> 24) & 0xFF),
44 static_cast<uint16_t>((packed >> 16) & 0xFF),
45 static_cast<uint16_t>(packed & 0xFFFF),
46 "",
47 "",
48 show_v};
49}
50
51// Compact 8-8-8 packing: (Major << 16) | (Minor << 8) | Patch
52[[nodiscard]] constexpr semver from_packed24(uint32_t packed, bool show_v = false) noexcept {
53 return semver{static_cast<uint16_t>((packed >> 16) & 0xFF),
54 static_cast<uint16_t>((packed >> 8) & 0xFF),
55 static_cast<uint16_t>(packed & 0xFF),
56 "",
57 "",
58 show_v};
59}
60
61// ============================================================================
62// Formatter Specialization for semver
63// ============================================================================
64
65template <> struct formatter<semver> {
66 bool force_v_prefix{false};
67 bool hide_prerelease{false};
68 bool hide_build{false};
69
70 constexpr void parse(format_parse_context &ctx) noexcept {
71 for (char c : ctx.spec()) {
72 if (c == '#' || c == 'v' || c == 'V') {
73 force_v_prefix = true; // {:#} or {:v} forces "v" prefix
74 } else if (c == 'c' || c == 'C') {
75 // Compact / Core: {:c} hides prerelease and build metadata
76 hide_prerelease = true;
77 hide_build = true;
78 }
79 }
80 }
81
82 MICROFMT_API void format(const semver &sv, const sink &out) const noexcept;
83};
84
85#if MICROFMT_SHARED_PROVIDE_DEFINITIONS
86#include "semver.ipp"
87#endif
88
89} // namespace microfmt
90
91// See <reloco/type_id.hpp> for the full RELOCO_TYPE_ID_NAME rationale;
92// defined here, alongside semver's own definition.
Definition microfmt.hpp:881
#define MICROFMT_API_CLASS
Definition compat.hpp:181
#define MICROFMT_API
Definition compat.hpp:135
Definition assert.hpp:19
constexpr semver version(uint16_t major, uint16_t minor, uint16_t patch, microfmt::string_view prerelease="", microfmt::string_view build="", bool show_v=false) noexcept
Definition semver.hpp:34
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
constexpr semver from_packed32(uint32_t packed, bool show_v=false) noexcept
Definition semver.hpp:42
constexpr semver from_packed24(uint32_t packed, bool show_v=false) noexcept
Definition semver.hpp:52
RELOCO_TYPE_ID_NAME(microfmt::semver, "microfmt::semver")
constexpr void parse(format_parse_context &ctx) noexcept
Definition semver.hpp:70
MICROFMT_API void format(const semver &sv, const sink &out) const noexcept
Definition microfmt.hpp:985
Definition semver.hpp:20