16 requires(std::is_integral_v<T> || std::is_floating_point_v<T>)
17[[nodiscard]] std::optional<std::vector<T>>
parse_numbers(std::string_view source)
19 const auto whitespace = [](
char value) {
20 return value ==
' ' || value ==
'\t' || value ==
'\n' || value ==
'\r';
23 std::vector<T> values;
24 const char* current = source.data();
25 const char* end = current + source.size();
26 while (current != end) {
27 while (current != end && whitespace(*current))
32 const bool positive_sign = *current ==
'+';
37 const auto [next, status] = std::from_chars(current, end, value);
38 if ((positive_sign && (current == end || *current ==
'+' || *current ==
'-'))
39 || status != std::errc {} || next == current
40 || (std::is_floating_point_v<T> && !std::isfinite(value))
41 || (next != end && !whitespace(*next))) {
44 values.push_back(value);
54template <
typename T,
size_t N>
55[[nodiscard]] std::optional<std::array<T, N>>
parse_tuple(std::string_view source,
63 if (values->size() != N) {
67 std::array<T, N> result;
68 std::ranges::copy(*values, result.begin());
84 requires((std::is_integral_v<T> || std::is_floating_point_v<T>)
85 && !std::same_as<T, bool>)
87 static constexpr size_t extent = 1;
94 value = (*decoded)[0];
105 if (source ==
"true" || source ==
"1") {
109 if (source ==
"false" || source ==
"0") {
130 requires std::is_enum_v<T>
132 static constexpr size_t extent = 0;
152 requires((std::is_integral_v<T> || std::is_floating_point_v<T>)
153 && !std::same_as<T, bool>)
165 value = std::move(*decoded);
173template <GlmVectorType T>
175 static constexpr size_t extent = glm_component_count<T>();
179 using Scalar =
typename T::value_type;
180 constexpr size_t count = glm_component_count<T>();
184 for (
size_t i = 0; i < count; ++i)
185 value[
static_cast<glm::length_t
>(i)] = (*decoded)[i];
196template <GlmVectorType T>
197struct TextCodec<
std::vector<T>> {
198 static constexpr size_t extent = glm_component_count<T>();
199 static constexpr bool sequence =
true;
203 using Scalar =
typename T::value_type;
204 constexpr size_t count = glm_component_count<T>();
210 if (numbers->size() % count != 0) {
214 std::vector<T> decoded(numbers->size() / count);
215 for (
size_t i = 0; i < decoded.size(); ++i) {
216 for (
size_t j = 0; j < count; ++j)
217 decoded[i][
static_cast<glm::length_t
>(j)] = (*numbers)[i * count + j];
219 value = std::move(decoded);
229template <TextDecodable T>
233 static bool decode(std::string_view source, std::optional<T>& value,
236 T decoded = value.value_or(T {});
239 value = std::move(decoded);
constexpr std::optional< EnumType > string_to_enum(std::string_view str) noexcept
Universal string to enum converter using magic_enum (exact case match).
std::optional< std::vector< T > > parse_numbers(std::string_view source)
Parse whitespace separated finite numbers.
std::optional< std::array< T, N > > parse_tuple(std::string_view source, TextError &error)
Parse exactly N whitespace separated finite numbers.
static bool decode(std::string_view source, T &value, TextError &error)
static constexpr size_t extent
static bool decode(std::string_view source, bool &value, TextError &error)
static bool decode(std::string_view source, std::optional< T > &value, TextError &error)
static constexpr size_t extent
static bool decode(std::string_view source, std::string &value, TextError &)
static constexpr size_t extent
static bool decode(std::string_view source, std::vector< T > &value, TextError &error)
static constexpr size_t extent
static constexpr bool sequence
Conversion of authored text to one value type.