mirror of
https://github.com/shadps4-emu/ext-fmt.git
synced 2024-11-27 03:30:32 +00:00
Improve scan
This commit is contained in:
parent
d06921d8d8
commit
c4f2de4933
@ -127,11 +127,15 @@ TEST(scan_test, example) {
|
|||||||
TEST(scan_test, file) {
|
TEST(scan_test, file) {
|
||||||
fmt::file read_end, write_end;
|
fmt::file read_end, write_end;
|
||||||
fmt::file::pipe(read_end, write_end);
|
fmt::file::pipe(read_end, write_end);
|
||||||
fmt::string_view input = "42";
|
|
||||||
|
fmt::string_view input = "10 20";
|
||||||
write_end.write(input.data(), input.size());
|
write_end.write(input.data(), input.size());
|
||||||
write_end.close();
|
write_end.close();
|
||||||
int value = 0;
|
|
||||||
fmt::scan(read_end.fdopen("r").get(), "{}", value);
|
int n1 = 0, n2 = 0;
|
||||||
EXPECT_EQ(value, 42);
|
fmt::buffered_file f = read_end.fdopen("r");
|
||||||
|
fmt::scan(f.get(), "{} {}", n1, n2);
|
||||||
|
EXPECT_EQ(n1, 10);
|
||||||
|
EXPECT_EQ(n2, 20);
|
||||||
}
|
}
|
||||||
#endif // FMT_USE_FCNTL
|
#endif // FMT_USE_FCNTL
|
||||||
|
30
test/scan.h
30
test/scan.h
@ -14,6 +14,10 @@
|
|||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
inline bool is_whitespace(char c) {
|
||||||
|
return c == ' ' || c == '\n';
|
||||||
|
}
|
||||||
|
|
||||||
struct maybe_contiguous_range {
|
struct maybe_contiguous_range {
|
||||||
const char* begin;
|
const char* begin;
|
||||||
const char* end;
|
const char* end;
|
||||||
@ -39,21 +43,19 @@ class scan_buffer {
|
|||||||
end_ = end;
|
end_ = end;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto peek() -> int {
|
const char* ptr() const { return ptr_; }
|
||||||
if (ptr_ == end_) {
|
|
||||||
// TODO: refill buffer
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
return *ptr_;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fills the buffer with more input if available.
|
auto peek() -> int {
|
||||||
virtual void consume() = 0;
|
return ptr_ != end_ ? *ptr_ : EOF;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
scan_buffer(const scan_buffer&) = delete;
|
scan_buffer(const scan_buffer&) = delete;
|
||||||
void operator=(const scan_buffer&) = delete;
|
void operator=(const scan_buffer&) = delete;
|
||||||
|
|
||||||
|
// Fills the buffer with more input if available.
|
||||||
|
virtual void consume() = 0;
|
||||||
|
|
||||||
class iterator {
|
class iterator {
|
||||||
private:
|
private:
|
||||||
const char** ptr_;
|
const char** ptr_;
|
||||||
@ -76,6 +78,7 @@ class scan_buffer {
|
|||||||
|
|
||||||
iterator(scan_buffer* buf)
|
iterator(scan_buffer* buf)
|
||||||
: ptr_(&buf->ptr_), buf_(buf), value_(static_cast<char>(buf->peek())) {
|
: ptr_(&buf->ptr_), buf_(buf), value_(static_cast<char>(buf->peek())) {
|
||||||
|
// TODO: fix check
|
||||||
if (value_ == EOF) ptr_ = sentinel();
|
if (value_ == EOF) ptr_ = sentinel();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,7 +238,8 @@ 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
|
// TODO: do it more efficiently
|
||||||
for (size_t i = 0, n = file_.buffer().size(); i != n; ++i) file_.get();
|
size_t n = to_unsigned(ptr() - file_.buffer().begin());
|
||||||
|
for (size_t i = 0; i != n; ++i) file_.get();
|
||||||
fill();
|
fill();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,8 +288,7 @@ struct scan_context {
|
|||||||
auto end() const -> iterator { return buf_.end(); }
|
auto end() const -> iterator { return buf_.end(); }
|
||||||
|
|
||||||
void advance_to(iterator) {
|
void advance_to(iterator) {
|
||||||
// The scan_buffer iterator automatically updates the buffer position when
|
buf_.consume();
|
||||||
// incremented.
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -418,6 +421,7 @@ struct scan_handler : error_handler {
|
|||||||
auto pos() const -> scan_buffer::iterator { return scan_ctx_.begin(); }
|
auto pos() const -> scan_buffer::iterator { return scan_ctx_.begin(); }
|
||||||
|
|
||||||
void on_text(const char* begin, const char* end) {
|
void on_text(const char* begin, const char* end) {
|
||||||
|
if (begin == end) return;
|
||||||
auto it = scan_ctx_.begin(), scan_end = scan_ctx_.end();
|
auto it = scan_ctx_.begin(), scan_end = scan_ctx_.end();
|
||||||
for (; begin != end; ++begin, ++it) {
|
for (; begin != end; ++begin, ++it) {
|
||||||
if (it == scan_end || *begin != *it) on_error("invalid input");
|
if (it == scan_end || *begin != *it) on_error("invalid input");
|
||||||
@ -438,6 +442,8 @@ struct scan_handler : error_handler {
|
|||||||
|
|
||||||
void on_replacement_field(int, const char*) {
|
void on_replacement_field(int, const char*) {
|
||||||
auto it = scan_ctx_.begin(), end = scan_ctx_.end();
|
auto it = scan_ctx_.begin(), end = scan_ctx_.end();
|
||||||
|
while (it != end && is_whitespace(*it)) ++it;
|
||||||
|
scan_ctx_.advance_to(it);
|
||||||
switch (arg_.type) {
|
switch (arg_.type) {
|
||||||
case scan_type::int_type:
|
case scan_type::int_type:
|
||||||
*arg_.int_value = read_int();
|
*arg_.int_value = read_int();
|
||||||
|
Loading…
Reference in New Issue
Block a user