Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
StringBuilder.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_PS_STRINGBUILDER
19#define INCLUDED_PS_STRINGBUILDER
20
21#include <charconv>
22#include <cstdint>
23#include <span>
24#include <string>
25#include <string_view>
26#include <type_traits>
27#include <version>
28
29#if defined(__cpp_lib_to_chars)
30#include <charconv>
31#else
32#include <fmt/core.h>
33#endif
34
35namespace PS
36{
37
38/**
39 * A helper class to build string in-place without additional cost of
40 * allocations, locales and format parsing. Prefer to use the class with stack
41 * allocated buffers over standard tools in high-load cases.
42 * TODO: in case of overflow it might use a heap allocated storage.
43 */
45{
46public:
47 StringBuilder(std::span<char> buffer) noexcept
48 : m_Buffer{buffer}, m_BufferBegin{buffer.data()}
49 {
50 ENSURE(!m_Buffer.empty());
51 }
52
53 template<typename T>
54 typename std::enable_if<std::is_arithmetic_v<T>>::type Append(const T value) noexcept
55 {
56 ENSURE(m_Buffer.size() > 0);
57#if defined(__cpp_lib_to_chars)
58 const std::to_chars_result result{std::to_chars(m_Buffer.data(), m_Buffer.data() + m_Buffer.size() - 1, value)};
59 ENSURE(result.ec == std::errc());
60 m_Buffer = m_Buffer.subspan(result.ptr - m_Buffer.data());
61 // to_chars doesn't write terminating null.
62 m_Buffer.front() = 0;
63#else
64 // TODO: switch to std::to_chars after minimal libcxx supports it.
65 const fmt::format_to_n_result result{
66 fmt::format_to_n(m_Buffer.data(), m_Buffer.size() - 1, "{}", value)};
67 ENSURE(m_Buffer.data() != result.out);
68 m_Buffer = m_Buffer.subspan(result.size);
69 // format_to_n doesn't write terminating null.
70 m_Buffer.front() = 0;
71#endif
72 }
73
74 void Append(const char ch)
75 {
76 ENSURE(m_Buffer.size() > 1);
77 m_Buffer.front() = ch;
78 m_Buffer = m_Buffer.subspan(1);
79 m_Buffer.front() = 0;
80 }
81
82 void Append(const std::string_view str) noexcept
83 {
84 ENSURE(m_Buffer.size() >= static_cast<size_t>(str.size()) + 1);
85 std::copy(str.begin(), str.end(), m_Buffer.begin());
86 m_Buffer = m_Buffer.subspan(str.size());
87 m_Buffer.front() = 0;
88 }
89
90 /**
91 * Returns a string_view to a null-terminated string.
92 */
93 std::string_view Str() noexcept
94 {
95 return {m_BufferBegin, m_Buffer.data()};
96 }
97
98private:
99 std::span<char> m_Buffer;
101};
102
103} // namespace PS
104
105#endif // INCLUDED_PS_STRINGBUILDER
A helper class to build string in-place without additional cost of allocations, locales and format pa...
Definition: StringBuilder.h:45
void Append(const std::string_view str) noexcept
Definition: StringBuilder.h:82
char * m_BufferBegin
Definition: StringBuilder.h:100
void Append(const char ch)
Definition: StringBuilder.h:74
std::string_view Str() noexcept
Returns a string_view to a null-terminated string.
Definition: StringBuilder.h:93
StringBuilder(std::span< char > buffer) noexcept
Definition: StringBuilder.h:47
std::enable_if< std::is_arithmetic_v< T > >::type Append(const T value) noexcept
Definition: StringBuilder.h:54
std::span< char > m_Buffer
Definition: StringBuilder.h:99
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
Definition: GUIManager.h:45
#define T(string_literal)
Definition: secure_crt.cpp:81