Initial range support in scan

This commit is contained in:
Victor Zverovich 2023-12-29 06:25:57 -08:00
parent ea1066bbe3
commit a3bf40838f
2 changed files with 18 additions and 8 deletions

View File

@ -76,7 +76,6 @@ TEST(scan_test, separator) {
EXPECT_EQ(n2, 20);
}
#ifdef FMT_HAVE_STRPTIME
struct num {
int value;
};
@ -93,10 +92,9 @@ template <> struct scanner<num> {
}
template <class ScanContext>
auto scan(num&, ScanContext& ctx) const -> typename ScanContext::iterator {
// TODO
// return fmt::scan({ctx.begin(), ctx.end()}, "{}", n.value);
return ctx.begin();
auto scan(num& n, ScanContext& ctx) const -> typename ScanContext::iterator {
// TODO: handle specifier
return fmt::scan(ctx, "{}", n.value);
}
};
} // namespace fmt
@ -105,9 +103,8 @@ TEST(scan_test, read_custom) {
auto input = "42";
auto n = num();
fmt::scan(input, "{:}", n);
// EXPECT_EQ(n, 42);
EXPECT_EQ(n.value, 42);
}
#endif
TEST(scan_test, invalid_format) {
EXPECT_THROW_MSG(fmt::scan("", "{}"), fmt::format_error,

View File

@ -94,6 +94,10 @@ class scan_buffer {
value_ = *buf->ptr_;
}
friend scan_buffer& get_buffer(iterator it) {
return *it.buf_;
}
public:
iterator() : ptr_(sentinel()), buf_(nullptr) {}
@ -154,7 +158,7 @@ class string_scan_buffer : public scan_buffer {
#ifdef _WIN32
void flockfile(FILE* f) { _lock_file(f); }
void funlockfile(FILE* f) { _unlock_file(f); }
int getc_unlocked(FILE *f) { return _fgetc_nolock(f); }
int getc_unlocked(FILE* f) { return _fgetc_nolock(f); }
#endif
// A FILE wrapper. F is FILE defined as a template parameter to make
@ -534,6 +538,15 @@ auto scan(string_view input, string_view fmt, T&... args)
return input.begin() + (buf.begin().base() - input.data());
}
template <typename InputRange, typename... T,
FMT_ENABLE_IF(!std::is_convertible<InputRange, string_view>::value)>
auto scan(InputRange&& input, string_view fmt, T&... args)
-> decltype(std::begin(input)) {
auto it = std::begin(input);
vscan(get_buffer(it), fmt, make_scan_args(args...));
return it;
}
template <typename... T> bool scan(std::FILE* f, string_view fmt, T&... args) {
auto&& buf = detail::file_scan_buffer(f);
vscan(buf, fmt, make_scan_args(args...));