11 template <std::same_as<
char> T>
12 std::string to_string(T c) {
return std::string{c}; }
14 requires (!std::same_as<T, char> && std::integral<T>)
15 std::string to_string(T
val) {
return std::to_string(
val); }
16 template <std::
floating_po
int T>
17 std::string to_string(T
val)
19 const auto len =
static_cast<size_t>(
::snprintf(
nullptr, 0,
"%g",
val));
20 std::string str(len,
'\0');
25 requires std::constructible_from<std::string_view, T>
26 std::string to_string(T&&
val) {
return std::string{std::string_view{std::forward<T>(
val)}}; }
27 inline std::string to_string(
nullptr_t) {
return "null"; }
36 template <
bool TO_STRING>
41 template <
typename...
ARGS>
44 ((result += ghassanpl::to_string(
args)), ...);
50 concept from_charsable =
requires (
const char* a, T&
to) { { std::from_chars(a, a,
to) } -> std::same_as<std::from_chars_result>; };
53 bool eat_from_string(std::string_view& from, T&&
val)
55 if constexpr (std::is_reference_v<T>)
57 if constexpr (std::same_as<T, char>)
59 if (from.empty())
return false;
61 from.remove_prefix(1);
67 if (
ec == std::errc{})
69 from = ghassanpl::string_ops::make_sv(
ptr, from.end());
81 if constexpr (std::same_as<T, char> || std::convertible_to<T, std::string_view>)
93 struct string_stringifier<
false>
95 std::string_view from;
97 template <
typename...
ARGS>
100 return ((ghassanpl::eat_from_string(from, std::forward<ARGS>(
args))) && ...);
104 template <
typename T,
template<
bool>
typename STRINGIFIER = string_stringifier>
106 std::string to_string(T&&
val)
110 stringify(str, std::forward<T>(
val));
114 template <
typename T,
template<
bool>
typename STRINGIFIER = string_stringifier>
116 bool from_string(std::string_view
val, T&
target)
119 if (!stringify(str,
target))
124 template <
typename T,
template<
bool>
typename STRINGIFIER = string_stringifier>
126 T from_string(std::string_view
val)
130 if (!stringify(str, result))
138struct std::formatter<T> : std::formatter<std::string>
140 template<
typename U,
class FormatContext>
141 auto format(U&& t, FormatContext& fc)
const
145 stringify(str, std::forward<U>(t));
146 return std::formatter<std::string>::format(std::move(result), fc);
151requires requires (T val) { { (std::string_view)val } -> std::same_as<std::string_view>; } && (!std::convertible_to<T, std::string_view>)
152struct std::formatter<T> : std::formatter<std::string_view>
154 template<
typename U,
class FormatContext>
155 auto format(U&& t, FormatContext& fc)
const
157 return std::formatter<std::string_view>::format((std::string_view)std::forward<U>(t), fc);
constexpr auto bit_count
Equal to the number of bits in the type.
constexpr CONTAINER to(RANGE &&range, TYPES &&... args)
to<container>();
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.
char consume(std::string_view &str)
Consumes and returns the first character in the str, or \0 if no more characters.
Primary namespace for everything in this library.
NOTE: The below is a MAJOR WORK IN PROGRESS.