mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 18:57:34 +02:00
Simplify test
This commit is contained in:
@ -68,40 +68,43 @@ TEST(scan_test, read_string_view) {
|
|||||||
EXPECT_EQ(s, "foo");
|
EXPECT_EQ(s, "foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(scan_test, separator) {
|
||||||
|
int n1 = 0, n2 = 0;
|
||||||
|
fmt::scan("10 20", "{} {}", n1, n2);
|
||||||
|
EXPECT_EQ(n1, 10);
|
||||||
|
EXPECT_EQ(n2, 20);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef FMT_HAVE_STRPTIME
|
#ifdef FMT_HAVE_STRPTIME
|
||||||
|
struct num {
|
||||||
|
int value;
|
||||||
|
};
|
||||||
|
|
||||||
namespace fmt {
|
namespace fmt {
|
||||||
template <> struct scanner<tm> {
|
template <> struct scanner<num> {
|
||||||
std::string format;
|
bool hex = false;
|
||||||
|
|
||||||
auto parse(scan_parse_context& ctx) -> scan_parse_context::iterator {
|
auto parse(scan_parse_context& ctx) -> scan_parse_context::iterator {
|
||||||
auto it = ctx.begin();
|
auto it = ctx.begin(), end = ctx.end();
|
||||||
if (it != ctx.end() && *it == ':') ++it;
|
if (it != end && *it == 'x') hex = true;
|
||||||
auto end = it;
|
if (it != end && *it != '}') throw_format_error("invalid format");
|
||||||
while (end != ctx.end() && *end != '}') ++end;
|
return it;
|
||||||
format.reserve(detail::to_unsigned(end - it + 1));
|
|
||||||
format.append(it, end);
|
|
||||||
format.push_back('\0');
|
|
||||||
return end;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class ScanContext>
|
template <class ScanContext>
|
||||||
auto scan(tm&, ScanContext& ctx) const -> typename ScanContext::iterator {
|
auto scan(num&, ScanContext& ctx) const -> typename ScanContext::iterator {
|
||||||
// TODO: replace strptime with get_time
|
// TODO
|
||||||
// auto result = strptime(ctx.begin(), format.c_str(), &t);
|
//return fmt::scan({ctx.begin(), ctx.end()}, "{}", n.value);
|
||||||
// if (!result) throw format_error("failed to parse time");
|
|
||||||
// return result;
|
|
||||||
return ctx.begin();
|
return ctx.begin();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace fmt
|
} // namespace fmt
|
||||||
|
|
||||||
TEST(scan_test, read_custom) {
|
TEST(scan_test, read_custom) {
|
||||||
/*auto input = "Date: 1985-10-25";
|
auto input = "42";
|
||||||
auto t = tm();
|
auto n = num();
|
||||||
fmt::scan(input, "Date: {0:%Y-%m-%d}", t);
|
fmt::scan(input, "{:}", n);
|
||||||
EXPECT_EQ(t.tm_year, 85);
|
//EXPECT_EQ(n, 42);
|
||||||
EXPECT_EQ(t.tm_mon, 9);
|
|
||||||
EXPECT_EQ(t.tm_mday, 25);*/
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -113,8 +116,8 @@ TEST(scan_test, invalid_format) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(scan_test, example) {
|
TEST(scan_test, example) {
|
||||||
auto key = std::string();
|
std::string key;
|
||||||
auto value = int();
|
int value = 0;
|
||||||
fmt::scan("answer = 42", "{} = {}", key, value);
|
fmt::scan("answer = 42", "{} = {}", key, value);
|
||||||
EXPECT_EQ(key, "answer");
|
EXPECT_EQ(key, "answer");
|
||||||
EXPECT_EQ(value, 42);
|
EXPECT_EQ(value, 42);
|
||||||
|
@ -234,6 +234,7 @@ class file_scan_buffer : public scan_buffer {
|
|||||||
|
|
||||||
void consume() override {
|
void consume() override {
|
||||||
// Consume the current buffer content.
|
// Consume the current buffer content.
|
||||||
|
// TODO: do it more efficiently
|
||||||
for (size_t i = 0, n = file_.buffer().size(); i != n; ++i) file_.get();
|
for (size_t i = 0, n = file_.buffer().size(); i != n; ++i) file_.get();
|
||||||
fill();
|
fill();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user