15 template <std::
floating_po
int T>
16 [[nodiscard]] std::string
to_string(T floating_point)
18 const auto len =
static_cast<size_t>(::snprintf(
nullptr, 0,
"%g", floating_point));
19 std::string str(len,
'\0');
20 ::sprintf_s(&str[0], len + 1,
"%g", floating_point);
26 requires (!std::same_as<T, char>&& std::integral<T>)
27 [[nodiscard]] std::string
to_string(T integral) {
return std::to_string(integral); }
30 template <std::same_as<
char> T>
31 [[nodiscard]] std::string
to_string(T c) {
return std::string{c}; }
35 requires std::constructible_from<std::string_view, T>
36 [[nodiscard]] std::string
to_string(T&& val) {
return std::string{std::string_view{std::forward<T>(val)}}; }
39 [[nodiscard]]
inline std::string
to_string(nullptr_t) {
return "null"; }
50 template <
bool TO_STRING>
56 std::string str(T&& val)
const;
58 template <
typename... ARGS>
59 bool operator()(ARGS&&... args)
const
61 ((result += this->str(args)), ...);
67 concept from_charsable =
requires (
const char* a, T&
to) { { std::from_chars(a, a,
to) } -> std::same_as<std::from_chars_result>; };
70 bool eat_from_string(std::string_view& from, T&& val)
72 if constexpr (std::is_reference_v<T>)
74 if constexpr (std::same_as<T, char>)
76 if (from.empty())
return false;
78 from.remove_prefix(1);
81 else if constexpr (from_charsable<T>)
84 if (ec == std::errc{})
86 from = ghassanpl::string_ops::make_sv(ptr, from.end());
98 if constexpr (std::same_as<T, char> || std::convertible_to<T, std::string_view>)
112 std::string_view from;
114 template <
typename... ARGS>
115 bool operator()(ARGS&&... args)
117 return ((ghassanpl::eat_from_string(from, std::forward<ARGS>(args))) && ...);
122 requires requires (STRINGIFIER<true> str, T val) { stringify(str, val); }
123 [[nodiscard]] std::string
to_string(T&& val)
126 STRINGIFIER<true> str{result};
127 stringify(str, std::forward<T>(val));
132 requires requires (STRINGIFIER<false> str, T val) { stringify(str, val); }
133 bool from_string(std::string_view val, T& target)
135 STRINGIFIER<false> str{val};
136 if (!stringify(str, target))
142 requires requires (STRINGIFIER<false> str, T val) { stringify(str, val); }
143 [[nodiscard]] T from_string(std::string_view val)
146 STRINGIFIER<false> str{val};
147 if (!stringify(str, result))
152 template<
bool TO_STRING>
154 inline std::string string_stringifier<TO_STRING>::str(T&& val)
const
163struct std::formatter<T> : std::formatter<std::string>
165 template<
typename U,
class FormatContext>
166 auto format(U&& t, FormatContext& fc)
const
169 ghassanpl::string_stringifier<true> str{ result };
170 stringify(str, std::forward<U>(t));
171 return std::formatter<std::string>::format(std::move(result), fc);
176requires requires (T val) { { (std::string_view)val } -> std::same_as<std::string_view>; } && (!std::convertible_to<T, std::string_view>)
177struct std::formatter<T> : std::formatter<std::string_view>
179 template<
typename U,
class FormatContext>
180 auto format(U&& t, FormatContext& fc)
const
182 return std::formatter<std::string_view>::format((std::string_view)std::forward<U>(t), fc);
constexpr CONTAINER to(RANGE &&range, TYPES &&... args)
to<container>();
char consume(std::string_view &str)
Consumes and returns the first character in the str, or \0 if no more characters.
constexpr auto from_chars(std::string_view str, T &value, const int base=10) noexcept
A version of std::from_chars that takes a std::string_view as the first argument.
std::string to_string(T floating_point)
float overload
Primary namespace for everything in this library.
NOTE: The below is a MAJOR WORK IN PROGRESS.