forked from fmtlib/fmt
Update scan test
This commit is contained in:
@@ -122,11 +122,11 @@ 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 = "4";
|
fmt::string_view input = "42";
|
||||||
write_end.write(input.data(), input.size());
|
write_end.write(input.data(), input.size());
|
||||||
write_end.close();
|
write_end.close();
|
||||||
int value = 0;
|
int value = 0;
|
||||||
fmt::scan(read_end.fdopen("r").get(), "{}", value);
|
fmt::scan(read_end.fdopen("r").get(), "{}", value);
|
||||||
EXPECT_EQ(value, 4);
|
EXPECT_EQ(value, 42);
|
||||||
}
|
}
|
||||||
#endif // FMT_USE_FCNTL
|
#endif // FMT_USE_FCNTL
|
||||||
|
92
test/scan.h
92
test/scan.h
@@ -48,7 +48,7 @@ class scan_buffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fills the buffer with more input if available.
|
// Fills the buffer with more input if available.
|
||||||
virtual void fill() = 0;
|
virtual void consume() = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
scan_buffer(const scan_buffer&) = delete;
|
scan_buffer(const scan_buffer&) = delete;
|
||||||
@@ -121,17 +121,15 @@ class scan_buffer {
|
|||||||
auto try_consume() -> bool {
|
auto try_consume() -> bool {
|
||||||
FMT_ASSERT(ptr_ != end_, "");
|
FMT_ASSERT(ptr_ != end_, "");
|
||||||
++ptr_;
|
++ptr_;
|
||||||
if (ptr_ == end_) {
|
if (ptr_ != end_) return true;
|
||||||
// TODO: refill buffer
|
consume();
|
||||||
return false;
|
return ptr_ != end_;
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class string_scan_buffer : public scan_buffer {
|
class string_scan_buffer : public scan_buffer {
|
||||||
private:
|
private:
|
||||||
void fill() override {}
|
void consume() override {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit string_scan_buffer(string_view s)
|
explicit string_scan_buffer(string_view s)
|
||||||
@@ -142,37 +140,55 @@ class file_scan_buffer : public scan_buffer {
|
|||||||
private:
|
private:
|
||||||
FILE* file_;
|
FILE* file_;
|
||||||
char next_;
|
char next_;
|
||||||
|
bool filled_ = false;
|
||||||
|
|
||||||
|
// Returns the file's read buffer as a string_view.
|
||||||
template <typename F, FMT_ENABLE_IF(sizeof(F::_p) != 0)>
|
template <typename F, FMT_ENABLE_IF(sizeof(F::_p) != 0)>
|
||||||
void set_buffer(int, F* f) {
|
auto get_buffer(F* file) -> string_view { // Apple libc
|
||||||
const char* ptr = reinterpret_cast<const char*>(f->_p);
|
char* ptr = reinterpret_cast<char*>(file->_p);
|
||||||
this->set(ptr, ptr + f->_r);
|
return {ptr, to_unsigned(file->_r)};
|
||||||
}
|
}
|
||||||
void set_buffer(int c, ...) {
|
auto get_buffer(...) -> string_view {
|
||||||
if (c == EOF) return;
|
return {&next_, (filled_ ? 1u : 0u)};
|
||||||
next_ = static_cast<char>(c);
|
|
||||||
this->set(&next_, &next_ + 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void fill() override {
|
void do_fill() {
|
||||||
int result = getc(file_);
|
string_view buf = get_buffer(file_);
|
||||||
if (result == EOF) {
|
if (buf.size() == 0) {
|
||||||
if (ferror(file_) != 0)
|
int result = getc(file_);
|
||||||
FMT_THROW(system_error(errno, FMT_STRING("I/O error")));
|
if (result != EOF) {
|
||||||
return;
|
// Put the character back since we are only filling the buffer.
|
||||||
|
if (ungetc(result, file_) == EOF)
|
||||||
|
FMT_THROW(system_error(errno, FMT_STRING("I/O error")));
|
||||||
|
next_ = static_cast<char>(result);
|
||||||
|
filled_ = true;
|
||||||
|
} else {
|
||||||
|
if (ferror(file_) != 0)
|
||||||
|
FMT_THROW(system_error(errno, FMT_STRING("I/O error")));
|
||||||
|
filled_ = false;
|
||||||
|
}
|
||||||
|
buf = get_buffer(file_);
|
||||||
}
|
}
|
||||||
// Put the character back since we are only filling the buffer.
|
this->set(buf.begin(), buf.end());
|
||||||
if (ungetc(result, file_) == EOF)
|
}
|
||||||
FMT_THROW(system_error(errno, FMT_STRING("I/O error")));
|
|
||||||
set_buffer(result, file_);
|
void consume() override {
|
||||||
|
// Consume the current buffer content.
|
||||||
|
string_view buf = get_buffer(file_);
|
||||||
|
for (size_t i = 0, n = buf.size(); i != n; ++i) {
|
||||||
|
int result = getc(file_);
|
||||||
|
if (result == EOF && ferror(file_) != 0)
|
||||||
|
FMT_THROW(system_error(errno, FMT_STRING("I/O error")));
|
||||||
|
}
|
||||||
|
filled_ = false;
|
||||||
|
do_fill();
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit file_scan_buffer(FILE* f)
|
explicit file_scan_buffer(FILE* f)
|
||||||
: scan_buffer(nullptr, nullptr, false), file_(f) {
|
: scan_buffer(nullptr, nullptr, false), file_(f) {
|
||||||
// TODO: lock file?
|
// TODO: lock file?
|
||||||
set_buffer(EOF, f);
|
do_fill();
|
||||||
if (is_empty()) fill();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
@@ -303,12 +319,14 @@ struct scan_handler : error_handler {
|
|||||||
template <typename T = unsigned> auto read_uint() -> T {
|
template <typename T = unsigned> auto read_uint() -> T {
|
||||||
T value = 0;
|
T value = 0;
|
||||||
auto it = scan_ctx_.begin(), end = scan_ctx_.end();
|
auto it = scan_ctx_.begin(), end = scan_ctx_.end();
|
||||||
while (it != end) {
|
char c = it != end ? *it : '\0';
|
||||||
char c = *it++;
|
if (c < '0' || c > '9') on_error("invalid input");
|
||||||
if (c < '0' || c > '9') on_error("invalid input");
|
do {
|
||||||
// TODO: check overflow
|
|
||||||
value = value * 10 + static_cast<unsigned>(c - '0');
|
value = value * 10 + static_cast<unsigned>(c - '0');
|
||||||
}
|
c = *++it;
|
||||||
|
if (c < '0' || c > '9') break;
|
||||||
|
// TODO: check overflow
|
||||||
|
} while (it != end);
|
||||||
scan_ctx_.advance_to(it);
|
scan_ctx_.advance_to(it);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@@ -316,11 +334,13 @@ struct scan_handler : error_handler {
|
|||||||
template <typename T = int> auto read_int() -> T {
|
template <typename T = int> auto read_int() -> T {
|
||||||
auto it = scan_ctx_.begin(), end = scan_ctx_.end();
|
auto it = scan_ctx_.begin(), end = scan_ctx_.end();
|
||||||
bool negative = it != end && *it == '-';
|
bool negative = it != end && *it == '-';
|
||||||
if (negative) ++it;
|
if (negative) {
|
||||||
scan_ctx_.advance_to(it);
|
++it;
|
||||||
const auto value = read_uint<typename std::make_unsigned<T>::type>();
|
scan_ctx_.advance_to(it);
|
||||||
if (negative) return -static_cast<T>(value);
|
}
|
||||||
return static_cast<T>(value);
|
auto abs_value = read_uint<typename std::make_unsigned<T>::type>();
|
||||||
|
auto value = static_cast<T>(abs_value);
|
||||||
|
return negative ? -value : value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Reference in New Issue
Block a user