From 7307815e99a79dac42f2a9c06b0fe6171ea11ba0 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 1 Jul 2022 16:01:04 +0200 Subject: refactor: use ranges --- src/game/RLE_reader.cpp | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) (limited to 'src/game/RLE_reader.cpp') 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 #include +#include +#include +#include +#include +#include +#include +#include #include #include #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(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(); + + 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) { -- cgit v1.2.3-18-g5258