fmt/test/scan.h

670 lines
19 KiB
C
Raw Normal View History

// Formatting library for C++ - scanning API proof of concept
//
// Copyright (c) 2019 - present, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#include <array>
#include <cassert>
#include <climits>
2024-01-21 15:56:56 +00:00
#include <tuple>
2024-01-21 16:53:29 +00:00
#include "fmt/format-inl.h"
FMT_BEGIN_NAMESPACE
2023-11-26 17:22:31 +00:00
namespace detail {
2024-01-01 16:39:12 +00:00
inline auto is_whitespace(char c) -> bool { return c == ' ' || c == '\n'; }
// If c is a hex digit returns its numeric value, otherwise -1.
2024-01-01 16:39:12 +00:00
inline auto to_hex_digit(char c) -> int {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
2023-12-24 15:32:27 +00:00
2023-12-02 17:34:27 +00:00
struct maybe_contiguous_range {
const char* begin;
const char* end;
explicit operator bool() const { return begin != nullptr; }
};
2023-11-26 17:22:31 +00:00
class scan_buffer {
private:
const char* ptr_;
2023-12-02 17:34:27 +00:00
const char* end_;
bool contiguous_;
2023-11-26 17:22:31 +00:00
protected:
2023-12-02 17:34:27 +00:00
scan_buffer(const char* ptr, const char* end, bool contiguous)
: ptr_(ptr), end_(end), contiguous_(contiguous) {}
2023-11-26 17:22:31 +00:00
~scan_buffer() = default;
2024-01-18 15:27:59 +00:00
void set(span<const char> buf) {
ptr_ = buf.data;
end_ = buf.data + buf.size;
2023-11-26 17:22:31 +00:00
}
2023-12-25 18:16:55 +00:00
auto ptr() const -> const char* { return ptr_; }
2023-12-24 15:32:27 +00:00
2023-11-26 17:22:31 +00:00
public:
scan_buffer(const scan_buffer&) = delete;
void operator=(const scan_buffer&) = delete;
2023-12-24 15:32:27 +00:00
// Fills the buffer with more input if available.
virtual void consume() = 0;
2024-01-01 15:25:01 +00:00
class sentinel {};
2023-12-02 17:34:27 +00:00
class iterator {
private:
const char** ptr_;
scan_buffer* buf_; // This could be merged with ptr_.
char value_;
2024-01-01 15:25:01 +00:00
static auto get_sentinel() -> const char** {
2023-12-02 17:34:27 +00:00
static const char* ptr = nullptr;
return &ptr;
}
friend class scan_buffer;
2024-01-01 15:25:01 +00:00
friend auto operator==(iterator lhs, sentinel) -> bool {
return *lhs.ptr_ == nullptr;
2023-12-02 17:34:27 +00:00
}
2024-01-01 15:25:01 +00:00
friend auto operator!=(iterator lhs, sentinel) -> bool {
return *lhs.ptr_ != nullptr;
2023-12-02 17:34:27 +00:00
}
2023-12-25 18:47:45 +00:00
iterator(scan_buffer* buf) : buf_(buf) {
if (buf->ptr_ == buf->end_) {
2024-01-01 15:25:01 +00:00
ptr_ = get_sentinel();
2023-12-25 18:47:45 +00:00
return;
}
ptr_ = &buf->ptr_;
value_ = *buf->ptr_;
2023-12-02 17:34:27 +00:00
}
2023-12-31 16:11:54 +00:00
friend scan_buffer& get_buffer(iterator it) { return *it.buf_; }
2023-12-29 14:25:57 +00:00
2023-12-02 17:34:27 +00:00
public:
2024-01-01 15:25:01 +00:00
iterator() : ptr_(get_sentinel()), buf_(nullptr) {}
2023-11-26 17:22:31 +00:00
2023-12-02 17:34:27 +00:00
auto operator++() -> iterator& {
2024-01-01 15:25:01 +00:00
if (!buf_->try_consume()) ptr_ = get_sentinel();
2023-12-02 17:34:27 +00:00
value_ = *buf_->ptr_;
return *this;
}
auto operator++(int) -> iterator {
iterator copy = *this;
++*this;
return copy;
}
auto operator*() const -> char { return value_; }
auto base() const -> const char* { return buf_->ptr_; }
friend auto to_contiguous(iterator it) -> maybe_contiguous_range;
2023-12-31 17:51:24 +00:00
friend auto advance(iterator it, size_t n) -> iterator;
2023-12-02 17:34:27 +00:00
};
friend auto to_contiguous(iterator it) -> maybe_contiguous_range {
if (it.buf_->is_contiguous()) return {it.buf_->ptr_, it.buf_->end_};
return {nullptr, nullptr};
}
2023-12-31 17:51:24 +00:00
friend auto advance(iterator it, size_t n) -> iterator {
2023-12-02 17:34:27 +00:00
FMT_ASSERT(it.buf_->is_contiguous(), "");
const char*& ptr = it.buf_->ptr_;
ptr += n;
it.value_ = *ptr;
2024-01-01 15:25:01 +00:00
if (ptr == it.buf_->end_) it.ptr_ = iterator::get_sentinel();
2023-12-31 17:51:24 +00:00
return it;
2023-12-02 17:34:27 +00:00
}
2023-11-26 17:22:31 +00:00
2023-12-25 18:28:50 +00:00
auto begin() -> iterator { return this; }
2024-01-01 15:25:01 +00:00
auto end() -> sentinel { return {}; }
2023-12-02 17:34:27 +00:00
auto is_contiguous() const -> bool { return contiguous_; }
2023-12-25 18:28:50 +00:00
// Tries consuming a single code unit. Returns true iff there is more input.
2023-12-02 17:34:27 +00:00
auto try_consume() -> bool {
FMT_ASSERT(ptr_ != end_, "");
++ptr_;
2023-12-22 14:35:36 +00:00
if (ptr_ != end_) return true;
consume();
return ptr_ != end_;
2023-11-26 17:22:31 +00:00
}
};
2024-01-01 15:40:05 +00:00
using scan_iterator = scan_buffer::iterator;
using scan_sentinel = scan_buffer::sentinel;
2024-03-10 17:27:42 +00:00
class string_scan_buffer final : public scan_buffer {
2023-11-26 17:22:31 +00:00
private:
2023-12-22 14:35:36 +00:00
void consume() override {}
2023-11-26 17:22:31 +00:00
public:
2023-12-02 17:34:27 +00:00
explicit string_scan_buffer(string_view s)
: scan_buffer(s.begin(), s.end(), true) {}
2023-11-26 17:22:31 +00:00
};
2024-03-10 17:27:42 +00:00
class file_scan_buffer final : public scan_buffer {
2023-12-22 18:50:01 +00:00
private:
template <typename F, FMT_ENABLE_IF(sizeof(F::_IO_read_ptr) != 0 &&
!FMT_USE_FALLBACK_FILE)>
2023-12-25 17:22:29 +00:00
static auto get_file(F* f, int) -> glibc_file<F> {
return f;
}
template <typename F,
FMT_ENABLE_IF(sizeof(F::_p) != 0 && !FMT_USE_FALLBACK_FILE)>
2023-12-25 17:22:29 +00:00
static auto get_file(F* f, int) -> apple_file<F> {
return f;
}
static auto get_file(FILE* f, ...) -> fallback_file<FILE> { return f; }
2023-12-22 14:35:36 +00:00
2023-12-25 17:22:29 +00:00
decltype(get_file(static_cast<FILE*>(nullptr), 0)) file_;
2023-12-25 17:00:03 +00:00
2023-12-25 17:05:26 +00:00
// Fills the buffer if it is empty.
2023-12-22 21:39:18 +00:00
void fill() {
2024-01-18 15:27:59 +00:00
span<const char> buf = file_.get_read_buffer();
if (buf.size == 0) {
2023-12-22 18:50:01 +00:00
int c = file_.get();
// Put the character back since we are only filling the buffer.
if (c != EOF) file_.unget(static_cast<char>(c));
2024-01-18 15:27:59 +00:00
buf = file_.get_read_buffer();
2023-12-22 14:35:36 +00:00
}
2023-12-25 18:28:50 +00:00
set(buf);
2023-11-26 17:22:31 +00:00
}
2023-12-22 14:35:36 +00:00
void consume() override {
// Consume the current buffer content.
2024-01-18 15:27:59 +00:00
size_t n = to_unsigned(ptr() - file_.get_read_buffer().data);
2023-12-24 15:32:27 +00:00
for (size_t i = 0; i != n; ++i) file_.get();
2023-12-22 21:39:18 +00:00
fill();
2023-11-26 17:22:31 +00:00
}
public:
explicit file_scan_buffer(FILE* f)
2023-12-02 17:34:27 +00:00
: scan_buffer(nullptr, nullptr, false), file_(f) {
2023-12-25 16:39:14 +00:00
flockfile(f);
2023-12-22 21:39:18 +00:00
fill();
2023-11-26 17:22:31 +00:00
}
~file_scan_buffer() {
FILE* f = file_;
funlockfile(f);
}
2023-11-26 17:22:31 +00:00
};
} // namespace detail
template <typename T, typename Char = char> struct scanner {
// A deleted default constructor indicates a disabled scanner.
scanner() = delete;
};
class scan_parse_context {
private:
string_view format_;
public:
using iterator = string_view::iterator;
explicit FMT_CONSTEXPR scan_parse_context(string_view format)
: format_(format) {}
2023-11-25 15:41:04 +00:00
FMT_CONSTEXPR auto begin() const -> iterator { return format_.begin(); }
FMT_CONSTEXPR auto end() const -> iterator { return format_.end(); }
void advance_to(iterator it) {
2020-05-10 14:25:42 +00:00
format_.remove_prefix(detail::to_unsigned(it - begin()));
}
};
2020-05-10 14:25:42 +00:00
namespace detail {
enum class scan_type {
none_type,
int_type,
uint_type,
long_long_type,
ulong_long_type,
string_type,
string_view_type,
custom_type
};
2024-01-01 16:39:12 +00:00
template <typename Context> struct custom_scan_arg {
void* value;
2023-12-31 16:11:54 +00:00
void (*scan)(void* arg, scan_parse_context& parse_ctx, Context& ctx);
};
2023-12-31 16:11:54 +00:00
} // namespace detail
// A scan argument. Context is a template parameter for the compiled API where
// output can be unbuffered.
template <typename Context> class basic_scan_arg {
private:
using scan_type = detail::scan_type;
2023-12-31 17:51:24 +00:00
scan_type type_;
union {
2023-12-31 23:06:17 +00:00
int* int_value_;
unsigned* uint_value_;
long long* long_long_value_;
unsigned long long* ulong_long_value_;
std::string* string_;
2024-01-01 15:40:05 +00:00
string_view* string_view_;
2023-12-31 23:06:17 +00:00
detail::custom_scan_arg<Context> custom_;
// TODO: more types
};
2023-12-31 23:06:17 +00:00
template <typename T>
static void scan_custom_arg(void* arg, scan_parse_context& parse_ctx,
Context& ctx) {
auto s = scanner<T>();
parse_ctx.advance_to(s.parse(parse_ctx));
ctx.advance_to(s.scan(*static_cast<T*>(arg), ctx));
}
public:
2023-12-31 16:11:54 +00:00
FMT_CONSTEXPR basic_scan_arg()
2023-12-31 23:06:17 +00:00
: type_(scan_type::none_type), int_value_(nullptr) {}
2023-12-31 16:11:54 +00:00
FMT_CONSTEXPR basic_scan_arg(int& value)
2023-12-31 23:06:17 +00:00
: type_(scan_type::int_type), int_value_(&value) {}
2023-12-31 16:11:54 +00:00
FMT_CONSTEXPR basic_scan_arg(unsigned& value)
2023-12-31 23:06:17 +00:00
: type_(scan_type::uint_type), uint_value_(&value) {}
2023-12-31 16:11:54 +00:00
FMT_CONSTEXPR basic_scan_arg(long long& value)
2023-12-31 23:06:17 +00:00
: type_(scan_type::long_long_type), long_long_value_(&value) {}
2023-12-31 16:11:54 +00:00
FMT_CONSTEXPR basic_scan_arg(unsigned long long& value)
2023-12-31 23:06:17 +00:00
: type_(scan_type::ulong_long_type), ulong_long_value_(&value) {}
2023-12-31 16:11:54 +00:00
FMT_CONSTEXPR basic_scan_arg(std::string& value)
2023-12-31 23:06:17 +00:00
: type_(scan_type::string_type), string_(&value) {}
2024-01-01 15:40:05 +00:00
FMT_CONSTEXPR basic_scan_arg(string_view& value)
2023-12-31 23:06:17 +00:00
: type_(scan_type::string_view_type), string_view_(&value) {}
2022-09-12 09:01:44 +00:00
template <typename T>
2023-12-31 17:51:24 +00:00
FMT_CONSTEXPR basic_scan_arg(T& value) : type_(scan_type::custom_type) {
2023-12-31 23:06:17 +00:00
custom_.value = &value;
custom_.scan = scan_custom_arg<T>;
}
2023-12-31 16:11:54 +00:00
constexpr explicit operator bool() const noexcept {
2023-12-31 17:51:24 +00:00
return type_ != scan_type::none_type;
2023-12-31 16:11:54 +00:00
}
2023-12-31 17:51:24 +00:00
auto type() const -> detail::scan_type { return type_; }
2023-12-30 15:43:21 +00:00
template <typename Visitor>
2023-12-31 17:51:24 +00:00
auto visit(Visitor&& vis) -> decltype(vis(monostate())) {
switch (type_) {
2023-12-30 15:43:21 +00:00
case scan_type::none_type:
break;
case scan_type::int_type:
2023-12-31 23:06:17 +00:00
return vis(*int_value_);
2023-12-30 15:43:21 +00:00
case scan_type::uint_type:
2023-12-31 23:06:17 +00:00
return vis(*uint_value_);
2023-12-30 15:43:21 +00:00
case scan_type::long_long_type:
2023-12-31 23:06:17 +00:00
return vis(*long_long_value_);
2023-12-30 15:43:21 +00:00
case scan_type::ulong_long_type:
2023-12-31 23:06:17 +00:00
return vis(*ulong_long_value_);
2023-12-30 15:43:21 +00:00
case scan_type::string_type:
2023-12-31 23:06:17 +00:00
return vis(*string_);
2023-12-30 15:43:21 +00:00
case scan_type::string_view_type:
2023-12-31 23:06:17 +00:00
return vis(*string_view_);
2023-12-30 15:43:21 +00:00
case scan_type::custom_type:
break;
}
return vis(monostate());
}
2024-01-01 16:39:12 +00:00
auto scan_custom(const char* parse_begin, scan_parse_context& parse_ctx,
2023-12-31 23:06:17 +00:00
Context& ctx) const -> bool {
if (type_ != scan_type::custom_type) return false;
parse_ctx.advance_to(parse_begin);
custom_.scan(custom_.value, parse_ctx, ctx);
return true;
}
};
2023-12-31 16:11:54 +00:00
class scan_context;
using scan_arg = basic_scan_arg<scan_context>;
struct scan_args {
int size;
2023-12-31 16:11:54 +00:00
const scan_arg* data;
template <size_t N>
2023-12-31 16:11:54 +00:00
FMT_CONSTEXPR scan_args(const std::array<scan_arg, N>& store)
: size(N), data(store.data()) {
static_assert(N < INT_MAX, "too many arguments");
}
};
2023-12-31 16:11:54 +00:00
class scan_context {
private:
detail::scan_buffer& buf_;
scan_args args_;
public:
2024-01-01 15:40:05 +00:00
using iterator = detail::scan_iterator;
using sentinel = detail::scan_sentinel;
2023-12-31 16:11:54 +00:00
explicit FMT_CONSTEXPR scan_context(detail::scan_buffer& buf, scan_args args)
: buf_(buf), args_(args) {}
FMT_CONSTEXPR auto arg(int id) const -> scan_arg {
return id < args_.size ? args_.data[id] : scan_arg();
}
auto begin() const -> iterator { return buf_.begin(); }
2024-01-01 15:25:01 +00:00
auto end() const -> sentinel { return {}; }
2023-12-31 16:11:54 +00:00
void advance_to(iterator) { buf_.consume(); }
};
2020-05-10 14:25:42 +00:00
namespace detail {
2023-12-31 16:11:54 +00:00
const char* parse_scan_specs(const char* begin, const char* end,
format_specs& specs, scan_type) {
2023-12-29 15:15:02 +00:00
while (begin != end) {
switch (to_ascii(*begin)) {
2024-01-01 15:25:01 +00:00
// TODO: parse more scan format specifiers
2023-12-31 16:11:54 +00:00
case 'x':
2024-08-11 17:13:17 +00:00
specs.set_type(presentation_type::hex);
2024-01-01 16:39:12 +00:00
++begin;
2023-12-31 16:11:54 +00:00
break;
case '}':
return begin;
2023-12-29 15:15:02 +00:00
}
}
return begin;
}
2024-01-01 15:40:05 +00:00
template <typename T, FMT_ENABLE_IF(std::is_unsigned<T>::value)>
2024-01-14 17:16:18 +00:00
auto read(scan_iterator it, T& value) -> scan_iterator {
2024-01-01 15:40:05 +00:00
if (it == scan_sentinel()) return it;
char c = *it;
if (c < '0' || c > '9') report_error("invalid input");
2024-01-01 15:40:05 +00:00
int num_digits = 0;
T n = 0, prev = 0;
char prev_digit = c;
do {
prev = n;
n = n * 10 + static_cast<unsigned>(c - '0');
prev_digit = c;
c = *++it;
++num_digits;
if (c < '0' || c > '9') break;
} while (it != scan_sentinel());
// Check overflow.
if (num_digits <= std::numeric_limits<int>::digits10) {
value = n;
2023-12-31 17:51:24 +00:00
return it;
}
2024-01-01 15:40:05 +00:00
unsigned max = to_unsigned((std::numeric_limits<int>::max)());
if (num_digits == std::numeric_limits<int>::digits10 + 1 &&
prev * 10ull + unsigned(prev_digit - '0') <= max) {
value = n;
} else {
report_error("number is too big");
2023-12-31 17:51:24 +00:00
}
2024-01-01 15:40:05 +00:00
return it;
}
2023-12-31 17:51:24 +00:00
2024-01-01 16:39:12 +00:00
template <typename T, FMT_ENABLE_IF(std::is_unsigned<T>::value)>
2024-01-14 17:16:18 +00:00
auto read_hex(scan_iterator it, T& value) -> scan_iterator {
2024-01-01 16:39:12 +00:00
if (it == scan_sentinel()) return it;
int digit = to_hex_digit(*it);
if (digit < 0) report_error("invalid input");
2024-01-01 16:39:12 +00:00
int num_digits = 0;
T n = 0;
do {
n = (n << 4) + static_cast<unsigned>(digit);
++num_digits;
digit = to_hex_digit(*++it);
if (digit < 0) break;
} while (it != scan_sentinel());
2024-01-01 17:17:10 +00:00
// Check overflow.
if (num_digits <= (std::numeric_limits<T>::digits >> 2))
value = n;
else
report_error("number is too big");
2024-01-01 16:39:12 +00:00
return it;
}
template <typename T, FMT_ENABLE_IF(std::is_unsigned<T>::value)>
auto read(scan_iterator it, T& value, const format_specs& specs)
2024-01-01 16:39:12 +00:00
-> scan_iterator {
2024-08-11 17:13:17 +00:00
if (specs.type() == presentation_type::hex) return read_hex(it, value);
2024-01-01 16:39:12 +00:00
return read(it, value);
}
2024-01-01 15:40:05 +00:00
template <typename T, FMT_ENABLE_IF(std::is_signed<T>::value)>
auto read(scan_iterator it, T& value, const format_specs& specs = {})
2024-01-01 16:39:12 +00:00
-> scan_iterator {
2024-01-01 15:40:05 +00:00
bool negative = it != scan_sentinel() && *it == '-';
if (negative) {
++it;
if (it == scan_sentinel()) report_error("invalid input");
2024-01-01 15:40:05 +00:00
}
using unsigned_type = typename std::make_unsigned<T>::type;
unsigned_type abs_value = 0;
2024-01-14 17:16:18 +00:00
it = read(it, abs_value, specs);
2024-01-01 15:40:05 +00:00
auto n = static_cast<T>(abs_value);
value = negative ? -n : n;
return it;
}
auto read(scan_iterator it, std::string& value, const format_specs& = {})
2024-01-01 16:39:12 +00:00
-> scan_iterator {
2024-01-01 15:40:05 +00:00
while (it != scan_sentinel() && *it != ' ') value.push_back(*it++);
return it;
}
auto read(scan_iterator it, string_view& value, const format_specs& = {})
2024-01-01 16:39:12 +00:00
-> scan_iterator {
2024-01-01 15:40:05 +00:00
auto range = to_contiguous(it);
// This could also be checked at compile time in scan.
if (!range) report_error("string_view requires contiguous input");
2024-01-01 15:40:05 +00:00
auto p = range.begin;
while (p != range.end && *p != ' ') ++p;
size_t size = to_unsigned(p - range.begin);
value = {range.begin, size};
return advance(it, size);
}
auto read(scan_iterator it, monostate, const format_specs& = {})
2024-01-01 16:39:12 +00:00
-> scan_iterator {
2024-01-01 15:40:05 +00:00
return it;
}
// An argument scanner that uses the default format, e.g. decimal for integers.
struct default_arg_scanner {
scan_iterator it;
2024-01-01 16:39:12 +00:00
template <typename T> FMT_INLINE auto operator()(T&& value) -> scan_iterator {
2024-01-01 15:40:05 +00:00
return read(it, value);
}
2023-12-31 17:51:24 +00:00
};
2024-01-01 15:40:05 +00:00
// An argument scanner with format specifiers.
2023-12-31 17:51:24 +00:00
struct arg_scanner {
2024-01-01 15:40:05 +00:00
scan_iterator it;
const format_specs& specs;
2023-12-31 17:51:24 +00:00
2024-01-01 16:39:12 +00:00
template <typename T> auto operator()(T&& value) -> scan_iterator {
return read(it, value, specs);
2023-12-31 17:51:24 +00:00
}
};
2024-01-02 00:10:13 +00:00
struct scan_handler {
2023-12-31 17:51:24 +00:00
private:
scan_parse_context parse_ctx_;
scan_context scan_ctx_;
int next_arg_id_;
2024-01-01 15:25:01 +00:00
using sentinel = scan_buffer::sentinel;
public:
2023-12-02 17:34:27 +00:00
FMT_CONSTEXPR scan_handler(string_view format, scan_buffer& buf,
scan_args args)
2023-12-31 16:11:54 +00:00
: parse_ctx_(format), scan_ctx_(buf, args), next_arg_id_(0) {}
2023-12-02 17:34:27 +00:00
auto pos() const -> scan_buffer::iterator { return scan_ctx_.begin(); }
void on_text(const char* begin, const char* end) {
2023-12-24 15:32:27 +00:00
if (begin == end) return;
2024-01-01 15:25:01 +00:00
auto it = scan_ctx_.begin();
2023-12-02 17:34:27 +00:00
for (; begin != end; ++begin, ++it) {
2024-01-01 15:25:01 +00:00
if (it == sentinel() || *begin != *it) on_error("invalid input");
2023-12-02 17:34:27 +00:00
}
scan_ctx_.advance_to(it);
}
2023-11-25 15:41:04 +00:00
FMT_CONSTEXPR auto on_arg_id() -> int { return on_arg_id(next_arg_id_++); }
FMT_CONSTEXPR auto on_arg_id(int id) -> int {
2023-12-31 16:11:54 +00:00
if (!scan_ctx_.arg(id)) on_error("argument index out of range");
2020-06-06 14:13:38 +00:00
return id;
}
2023-11-25 15:41:04 +00:00
FMT_CONSTEXPR auto on_arg_id(string_view id) -> int {
2021-06-01 18:51:59 +00:00
if (id.data()) on_error("invalid format");
return 0;
}
2024-01-14 16:11:13 +00:00
void on_replacement_field(int arg_id, const char* begin) {
2023-12-31 16:11:54 +00:00
scan_arg arg = scan_ctx_.arg(arg_id);
2024-01-14 16:11:13 +00:00
if (arg.scan_custom(begin, parse_ctx_, scan_ctx_)) return;
2024-01-01 15:25:01 +00:00
auto it = scan_ctx_.begin();
while (it != sentinel() && is_whitespace(*it)) ++it;
scan_ctx_.advance_to(arg.visit(default_arg_scanner{it}));
}
2023-12-31 16:11:54 +00:00
auto on_format_specs(int arg_id, const char* begin, const char* end) -> const
char* {
scan_arg arg = scan_ctx_.arg(arg_id);
2023-12-31 23:06:17 +00:00
if (arg.scan_custom(begin, parse_ctx_, scan_ctx_))
2023-12-29 15:15:02 +00:00
return parse_ctx_.begin();
auto specs = format_specs();
2023-12-31 17:51:24 +00:00
begin = parse_scan_specs(begin, end, specs, arg.type());
2023-12-31 16:11:54 +00:00
if (begin == end || *begin != '}') on_error("missing '}' in format string");
2024-01-01 15:40:05 +00:00
scan_ctx_.advance_to(arg.visit(arg_scanner{scan_ctx_.begin(), specs}));
2023-12-29 15:15:02 +00:00
return begin;
}
2023-12-25 16:39:14 +00:00
2024-07-14 14:05:18 +00:00
FMT_NORETURN void on_error(const char* message) { report_error(message); }
};
2024-01-21 15:56:56 +00:00
void vscan(detail::scan_buffer& buf, string_view fmt, scan_args args) {
auto h = detail::scan_handler(fmt, buf, args);
2024-09-01 16:34:38 +00:00
detail::parse_format_string(fmt, h);
2024-01-21 15:56:56 +00:00
}
2024-01-21 16:53:29 +00:00
template <size_t I, typename... T, FMT_ENABLE_IF(I == sizeof...(T))>
void make_args(std::array<scan_arg, sizeof...(T)>&, std::tuple<T...>&) {}
template <size_t I, typename... T, FMT_ENABLE_IF(I < sizeof...(T))>
void make_args(std::array<scan_arg, sizeof...(T)>& args,
std::tuple<T...>& values) {
using element_type = typename std::tuple_element<I, std::tuple<T...>>::type;
static_assert(std::is_same<remove_cvref_t<element_type>, element_type>::value,
"");
args[I] = std::get<I>(values);
make_args<I + 1>(args, values);
}
2024-01-21 16:53:29 +00:00
} // namespace detail
2024-02-10 15:38:29 +00:00
template <typename Range, typename... T> class scan_data {
2024-01-14 16:51:33 +00:00
private:
2024-01-21 15:56:56 +00:00
std::tuple<T...> values_;
2024-02-10 15:38:29 +00:00
Range range_;
2024-01-14 16:51:33 +00:00
public:
2024-01-21 16:53:29 +00:00
scan_data() = default;
2024-01-21 15:56:56 +00:00
scan_data(T... values) : values_(std::move(values)...) {}
2024-01-14 16:51:33 +00:00
2024-01-21 15:56:56 +00:00
auto value() const -> decltype(std::get<0>(values_)) {
return std::get<0>(values_);
}
2024-01-21 16:53:29 +00:00
2024-02-04 15:44:11 +00:00
auto values() const -> const std::tuple<T...>& { return values_; }
2024-01-21 16:53:29 +00:00
auto make_args() -> std::array<scan_arg, sizeof...(T)> {
auto args = std::array<scan_arg, sizeof...(T)>();
detail::make_args<0>(args, values_);
return args;
}
2024-02-10 15:38:29 +00:00
auto range() const -> Range { return range_; }
auto begin() const -> decltype(range_.begin()) { return range_.begin(); }
auto end() const -> decltype(range_.end()) { return range_.end(); }
2024-01-14 16:51:33 +00:00
};
2024-01-21 16:53:29 +00:00
template <typename... T>
auto make_scan_args(T&... args) -> std::array<scan_arg, sizeof...(T)> {
return {{args...}};
}
2024-01-21 15:56:56 +00:00
class scan_error {};
2024-01-14 16:51:33 +00:00
// A rudimentary version of std::expected for testing the API shape.
2024-01-21 15:56:56 +00:00
template <typename T, typename E> class expected {
2024-01-14 16:51:33 +00:00
private:
T value_;
2024-02-04 15:44:11 +00:00
bool has_value_ = true;
2024-01-14 16:51:33 +00:00
public:
expected(T value) : value_(std::move(value)) {}
2024-01-14 17:16:18 +00:00
2024-02-04 15:44:11 +00:00
explicit operator bool() const { return has_value_; }
auto operator->() const -> const T* { return &value_; }
2024-02-10 15:38:29 +00:00
auto error() -> E const { return E(); }
2024-01-14 16:51:33 +00:00
};
2024-02-10 15:38:29 +00:00
template <typename Range, typename... T>
using scan_result = expected<scan_data<Range, T...>, scan_error>;
2024-01-14 17:16:18 +00:00
2024-01-21 15:56:56 +00:00
auto vscan(string_view input, string_view fmt, scan_args args)
-> string_view::iterator {
auto&& buf = detail::string_scan_buffer(input);
detail::vscan(buf, fmt, args);
return input.begin() + (buf.begin().base() - input.data());
2024-01-14 17:16:18 +00:00
}
// Scans the input and stores the results (in)to args.
template <typename... T>
auto scan_to(string_view input, string_view fmt, T&... args)
-> string_view::iterator {
2024-01-21 15:56:56 +00:00
return vscan(input, fmt, make_scan_args(args...));
2024-01-14 17:16:18 +00:00
}
2024-01-14 16:51:33 +00:00
2024-02-04 15:44:11 +00:00
template <typename... T>
2024-02-10 15:38:29 +00:00
auto scan(string_view input, string_view fmt)
-> scan_result<string_view, T...> {
auto data = scan_data<string_view, T...>();
2024-01-21 16:53:29 +00:00
vscan(input, fmt, data.make_args());
return data;
2024-01-14 16:51:33 +00:00
}
2024-01-21 15:56:56 +00:00
template <typename Range, typename... T,
FMT_ENABLE_IF(!std::is_convertible<Range, string_view>::value)>
auto scan_to(Range&& input, string_view fmt, T&... args)
2023-12-29 14:25:57 +00:00
-> decltype(std::begin(input)) {
auto it = std::begin(input);
2024-01-21 15:56:56 +00:00
detail::vscan(get_buffer(it), fmt, make_scan_args(args...));
2023-12-29 14:25:57 +00:00
return it;
}
2024-01-14 17:16:18 +00:00
template <typename... T>
auto scan_to(FILE* f, string_view fmt, T&... args) -> bool {
2023-11-26 17:22:31 +00:00
auto&& buf = detail::file_scan_buffer(f);
2024-01-21 15:56:56 +00:00
detail::vscan(buf, fmt, make_scan_args(args...));
2023-12-25 16:39:14 +00:00
return buf.begin() != buf.end();
2023-11-26 17:22:31 +00:00
}
FMT_END_NAMESPACE