Skip to content

Commit a3bf408

Browse files
committed
Initial range support in scan
1 parent ea1066b commit a3bf408

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

test/scan-test.cc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ TEST(scan_test, separator) {
7676
EXPECT_EQ(n2, 20);
7777
}
7878

79-
#ifdef FMT_HAVE_STRPTIME
8079
struct num {
8180
int value;
8281
};
@@ -93,10 +92,9 @@ template <> struct scanner<num> {
9392
}
9493

9594
template <class ScanContext>
96-
auto scan(num&, ScanContext& ctx) const -> typename ScanContext::iterator {
97-
// TODO
98-
// return fmt::scan({ctx.begin(), ctx.end()}, "{}", n.value);
99-
return ctx.begin();
95+
auto scan(num& n, ScanContext& ctx) const -> typename ScanContext::iterator {
96+
// TODO: handle specifier
97+
return fmt::scan(ctx, "{}", n.value);
10098
}
10199
};
102100
} // namespace fmt
@@ -105,9 +103,8 @@ TEST(scan_test, read_custom) {
105103
auto input = "42";
106104
auto n = num();
107105
fmt::scan(input, "{:}", n);
108-
// EXPECT_EQ(n, 42);
106+
EXPECT_EQ(n.value, 42);
109107
}
110-
#endif
111108

112109
TEST(scan_test, invalid_format) {
113110
EXPECT_THROW_MSG(fmt::scan("", "{}"), fmt::format_error,

test/scan.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ class scan_buffer {
9494
value_ = *buf->ptr_;
9595
}
9696

97+
friend scan_buffer& get_buffer(iterator it) {
98+
return *it.buf_;
99+
}
100+
97101
public:
98102
iterator() : ptr_(sentinel()), buf_(nullptr) {}
99103

@@ -154,7 +158,7 @@ class string_scan_buffer : public scan_buffer {
154158
#ifdef _WIN32
155159
void flockfile(FILE* f) { _lock_file(f); }
156160
void funlockfile(FILE* f) { _unlock_file(f); }
157-
int getc_unlocked(FILE *f) { return _fgetc_nolock(f); }
161+
int getc_unlocked(FILE* f) { return _fgetc_nolock(f); }
158162
#endif
159163

160164
// 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)
534538
return input.begin() + (buf.begin().base() - input.data());
535539
}
536540

541+
template <typename InputRange, typename... T,
542+
FMT_ENABLE_IF(!std::is_convertible<InputRange, string_view>::value)>
543+
auto scan(InputRange&& input, string_view fmt, T&... args)
544+
-> decltype(std::begin(input)) {
545+
auto it = std::begin(input);
546+
vscan(get_buffer(it), fmt, make_scan_args(args...));
547+
return it;
548+
}
549+
537550
template <typename... T> bool scan(std::FILE* f, string_view fmt, T&... args) {
538551
auto&& buf = detail::file_scan_buffer(f);
539552
vscan(buf, fmt, make_scan_args(args...));

0 commit comments

Comments
 (0)