diff options
| author | Maxime Coste <mawww@kakoune.org> | 2024-08-09 18:16:51 +1000 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2024-08-12 20:02:11 +1000 |
| commit | 1a52006c3d215196997a2cd12450795d4ae4a1ca (patch) | |
| tree | 9cae5c08d6fa0a0ec4017b6a0156e64924eeb898 /src/string_utils.hh | |
| parent | 2d9886afe76adde9f33fc0c0454146176857a6f2 (diff) | |
Extract format implementation to its own file
Split it to avoid pulling all string_utils dependencies for just
format.
Diffstat (limited to 'src/string_utils.hh')
| -rw-r--r-- | src/string_utils.hh | 72 |
1 files changed, 2 insertions, 70 deletions
diff --git a/src/string_utils.hh b/src/string_utils.hh index 3034dfd5..24da0111 100644 --- a/src/string_utils.hh +++ b/src/string_utils.hh @@ -4,9 +4,10 @@ #include "string.hh" #include "enum.hh" #include "vector.hh" -#include "ranges.hh" #include "optional.hh" #include "utils.hh" +#include "format.hh" +#include "ranges.hh" namespace Kakoune { @@ -107,75 +108,6 @@ inline auto wrap_at(ColumnCount max_width) int str_to_int(StringView str); // throws on error Optional<int> str_to_int_ifp(StringView str); -template<size_t N> -struct InplaceString -{ - static_assert(N < 256, "InplaceString cannot handle sizes >= 256"); - - constexpr operator StringView() const { return {m_data, ByteCount{m_length}}; } - operator String() const { return {m_data, ByteCount{m_length}}; } - - unsigned char m_length{}; - char m_data[N]; -}; - -struct Hex { size_t val; }; -constexpr Hex hex(size_t val) { return {val}; } - -struct Grouped { size_t val; }; -constexpr Grouped grouped(size_t val) { return {val}; } - -InplaceString<15> to_string(int val); -InplaceString<15> to_string(unsigned val); -InplaceString<23> to_string(long int val); -InplaceString<23> to_string(unsigned long val); -InplaceString<23> to_string(long long int val); -InplaceString<23> to_string(Hex val); -InplaceString<23> to_string(Grouped val); -InplaceString<23> to_string(float val); -InplaceString<7> to_string(Codepoint c); - -template<typename RealType, typename ValueType> -decltype(auto) to_string(const StronglyTypedNumber<RealType, ValueType>& val) -{ - return to_string((ValueType)val); -} - -namespace detail -{ - -template<typename T> requires std::is_convertible_v<T, StringView> -StringView format_param(const T& val) { return val; } - -template<typename T> requires (not std::is_convertible_v<T, StringView>) -decltype(auto) format_param(const T& val) { return to_string(val); } - -} - -String format(StringView fmt, ArrayView<const StringView> params); - -template<typename... Types> -String format(StringView fmt, Types&&... params) -{ - return format(fmt, ArrayView<const StringView>{detail::format_param(std::forward<Types>(params))...}); -} - -StringView format_to(ArrayView<char> buffer, StringView fmt, ArrayView<const StringView> params); - -template<typename... Types> -StringView format_to(ArrayView<char> buffer, StringView fmt, Types&&... params) -{ - return format_to(buffer, fmt, ArrayView<const StringView>{detail::format_param(std::forward<Types>(params))...}); -} - -void format_with(FunctionRef<void (StringView)> append, StringView fmt, ArrayView<const StringView> params); - -template<typename... Types> -void format_with(FunctionRef<void (StringView)> append, StringView fmt, Types&&... params) -{ - return format_with(append, fmt, ArrayView<const StringView>{detail::format_param(std::forward<Types>(params))...}); -} - String double_up(StringView s, StringView characters); inline String quote(StringView s) |
