Implement format_parse_context::starts_with()
Add method to check whether the format string starts with a given prefix.
This commit is contained in:
parent
8abfc145be
commit
b23be10416
@ -675,6 +675,20 @@ template <typename Char> class basic_format_parse_context {
|
|||||||
*/
|
*/
|
||||||
constexpr auto end() const noexcept -> iterator { return format_str_.end(); }
|
constexpr auto end() const noexcept -> iterator { return format_str_.end(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the format string starts with the given prefix
|
||||||
|
*/
|
||||||
|
constexpr bool starts_with(iterator prefix) const noexcept {
|
||||||
|
auto first = begin();
|
||||||
|
auto last = end();
|
||||||
|
while (first != last && *prefix != '\0') {
|
||||||
|
if (*prefix++ != *first++) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *prefix == '\0';
|
||||||
|
}
|
||||||
|
|
||||||
/** Advances the begin iterator to ``it``. */
|
/** Advances the begin iterator to ``it``. */
|
||||||
FMT_CONSTEXPR void advance_to(iterator it) {
|
FMT_CONSTEXPR void advance_to(iterator it) {
|
||||||
format_str_.remove_prefix(detail::to_unsigned(it - begin()));
|
format_str_.remove_prefix(detail::to_unsigned(it - begin()));
|
||||||
|
|||||||
@ -38,12 +38,23 @@ struct custom_type {
|
|||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
template <> struct formatter<custom_type> {
|
template <> struct formatter<custom_type> {
|
||||||
auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
|
private:
|
||||||
|
bool hex = false;
|
||||||
|
|
||||||
|
public:
|
||||||
|
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
|
||||||
|
if (ctx.starts_with("hex")) {
|
||||||
|
hex = true;
|
||||||
|
return ctx.begin() + 3;
|
||||||
|
}
|
||||||
return ctx.begin();
|
return ctx.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(const custom_type& p, FormatContext& ctx) -> decltype(ctx.out()) {
|
auto format(const custom_type& p, FormatContext& ctx) -> decltype(ctx.out()) {
|
||||||
|
if (hex) {
|
||||||
|
return fmt::format_to(ctx.out(), "cust={:x}", p.i);
|
||||||
|
}
|
||||||
return fmt::format_to(ctx.out(), "cust={}", p.i);
|
return fmt::format_to(ctx.out(), "cust={}", p.i);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -57,9 +68,9 @@ TEST(args_test, custom_format) {
|
|||||||
store.push_back(c);
|
store.push_back(c);
|
||||||
++c.i;
|
++c.i;
|
||||||
store.push_back(std::cref(c));
|
store.push_back(std::cref(c));
|
||||||
++c.i;
|
c.i = 15;
|
||||||
auto result = fmt::vformat("{} and {} and {}", store);
|
auto result = fmt::vformat("{} and {} and {:hex}", store);
|
||||||
EXPECT_EQ("cust=0 and cust=1 and cust=3", result);
|
EXPECT_EQ("cust=0 and cust=1 and cust=f", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct to_stringable {
|
struct to_stringable {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user