-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
char formatters for utf string/views (#2)
- Loading branch information
Showing
6 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <stralgo/utf/utf.h> | ||
#include <format> | ||
#include <ranges> | ||
|
||
namespace stralgo::utf::detail | ||
{ | ||
template<typename input_range, typename output_iterator> | ||
inline auto tansform_to_conetxt(input_range const & str, output_iterator oit) | ||
{ | ||
stralgo::utf::utf_input_view_t source{str}; | ||
stralgo::utf::utf_explicit_output_iterator_t<char>::iterator utf_oit{oit}; | ||
return std::ranges::copy(source, utf_oit).out.base(); | ||
} | ||
} // namespace stralgo::utf::detail | ||
|
||
namespace stralgo::concepts | ||
{ | ||
template<typename char_type> | ||
concept format_char_type | ||
= std::same_as<char_type, wchar_t> or std::same_as<char_type, char16_t> or std::same_as<char_type, char32_t>; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <stralgo/utf/utf.h> | ||
#include <stralgo/utf/detail/foramtters.h> | ||
#include <small_vectors/basic_string.h> | ||
#include <format> | ||
#include <ranges> | ||
|
||
template<stralgo::concepts::format_char_type char_type, uint64_t capacity, typename tag> | ||
struct std::formatter<small_vectors::basic_string_t<char_type, capacity, tag>, char> | ||
{ | ||
template<typename ParseContext> | ||
constexpr auto parse(ParseContext & ctx) -> decltype(ctx.begin()) | ||
{ | ||
return ctx.begin(); | ||
} | ||
|
||
template<typename FormatContext> | ||
auto format(small_vectors::basic_string_t<char_type, capacity, tag> const & str, FormatContext & ctx) const | ||
-> decltype(ctx.out()) | ||
{ | ||
return stralgo::utf::detail::tansform_to_conetxt(str, ctx.out()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <stralgo/utf/utf.h> | ||
#include <stralgo/utf/detail/foramtters.h> | ||
#include <format> | ||
#include <ranges> | ||
|
||
template<stralgo::concepts::format_char_type char_type> | ||
struct std::formatter<std::basic_string<char_type>, char> | ||
{ | ||
template<typename ParseContext> | ||
constexpr auto parse(ParseContext & ctx) -> decltype(ctx.begin()) | ||
{ | ||
return ctx.begin(); | ||
} | ||
|
||
template<typename FormatContext> | ||
auto format(std::basic_string<char_type> const & str, FormatContext & ctx) const -> decltype(ctx.out()) | ||
{ | ||
return stralgo::utf::detail::tansform_to_conetxt(str, ctx.out()); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <stralgo/utf/utf.h> | ||
#include <stralgo/utf/detail/foramtters.h> | ||
#include <format> | ||
#include <ranges> | ||
|
||
template<stralgo::concepts::format_char_type char_type> | ||
struct std::formatter<std::basic_string_view<char_type>, char> | ||
{ | ||
template<typename ParseContext> | ||
constexpr auto parse(ParseContext & ctx) -> decltype(ctx.begin()) | ||
{ | ||
return ctx.begin(); | ||
} | ||
|
||
template<typename FormatContext> | ||
auto format(std::basic_string_view<char_type> str, FormatContext & ctx) const -> decltype(ctx.out()) | ||
{ | ||
return stralgo::utf::detail::tansform_to_conetxt(str, ctx.out()); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters