Line data Source code
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 : 35 : namespace 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 : */ 44 : class StringBuilder 45 : { 46 : public: 47 71200 : StringBuilder(std::span<char> buffer) noexcept 48 71200 : : m_Buffer{buffer}, m_BufferBegin{buffer.data()} 49 : { 50 71200 : ENSURE(!m_Buffer.empty()); 51 71200 : } 52 : 53 : template<typename T> 54 394479 : typename std::enable_if<std::is_arithmetic_v<T>>::type Append(const T value) noexcept 55 : { 56 394479 : ENSURE(m_Buffer.size() > 0); 57 : #if defined(__cpp_lib_to_chars) 58 394479 : const std::to_chars_result result{std::to_chars(m_Buffer.data(), m_Buffer.data() + m_Buffer.size() - 1, value)}; 59 394479 : ENSURE(result.ec == std::errc()); 60 394479 : m_Buffer = m_Buffer.subspan(result.ptr - m_Buffer.data()); 61 : // to_chars doesn't write terminating null. 62 394479 : 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 394479 : } 73 : 74 69805 : void Append(const char ch) 75 : { 76 69805 : ENSURE(m_Buffer.size() > 1); 77 69805 : m_Buffer.front() = ch; 78 69805 : m_Buffer = m_Buffer.subspan(1); 79 69805 : m_Buffer.front() = 0; 80 69805 : } 81 : 82 5295 : void Append(const std::string_view str) noexcept 83 : { 84 5295 : ENSURE(m_Buffer.size() >= static_cast<size_t>(str.size()) + 1); 85 5295 : std::copy(str.begin(), str.end(), m_Buffer.begin()); 86 5295 : m_Buffer = m_Buffer.subspan(str.size()); 87 5295 : m_Buffer.front() = 0; 88 5295 : } 89 : 90 : /** 91 : * Returns a string_view to a null-terminated string. 92 : */ 93 71200 : std::string_view Str() noexcept 94 : { 95 71200 : return {m_BufferBegin, m_Buffer.data()}; 96 : } 97 : 98 : private: 99 : std::span<char> m_Buffer; 100 : char* m_BufferBegin; 101 : }; 102 : 103 : } // namespace PS 104 : 105 : #endif // INCLUDED_PS_STRINGBUILDER