Commit ef6282fc authored by Victor Zverovich's avatar Victor Zverovich

Fix gcc 4.4 build

parent e3e470bb
......@@ -10,27 +10,18 @@
#include "fmt/format.h"
#include "gmock.h"
#include "gtest-extra.h"
namespace fmt {
FMT_BEGIN_NAMESPACE
namespace internal {
struct scan_arg {
int& value;
int* value;
// TODO: more types
};
struct scan_args {
int size;
const scan_arg* data;
template <size_t N>
scan_args(const std::array<scan_arg, N>& store)
: size(N), data(store.data()) {
static_assert(N < INT_MAX, "too many arguments");
}
};
template <typename Handler>
void parse_scan_format(string_view format_str, Handler& handler) {
void parse_scan_format(string_view format_str, Handler&& handler) {
const char* p = format_str.data();
const char* end = p + format_str.size();
while (p != end) {
......@@ -40,15 +31,27 @@ void parse_scan_format(string_view format_str, Handler& handler) {
handler.on_arg();
}
}
} // namespace internal
void vscan(string_view input, string_view format_str, scan_args args) {
struct handler {
struct scan_args {
int size;
const internal::scan_arg* data;
template <size_t N>
scan_args(const std::array<internal::scan_arg, N>& store)
: size(N), data(store.data()) {
static_assert(N < INT_MAX, "too many arguments");
}
};
namespace internal {
struct scan_handler {
const char* begin;
const char* end;
scan_args args;
int next_arg_id;
handler(string_view input, scan_args args)
scan_handler(string_view input, scan_args args)
: begin(input.data()),
end(begin + input.size()),
args(args),
......@@ -63,25 +66,35 @@ void vscan(string_view input, string_view format_str, scan_args args) {
}
if (next_arg_id >= args.size)
throw format_error("argument index out of range");
args.data[0].value = value;
*args.data[0].value = value;
}
} h(input, args);
parse_scan_format(format_str, h);
};
} // namespace internal
void vscan(string_view input, string_view format_str, scan_args args) {
internal::parse_scan_format(format_str, internal::scan_handler(input, args));
}
template <typename... Args>
std::array<scan_arg, sizeof...(Args)> make_scan_args(Args&... args) {
return {args...};
std::array<internal::scan_arg, sizeof...(Args)> make_scan_args(Args&... args) {
return std::array<internal::scan_arg, sizeof...(Args)>{&args...};
}
template <typename... Args>
void scan(string_view input, string_view format_str, Args&... args) {
vscan(input, format_str, make_scan_args(args...));
}
} // namespace fmt
FMT_END_NAMESPACE
TEST(ScanTest, ReadInt) {
int n = 0;
fmt::scan("42", "{}", n);
EXPECT_EQ(n, 42);
}
TEST(ScanTest, InvalidFormat) {
EXPECT_THROW_MSG(fmt::scan("", "{}"), fmt::format_error,
"argument index out of range");
EXPECT_THROW_MSG(fmt::scan("", "{"), fmt::format_error,
"invalid format string");
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment