FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <algorithm>
12#include <sstream>
13#include <format>
14#include <expected>
15
16#include "concept.hpp"
17
26namespace ns_string
27{
28
33template<size_t N>
35{
36 char data[N];
37
42 constexpr static_string(const char (&str)[N])
43 {
44 std::copy_n(str, N, data);
45 }
46
48 constexpr static_string() = default;
49
51 constexpr operator const char*() const { return data; }
52
54 constexpr operator std::string_view() const { return std::string_view(data, N - 1); }
55
60 constexpr size_t size() const { return N - 1; }
61
67 template <auto... Strs>
68 constexpr auto join() const noexcept
69 {
70 // Total string length
71 constexpr std::size_t total_len = (N - 1) + (Strs.size() + ... + 0);
72 // Result of the operation
74 // Copy the member string
75 std::copy_n(data, N, result.data);
76 // Copy all other strings
77 auto append = [i=N-1, &result](auto const& s) mutable
78 {
79 std::copy_n(s.data, s.size(), result.data+i);
80 i += s.size();
81 };
82 (append(Strs), ...);
83 // String terminator
84 result.data[total_len] = '\0';
85 return result;
86 }
87};
88
96template<typename T>
97[[nodiscard]] inline std::string to_string(T&& t) noexcept
98{
100 {
101 return t;
102 } // if
103 else if constexpr ( ns_concept::StringConstructible<T> )
104 {
105 return std::string{t};
106 } // else if
107 else if constexpr ( ns_concept::Numeric<T> )
108 {
109 return std::to_string(t);
110 } // else if
111 else if constexpr ( ns_concept::StreamInsertable<T> )
112 {
113 std::stringstream ss;
114 ss << t;
115 return ss.str();
116 } // else if
117 else if constexpr ( ns_concept::Iterable<T> )
118 {
119 std::stringstream ss;
120 ss << '[';
121 std::for_each(t.cbegin(), t.cend(), [&](auto&& e){ ss << std::format("'{}',", e); });
122 ss << ']';
123 return ss.str();
124 } // else if
125 else
126 {
127 static_assert(false, "Cannot convert type to string");
128 }
129}
130
139template<typename T>
140[[nodiscard]] std::string from_container(T&& t, std::optional<char> sep = std::nullopt) noexcept
141{
142 std::stringstream ret;
143 for( auto it = t.begin(); it != t.end(); ++it )
144 {
145 ret << *it;
146 if ( std::next(it) != t.end() and sep ) { ret << *sep; }
147 } // if
148 return ret.str();
149}
150
160template<std::input_iterator It>
161[[nodiscard]] std::string from_container(It&& begin, It&& end, std::optional<char> sep = std::nullopt) noexcept
162{
163 return from_container(std::ranges::subrange(begin, end), sep);
164}
165
177template<typename... Args>
178[[nodiscard]] inline std::string placeholders_replace(std::string str, Args&&... args)
179{
180 std::vector<std::string> replacements;
181 // Convert all arguments to strings
182 ((replacements.push_back(to_string(std::forward<Args>(args)))), ...);
183
184 // Replace all occurrences of "{}" with the replacements in order
185 std::size_t arg_index = 0;
186 while (arg_index < replacements.size())
187 {
188 size_t pos = str.find("{}");
189 if (pos == std::string::npos) break;
190 str.replace(pos, 2, replacements[arg_index++]);
191 }
192 return str;
193}
194
195} // namespace ns_string
196
197/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
Custom C++ concepts for type constraints and compile-time validation.
Concept for iterable containers (has begin() and end())
Definition concept.hpp:227
Concept for numeric types (integral or floating-point)
Definition concept.hpp:389
Concept for types that support stream insertion (operator<<)
Definition concept.hpp:417
Concept for types that can construct a std::string.
Definition concept.hpp:365
Concept for types implicitly convertible to std::string.
Definition concept.hpp:351
String manipulation and conversion utilities.
std::string to_string(T &&t) noexcept
Converts a type to a string.
Definition string.hpp:97
std::string placeholders_replace(std::string str, Args &&... args)
Replace all occurrences of "{}" placeholders in a string with provided values.
Definition string.hpp:178
std::string from_container(T &&t, std::optional< char > sep=std::nullopt) noexcept
Converts a container into a string if it has string convertible elements.
Definition string.hpp:140
constexpr static_string()=default
Default constructor.
constexpr size_t size() const
Returns the size of the string (excluding null terminator)
Definition string.hpp:60
constexpr static_string(const char(&str)[N])
Constructs a static string from a string literal.
Definition string.hpp:42
constexpr auto join() const noexcept
Concatenates this string with other static strings.
Definition string.hpp:68