| 123456789101112131415161718192021222324252627282930313233343536373839 |
- #include "input.h"
- #include <fstream>
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <optional>
- std::string ReadFile(const std::string &filepath) {
- std::ifstream file(filepath, std::ios::binary | std::ios::ate);
- std::streamsize size = file.tellg();
- file.seekg(0, std::ios::beg);
- std::string buffer(size, '\0');
- file.read(buffer.data(), size);
- return buffer;
- }
- std::optional<std::string_view> Lines::Next() {
- if (text_.empty()) {
- return std::nullopt;
- }
- for (size_t i = 0; i < text_.size(); ++i) {
- if (text_[i] == '\n') {
- std::string_view result(text_.data(), i);
- if (text_.size() > i + 1) {
- text_ = std::string_view(text_.data() + i + 1, text_.size() - i - 1);
- } else {
- text_ = std::string_view();
- }
- return result;
- }
- }
- std::string_view last_result = text_;
- text_ = std::string_view();
- return last_result;
- }
|