diff options
author | HampusM <hampus@hampusmat.com> | 2022-07-01 16:01:04 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-07-01 16:01:04 +0200 |
commit | 7307815e99a79dac42f2a9c06b0fe6171ea11ba0 (patch) | |
tree | ad41ee819dc87fc2653caf720fa7d1df30c0caeb /src/game/RLE_reader.cpp | |
parent | 2bff8c999edde11270ecaf6fbd2d24f54d0e360b (diff) |
refactor: use ranges
Diffstat (limited to 'src/game/RLE_reader.cpp')
-rw-r--r-- | src/game/RLE_reader.cpp | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/src/game/RLE_reader.cpp b/src/game/RLE_reader.cpp index df2823c..79890f1 100644 --- a/src/game/RLE_reader.cpp +++ b/src/game/RLE_reader.cpp @@ -1,14 +1,19 @@ #include "RLE_reader.hpp" -#include <ctre.hpp> #include <cctype> +#include <ctre.hpp> +#include <range/v3/algorithm/find_if.hpp> +#include <range/v3/algorithm/fold_left.hpp> +#include <range/v3/range/conversion.hpp> +#include <range/v3/view/drop.hpp> +#include <range/v3/view/iota.hpp> +#include <range/v3/view/join.hpp> #include <string> #include <vector> #include "engine/data/bounds.hpp" #include "engine/data/vector2.hpp" #include "errors/RLE_reader.hpp" -#include "util/algorithm_impl.hpp" #include "util/io_impl.hpp" #include "util/string_impl.hpp" @@ -32,15 +37,14 @@ auto RLEReader::read_RLE_file(const std::filesystem::path &path) const content_lines.pop_back(); } - const auto header_line_iter = container_find( + const auto header_line_iter = ranges::find_if( content_lines, [](const std::string &line) { return ctre::starts_with<"x = \\d+, y = \\d+">(line); - // return ctre::match<"x = \\d+, y = \\d+(, rule = [a-zA-Z0-9/]+)?">(line); }); - if (header_line_iter == content_lines.end()) + if (header_line_iter == ranges::end(content_lines)) { throw InvalidRLEFileError(path, "No header line"); } @@ -64,26 +68,28 @@ auto RLEReader::read_RLE_file(const std::filesystem::path &path) const auto pattern_pos = Vector2({.x = 0, .y = 0}); - const auto first_pattern_line_iter = header_line_iter + 1; + const auto header_line_index = + static_cast<int32_t>(header_line_iter - content_lines.begin()); - for (auto pattern_line_iter = first_pattern_line_iter; - pattern_line_iter != content_lines.end(); - ++pattern_line_iter) + const auto pattern_lines = content_lines | + ranges::views::drop(header_line_index + 1) | + ranges::to<std::vector>(); + + for (const auto &pattern_line : pattern_lines) { - if (!ctre::match<"[bo$0-9!]+">(*pattern_line_iter)) + if (!ctre::match<"[bo$0-9!]+">(pattern_line)) { throw InvalidRLEFileError(path, "Invalid pattern line"); } } - auto pattern = std::string(); - - for (auto pattern_line_iter = first_pattern_line_iter; - pattern_line_iter != content_lines.end(); - ++pattern_line_iter) - { - pattern.append(*pattern_line_iter); - } + auto pattern = ranges::fold_left( + pattern_lines, + std::string(), + [](const std::string &acc, const std::string &line) + { + return acc + line; + }); auto run_count_str = std::string(); @@ -111,7 +117,7 @@ auto RLEReader::read_RLE_file(const std::filesystem::path &path) const continue; } - for (auto run_index = 0; run_index < run_count; run_index++) + for (const auto &run_index : ranges::views::iota(0, run_count)) { if (pattern_size.validate_coords(pattern_pos) != CoordsValidation::VALID) { |