From 91b30e5b4ed57ff4ad43b399c5a761b36fc74325 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 10 Feb 2024 07:38:29 -0800 Subject: [PATCH] More API details --- test/scan-test.cc | 12 +++++++++++- test/scan.h | 19 ++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/test/scan-test.cc b/test/scan-test.cc index 2ff322a7..db27a5b2 100644 --- a/test/scan-test.cc +++ b/test/scan-test.cc @@ -110,15 +110,25 @@ TEST(scan_test, invalid_format) { "invalid format string"); } +namespace std { + using fmt::scan; + using fmt::scan_error; +} + TEST(scan_test, example) { // Example from https://wg21.link/p1729r3. - if (auto result = fmt::scan("answer = 42", "{} = {}")) { + if (auto result = std::scan("answer = 42", "{} = {}")) { + auto range = result->range(); + EXPECT_EQ(range.begin(), range.end()); + EXPECT_EQ(result->begin(), result->end()); #ifdef __cpp_structured_bindings const auto& [key, value] = result->values(); EXPECT_EQ(key, "answer"); EXPECT_EQ(value, 42); #endif } else { + std::scan_error error = result.error(); + (void)error; FAIL(); } } diff --git a/test/scan.h b/test/scan.h index 7e477fed..7e340043 100644 --- a/test/scan.h +++ b/test/scan.h @@ -570,9 +570,10 @@ void make_args(std::array& args, } } // namespace detail -template class scan_data { +template class scan_data { private: std::tuple values_; + Range range_; public: scan_data() = default; @@ -589,6 +590,11 @@ template class scan_data { detail::make_args<0>(args, values_); return args; } + + auto range() const -> Range { return range_; } + + auto begin() const -> decltype(range_.begin()) { return range_.begin(); } + auto end() const -> decltype(range_.end()) { return range_.end(); } }; template @@ -610,10 +616,12 @@ template class expected { explicit operator bool() const { return has_value_; } auto operator->() const -> const T* { return &value_; } + + auto error() -> E const { return E(); } }; -template -using scan_result = expected, scan_error>; +template +using scan_result = expected, scan_error>; auto vscan(string_view input, string_view fmt, scan_args args) -> string_view::iterator { @@ -630,8 +638,9 @@ auto scan_to(string_view input, string_view fmt, T&... args) } template -auto scan(string_view input, string_view fmt) -> scan_result { - auto data = scan_data(); +auto scan(string_view input, string_view fmt) + -> scan_result { + auto data = scan_data(); vscan(input, fmt, data.make_args()); return data; }