| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_FORMAT_MESSAGE_HPP |
| 4 |
#define MPT_FORMAT_MESSAGE_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/macros.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/base/span.hpp" |
| 11 |
#include "mpt/string/types.hpp" |
| 12 |
#include "mpt/string/utility.hpp" |
| 13 |
|
| 14 |
#include <array> |
| 15 |
#include <stdexcept> |
| 16 |
#include <utility> |
| 17 |
|
| 18 |
#include <cstddef> |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace mpt { |
| 23 |
inline namespace MPT_INLINE_NS { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class format_message_syntax_error |
| 28 |
: public std::domain_error { |
| 29 |
public: |
| 30 |
format_message_syntax_error() |
| 31 |
: std::domain_error("format string syntax error") { |
| 32 |
return; |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
template <typename Tformatter, typename Tstring> |
| 37 |
class message_formatter { |
| 38 |
|
| 39 |
private: |
| 40 |
using Tstringview = typename mpt::make_string_view_type<Tstring>::type; |
| 41 |
|
| 42 |
private: |
| 43 |
Tstring format; |
| 44 |
|
| 45 |
private: |
| 46 |
MPT_NOINLINE Tstring do_format(const mpt::span<const Tstring> vals) const { |
| 47 |
using traits = typename mpt::string_traits<Tstring>; |
| 48 |
using char_type = typename traits::char_type; |
| 49 |
using size_type = typename traits::size_type; |
| 50 |
Tstring result; |
| 51 |
const size_type len = traits::length(format); |
| 52 |
traits::reserve(result, len); |
| 53 |
std::size_t max_arg = 0; |
| 54 |
//std::size_t args = 0; |
| 55 |
bool success = true; |
| 56 |
enum class state : int { |
| 57 |
error = -1, |
| 58 |
text = 0, |
| 59 |
open_seen = 1, |
| 60 |
number_seen = 2, |
| 61 |
close_seen = 3, |
| 62 |
}; |
| 63 |
state state = state::text; |
| 64 |
bool numbered_args = false; |
| 65 |
bool unnumbered_args = false; |
| 66 |
std::size_t last_arg = 0; |
| 67 |
std::size_t this_arg = 0; |
| 68 |
std::size_t current_arg = 0; |
| 69 |
for (size_type pos = 0; pos != len; ++pos) { |
| 70 |
char_type c = format[pos]; |
| 71 |
switch (state) { |
| 72 |
case state::text: |
| 73 |
if (c == char_type('{')) { |
| 74 |
state = state::open_seen; |
| 75 |
} else if (c == char_type('}')) { |
| 76 |
state = state::close_seen; |
| 77 |
} else { |
| 78 |
state = state::text; |
| 79 |
traits::append(result, c); // output c here |
| 80 |
} |
| 81 |
break; |
| 82 |
case state::open_seen: |
| 83 |
if (c == char_type('{')) { |
| 84 |
state = state::text; |
| 85 |
traits::append(result, char_type('{')); // output { here |
| 86 |
} else if (c == char_type('}')) { |
| 87 |
state = state::text; |
| 88 |
unnumbered_args = true; |
| 89 |
last_arg++; |
| 90 |
this_arg = last_arg; |
| 91 |
{ // output this_arg here |
| 92 |
const std::size_t n = this_arg - 1; |
| 93 |
if (n < std::size(vals)) { |
| 94 |
traits::append(result, vals[n]); |
| 95 |
} |
| 96 |
} |
| 97 |
if (this_arg > max_arg) { |
| 98 |
max_arg = this_arg; |
| 99 |
} |
| 100 |
//args += 1; |
| 101 |
} else if (char_type('0') <= c && c <= char_type('9')) { |
| 102 |
state = state::number_seen; |
| 103 |
numbered_args = true; |
| 104 |
current_arg = c - char_type('0'); |
| 105 |
} else { |
| 106 |
state = state::error; |
| 107 |
} |
| 108 |
break; |
| 109 |
case state::number_seen: |
| 110 |
if (c == char_type('{')) { |
| 111 |
state = state::error; |
| 112 |
} else if (c == char_type('}')) { |
| 113 |
state = state::text; |
| 114 |
this_arg = current_arg + 1; |
| 115 |
{ // output this_arg here |
| 116 |
const std::size_t n = this_arg - 1; |
| 117 |
if (n < std::size(vals)) { |
| 118 |
traits::append(result, vals[n]); |
| 119 |
} |
| 120 |
} |
| 121 |
if (this_arg > max_arg) { |
| 122 |
max_arg = this_arg; |
| 123 |
} |
| 124 |
//args += 1; |
| 125 |
} else if (char_type('0') <= c && c <= char_type('9')) { |
| 126 |
state = state::number_seen; |
| 127 |
numbered_args = true; |
| 128 |
current_arg = (current_arg * 10) + (c - char_type('0')); |
| 129 |
} else { |
| 130 |
state = state::error; |
| 131 |
} |
| 132 |
break; |
| 133 |
case state::close_seen: |
| 134 |
if (c == char_type('{')) { |
| 135 |
state = state::error; |
| 136 |
} else if (c == char_type('}')) { |
| 137 |
state = state::text; |
| 138 |
traits::append(result, char_type('}')); // output } here |
| 139 |
} else { |
| 140 |
state = state::error; |
| 141 |
} |
| 142 |
break; |
| 143 |
case state::error: |
| 144 |
state = state::error; |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
if (state == state::error) { |
| 149 |
success = false; |
| 150 |
} |
| 151 |
if (state != state::text) { |
| 152 |
success = false; |
| 153 |
} |
| 154 |
if (numbered_args && unnumbered_args) { |
| 155 |
success = false; |
| 156 |
} |
| 157 |
if (!success) { |
| 158 |
throw format_message_syntax_error(); |
| 159 |
} |
| 160 |
return result; |
| 161 |
} |
| 162 |
|
| 163 |
public: |
| 164 |
MPT_FORCEINLINE message_formatter(Tstring format_) |
| 165 |
: format(std::move(format_)) { |
| 166 |
} |
| 167 |
|
| 168 |
public: |
| 169 |
template <typename... Ts> |
| 170 |
MPT_NOINLINE Tstring operator()(Ts &&... xs) const { |
| 171 |
const std::array<Tstring, sizeof...(xs)> vals{{Tformatter::template format<Tstring>(std::forward<Ts>(xs))...}}; |
| 172 |
return do_format(mpt::as_span(vals)); |
| 173 |
} |
| 174 |
|
| 175 |
}; // class message_formatter |
| 176 |
|
| 177 |
|
| 178 |
template <typename Tformatter, std::ptrdiff_t N, typename Tchar, typename Tstring> |
| 179 |
class message_formatter_counted { |
| 180 |
|
| 181 |
private: |
| 182 |
using Tstringview = typename mpt::make_string_view_type<Tstring>::type; |
| 183 |
|
| 184 |
private: |
| 185 |
message_formatter<Tformatter, Tstring> formatter; |
| 186 |
|
| 187 |
public: |
| 188 |
template <std::size_t literal_length> |
| 189 |
inline message_formatter_counted(const Tchar (&format)[literal_length]) |
| 190 |
: formatter(Tstring(format)) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
public: |
| 195 |
template <typename... Ts> |
| 196 |
inline Tstring operator()(Ts &&... xs) const { |
| 197 |
static_assert(static_cast<std::ptrdiff_t>(sizeof...(xs)) == N); |
| 198 |
return formatter(std::forward<Ts>(xs)...); |
| 199 |
} |
| 200 |
|
| 201 |
}; // struct message_formatter_counted |
| 202 |
|
| 203 |
|
| 204 |
template <typename Tchar> |
| 205 |
MPT_CONSTEXPRINLINE std::ptrdiff_t parse_format_string_argument_count_impl(const Tchar * const format, const std::size_t len) { |
| 206 |
std::size_t max_arg = 0; |
| 207 |
std::size_t args = 0; |
| 208 |
bool success = true; |
| 209 |
enum class state : int { |
| 210 |
error = -1, |
| 211 |
text = 0, |
| 212 |
open_seen = 1, |
| 213 |
number_seen = 2, |
| 214 |
close_seen = 3, |
| 215 |
}; |
| 216 |
state state = state::text; |
| 217 |
bool numbered_args = false; |
| 218 |
bool unnumbered_args = false; |
| 219 |
std::size_t last_arg = 0; |
| 220 |
std::size_t this_arg = 0; |
| 221 |
std::size_t current_arg = 0; |
| 222 |
for (std::size_t pos = 0; pos != len; ++pos) { |
| 223 |
Tchar c = format[pos]; |
| 224 |
switch (state) { |
| 225 |
case state::text: |
| 226 |
if (c == Tchar('{')) { |
| 227 |
state = state::open_seen; |
| 228 |
} else if (c == Tchar('}')) { |
| 229 |
state = state::close_seen; |
| 230 |
} else { |
| 231 |
state = state::text; |
| 232 |
// output c here |
| 233 |
} |
| 234 |
break; |
| 235 |
case state::open_seen: |
| 236 |
if (c == Tchar('{')) { |
| 237 |
state = state::text; |
| 238 |
// output { here |
| 239 |
} else if (c == Tchar('}')) { |
| 240 |
state = state::text; |
| 241 |
unnumbered_args = true; |
| 242 |
last_arg++; |
| 243 |
this_arg = last_arg; |
| 244 |
// output this_arg here |
| 245 |
if (this_arg > max_arg) |
| 246 |
{ |
| 247 |
max_arg = this_arg; |
| 248 |
} |
| 249 |
args += 1; |
| 250 |
} else if (Tchar('0') <= c && c <= Tchar('9')) { |
| 251 |
state = state::number_seen; |
| 252 |
numbered_args = true; |
| 253 |
current_arg = c - Tchar('0'); |
| 254 |
} else { |
| 255 |
state = state::error; |
| 256 |
} |
| 257 |
break; |
| 258 |
case state::number_seen: |
| 259 |
if (c == Tchar('{')) { |
| 260 |
state = state::error; |
| 261 |
} else if (c == Tchar('}')) { |
| 262 |
state = state::text; |
| 263 |
this_arg = current_arg + 1; |
| 264 |
// output this_arg here |
| 265 |
if (this_arg > max_arg) { |
| 266 |
max_arg = this_arg; |
| 267 |
} |
| 268 |
args += 1; |
| 269 |
} else if (Tchar('0') <= c && c <= Tchar('9')) { |
| 270 |
state = state::number_seen; |
| 271 |
numbered_args = true; |
| 272 |
current_arg = (current_arg * 10) + (c - Tchar('0')); |
| 273 |
} else { |
| 274 |
state = state::error; |
| 275 |
} |
| 276 |
break; |
| 277 |
case state::close_seen: |
| 278 |
if (c == Tchar('{')) { |
| 279 |
state = state::error; |
| 280 |
} else if (c == Tchar('}')) { |
| 281 |
state = state::text; |
| 282 |
// output } here |
| 283 |
} else { |
| 284 |
state = state::error; |
| 285 |
} |
| 286 |
break; |
| 287 |
case state::error: |
| 288 |
state = state::error; |
| 289 |
break; |
| 290 |
} |
| 291 |
} |
| 292 |
if (state == state::error) { |
| 293 |
success = false; |
| 294 |
} |
| 295 |
if (state != state::text) { |
| 296 |
success = false; |
| 297 |
} |
| 298 |
if (numbered_args && unnumbered_args) { |
| 299 |
success = false; |
| 300 |
} |
| 301 |
if (!success) { |
| 302 |
throw format_message_syntax_error(); |
| 303 |
} |
| 304 |
if (max_arg != args) { |
| 305 |
throw format_message_syntax_error(); |
| 306 |
} |
| 307 |
return max_arg; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
template <typename Tchar, std::size_t literal_length> |
| 312 |
MPT_CONSTEXPRINLINE std::ptrdiff_t parse_format_string_argument_count(const Tchar (&format)[literal_length]) { |
| 313 |
return parse_format_string_argument_count_impl(format, literal_length - 1); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
template <typename Tformatter, std::size_t args, typename Tchar, std::size_t N> |
| 318 |
inline auto format_message(const Tchar (&format)[N]) { |
| 319 |
using Tstring = typename mpt::make_string_type<const Tchar *>::type; |
| 320 |
return message_formatter_counted<Tformatter, args, Tchar, Tstring>(format); |
| 321 |
} |
| 322 |
|
| 323 |
template <typename Tformatter, std::size_t args, typename Tstring, typename Tchar, std::size_t N> |
| 324 |
inline auto format_message_typed(const Tchar (&format)[N]) { |
| 325 |
return message_formatter_counted<Tformatter, args, Tchar, Tstring>(format); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
} // namespace MPT_INLINE_NS |
| 331 |
} // namespace mpt |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
#endif // MPT_FORMAT_MESSAGE_HPP |