fix: bucles cap a ranges algorithms (38 troballes)

This commit is contained in:
2026-05-14 21:36:21 +02:00
parent 0aa9f8fe0a
commit b4d3776239
14 changed files with 110 additions and 169 deletions

View File

@@ -4,6 +4,7 @@
#include <iostream> // Para basic_ostream, operator<<, cout, cerr #include <iostream> // Para basic_ostream, operator<<, cout, cerr
#include <memory> // Para shared_ptr, __shared_ptr_access, allocator, operator==, make_shared #include <memory> // Para shared_ptr, __shared_ptr_access, allocator, operator==, make_shared
#include <algorithm> // Para ranges::any_of
#include <ranges> // Para __find_if_fn, find_if #include <ranges> // Para __find_if_fn, find_if
#include <unordered_map> // Para unordered_map, _Node_iterator, operator==, _Node_iterator_base, _Node_const_iterator #include <unordered_map> // Para unordered_map, _Node_iterator, operator==, _Node_iterator_base, _Node_const_iterator
#include <utility> // Para pair, move #include <utility> // Para pair, move
@@ -182,14 +183,10 @@ auto Input::checkAnyInput(bool check_keyboard, const std::shared_ptr<Gamepad>& g
// Obtenemos el número total de acciones posibles para iterar sobre ellas. // Obtenemos el número total de acciones posibles para iterar sobre ellas.
// --- Comprobación del Teclado --- // --- Comprobación del Teclado ---
if (check_keyboard) { // Llegim l'estat pre-calculat per Input::update() (sense tornar a cridar SDL_GetKeyboardState).
for (const auto& pair : keyboard_.bindings) { if (check_keyboard && std::ranges::any_of(keyboard_.bindings,
// Simplemente leemos el estado pre-calculado por Input::update(). [](const auto& pair) { return pair.second.just_pressed; })) {
// Ya no se llama a SDL_GetKeyboardState ni se modifica el estado '.active'. return true;
if (pair.second.just_pressed) {
return true; // Se encontró una acción recién pulsada.
}
}
} }
// Si gamepad es nullptr pero hay mandos conectados, usar el primero // Si gamepad es nullptr pero hay mandos conectados, usar el primero
@@ -199,15 +196,10 @@ auto Input::checkAnyInput(bool check_keyboard, const std::shared_ptr<Gamepad>& g
} }
// --- Comprobación del Mando --- // --- Comprobación del Mando ---
// Comprobamos si hay mandos y si el índice solicitado es válido. // Iterem sobre totes les accions del mandos pre-calculades per Input::update().
if (active_gamepad != nullptr) { if (active_gamepad != nullptr && std::ranges::any_of(active_gamepad->bindings,
// Iteramos sobre todas las acciones, no sobre el número de mandos. [](const auto& pair) { return pair.second.just_pressed; })) {
for (const auto& pair : active_gamepad->bindings) { return true;
// Leemos el estado pre-calculado para el mando y la acción específicos.
if (pair.second.just_pressed) {
return true; // Se encontró una acción recién pulsada en el mando.
}
}
} }
// Si llegamos hasta aquí, no se detectó ninguna nueva pulsación. // Si llegamos hasta aquí, no se detectó ninguna nueva pulsación.
@@ -216,22 +208,14 @@ auto Input::checkAnyInput(bool check_keyboard, const std::shared_ptr<Gamepad>& g
// Comprueba si hay algún botón pulsado // Comprueba si hay algún botón pulsado
auto Input::checkAnyButton(bool repeat) -> bool { // NOLINT(readability-convert-member-functions-to-static) auto Input::checkAnyButton(bool repeat) -> bool { // NOLINT(readability-convert-member-functions-to-static)
// Solo comprueba los botones definidos previamente return std::ranges::any_of(BUTTON_INPUTS, [&](auto bi) {
for (auto bi : BUTTON_INPUTS) {
// Comprueba el teclado
if (checkAction(bi, repeat, CHECK_KEYBOARD)) { if (checkAction(bi, repeat, CHECK_KEYBOARD)) {
return true; return true;
} }
return std::ranges::any_of(gamepads_, [&](const auto& gamepad) {
// Comprueba los mandos return checkAction(bi, repeat, DO_NOT_CHECK_KEYBOARD, gamepad);
for (const auto& gamepad : gamepads_) { });
if (checkAction(bi, repeat, DO_NOT_CHECK_KEYBOARD, gamepad)) { });
return true;
}
}
}
return false;
} }
// Comprueba si hay algun mando conectado // Comprueba si hay algun mando conectado
@@ -245,9 +229,9 @@ auto Input::getControllerName(const std::shared_ptr<Gamepad>& gamepad) -> std::s
// Obtiene la lista de nombres de mandos // Obtiene la lista de nombres de mandos
auto Input::getControllerNames() const -> std::vector<std::string> { auto Input::getControllerNames() const -> std::vector<std::string> {
std::vector<std::string> names; std::vector<std::string> names;
for (const auto& gamepad : gamepads_) { names.reserve(gamepads_.size());
names.push_back(gamepad->name); std::ranges::transform(gamepads_, std::back_inserter(names),
} [](const auto& gamepad) { return gamepad->name; });
return names; return names;
} }
@@ -256,21 +240,15 @@ auto Input::getNumGamepads() const -> int { return gamepads_.size(); }
// Obtiene el gamepad a partir de un event.id // Obtiene el gamepad a partir de un event.id
auto Input::getGamepad(SDL_JoystickID id) const -> std::shared_ptr<Input::Gamepad> { // NOLINT(readability-convert-member-functions-to-static) auto Input::getGamepad(SDL_JoystickID id) const -> std::shared_ptr<Input::Gamepad> { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& gamepad : gamepads_) { auto it = std::ranges::find_if(gamepads_,
if (gamepad->instance_id == id) { [id](const auto& gamepad) { return gamepad->instance_id == id; });
return gamepad; return (it != gamepads_.end()) ? *it : nullptr;
}
}
return nullptr;
} }
auto Input::getGamepadByName(const std::string& name) const -> std::shared_ptr<Input::Gamepad> { // NOLINT(readability-convert-member-functions-to-static) auto Input::getGamepadByName(const std::string& name) const -> std::shared_ptr<Input::Gamepad> { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& gamepad : gamepads_) { auto it = std::ranges::find_if(gamepads_,
if (gamepad && gamepad->name == name) { [&name](const auto& gamepad) { return gamepad && gamepad->name == name; });
return gamepad; return (it != gamepads_.end()) ? *it : nullptr;
}
}
return nullptr;
} }
// Obtiene el SDL_GamepadButton asignado a un action // Obtiene el SDL_GamepadButton asignado a un action
@@ -511,19 +489,14 @@ auto Input::findAvailableGamepadByName(const std::string& gamepad_name) -> std::
} }
// Buscar por nombre // Buscar por nombre
for (const auto& gamepad : gamepads_) { auto by_name = std::ranges::find_if(gamepads_,
if (gamepad && gamepad->name == gamepad_name) { [&gamepad_name](const auto& gamepad) { return gamepad && gamepad->name == gamepad_name; });
return gamepad; if (by_name != gamepads_.end()) {
} return *by_name;
} }
// Si no se encuentra por nombre, devolver el primer gamepad válido // Si no se encuentra por nombre, devolver el primer gamepad válido
for (const auto& gamepad : gamepads_) { auto first_valid = std::ranges::find_if(gamepads_,
if (gamepad) { [](const auto& gamepad) { return static_cast<bool>(gamepad); });
return gamepad; return (first_valid != gamepads_.end()) ? *first_valid : nullptr;
}
}
// Si llegamos aquí, no hay gamepads válidos
return nullptr;
} }

View File

@@ -120,9 +120,7 @@ namespace {
} }
// Si quedan colores sin asignar, añadirlos al final // Si quedan colores sin asignar, añadirlos al final
for (const auto& c : available) { std::ranges::copy(available, std::back_inserter(result));
result.push_back(c);
}
Palette out{}; Palette out{};
out.fill(0); out.fill(0);

View File

@@ -6,6 +6,7 @@
#include <SDL3/SDL_filesystem.h> #include <SDL3/SDL_filesystem.h>
#include <algorithm> #include <algorithm>
#include <numeric>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@@ -14,11 +15,10 @@ namespace Resource {
// Calculate CRC32 checksum for data verification // Calculate CRC32 checksum for data verification
auto Pack::calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t { // NOLINT(readability-convert-member-functions-to-static) auto Pack::calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t { // NOLINT(readability-convert-member-functions-to-static)
uint32_t checksum = 0x12345678; return std::accumulate(data.begin(), data.end(), uint32_t{0x12345678},
for (unsigned char byte : data) { [](uint32_t acc, unsigned char byte) -> uint32_t {
checksum = ((checksum << 5) + checksum) + byte; return ((acc << 5) + acc) + byte;
} });
return checksum;
} }
// XOR encryption (symmetric - same function for encrypt/decrypt) // XOR encryption (symmetric - same function for encrypt/decrypt)

View File

@@ -1425,11 +1425,11 @@ auto MapEditor::deleteRoom() -> std::string { // NOLINT(readability-function-co
if (target == "0") { if (target == "0") {
// Buscar la primera room que no sea esta // Buscar la primera room que no sea esta
for (const auto& r : Resource::Cache::get()->getRooms()) { const auto& rooms = Resource::Cache::get()->getRooms();
if (r.name != deleted_name) { auto it = std::ranges::find_if(rooms,
target = r.name; [&deleted_name](const auto& r) { return r.name != deleted_name; });
break; if (it != rooms.end()) {
} target = it->name;
} }
} }
if (target == "0") { return "Cannot delete: no other room to navigate to"; } if (target == "0") { return "Cannot delete: no other room to navigate to"; }

View File

@@ -108,32 +108,23 @@ auto CollisionMap::getSlopeHeight(SDL_FPoint p, Tile slope) -> int {
// Comprueba las colisiones con paredes derechas // Comprueba las colisiones con paredes derechas
auto CollisionMap::checkRightSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static) auto CollisionMap::checkRightSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& s : right_walls_) { auto it = std::ranges::find_if(right_walls_,
if (checkCollision(s, rect)) { [&rect](const auto& s) { return checkCollision(s, rect); });
return s.x; return (it != right_walls_.end()) ? it->x : Collision::NONE;
}
}
return Collision::NONE;
} }
// Comprueba las colisiones con paredes izquierdas // Comprueba las colisiones con paredes izquierdas
auto CollisionMap::checkLeftSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static) auto CollisionMap::checkLeftSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& s : left_walls_) { auto it = std::ranges::find_if(left_walls_,
if (checkCollision(s, rect)) { [&rect](const auto& s) { return checkCollision(s, rect); });
return s.x; return (it != left_walls_.end()) ? it->x : Collision::NONE;
}
}
return Collision::NONE;
} }
// Comprueba las colisiones con techos // Comprueba las colisiones con techos
auto CollisionMap::checkTopSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static) auto CollisionMap::checkTopSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& s : top_floors_) { auto it = std::ranges::find_if(top_floors_,
if (checkCollision(s, rect)) { [&rect](const auto& s) { return checkCollision(s, rect); });
return s.y; return (it != top_floors_.end()) ? it->y : Collision::NONE;
}
}
return Collision::NONE;
} }
// Comprueba las colisiones punto con techos // Comprueba las colisiones punto con techos
@@ -145,22 +136,16 @@ auto CollisionMap::checkTopSurfaces(const SDL_FPoint& p) -> bool {
// Comprueba las colisiones con suelos // Comprueba las colisiones con suelos
auto CollisionMap::checkBottomSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static) auto CollisionMap::checkBottomSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& s : bottom_floors_) { auto it = std::ranges::find_if(bottom_floors_,
if (checkCollision(s, rect)) { [&rect](const auto& s) { return checkCollision(s, rect); });
return s.y; return (it != bottom_floors_.end()) ? it->y : Collision::NONE;
}
}
return Collision::NONE;
} }
// Comprueba las colisiones con conveyor belts // Comprueba las colisiones con conveyor belts
auto CollisionMap::checkAutoSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static) auto CollisionMap::checkAutoSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& s : conveyor_belt_floors_) { auto it = std::ranges::find_if(conveyor_belt_floors_,
if (checkCollision(s, rect)) { [&rect](const auto& s) { return checkCollision(s, rect); });
return s.y; return (it != conveyor_belt_floors_.end()) ? it->y : Collision::NONE;
}
}
return Collision::NONE;
} }
// Comprueba las colisiones punto con conveyor belts // Comprueba las colisiones punto con conveyor belts
@@ -208,18 +193,16 @@ auto CollisionMap::checkRightSlopes(const SDL_FPoint& p) -> bool {
// Obtiene puntero a slope en un punto (prioriza left_slopes_ sobre right_slopes_) // Obtiene puntero a slope en un punto (prioriza left_slopes_ sobre right_slopes_)
auto CollisionMap::getSlopeAtPoint(const SDL_FPoint& p) const -> const LineDiagonal* { // NOLINT(readability-convert-member-functions-to-static) auto CollisionMap::getSlopeAtPoint(const SDL_FPoint& p) const -> const LineDiagonal* { // NOLINT(readability-convert-member-functions-to-static)
// Primero busca en rampas izquierdas auto pred = [&p](const auto& slope) { return static_cast<bool>(checkCollision(p, slope)); };
for (const auto& slope : left_slopes_) {
if (checkCollision(p, slope)) { auto left_it = std::ranges::find_if(left_slopes_, pred);
return &slope; if (left_it != left_slopes_.end()) {
} return &(*left_it);
} }
// Luego busca en rampas derechas auto right_it = std::ranges::find_if(right_slopes_, pred);
for (const auto& slope : right_slopes_) { if (right_it != right_slopes_.end()) {
if (checkCollision(p, slope)) { return &(*right_it);
return &slope;
}
} }
// No hay colisión con ninguna slope // No hay colisión con ninguna slope

View File

@@ -44,9 +44,7 @@ auto RoomLoader::flattenTilemap(const std::vector<std::vector<int>>& tilemap_2d)
tilemap_flat.reserve(512); // 16 rows × 32 cols tilemap_flat.reserve(512); // 16 rows × 32 cols
for (const auto& row : tilemap_2d) { for (const auto& row : tilemap_2d) {
for (int tile : row) { std::ranges::copy(row, std::back_inserter(tilemap_flat));
tilemap_flat.push_back(tile);
}
} }
return tilemap_flat; return tilemap_flat;
@@ -135,12 +133,9 @@ void RoomLoader::parseTilemap(const fkyaml::node& yaml, Room::Data& room, const
for (const auto& row_node : tilemap_node) { for (const auto& row_node : tilemap_node) {
std::vector<int> row; std::vector<int> row;
row.reserve(32); row.reserve(32);
std::ranges::transform(row_node, std::back_inserter(row),
for (const auto& tile_node : row_node) { [](const auto& tile_node) { return tile_node.template get_value<int>(); });
row.push_back(tile_node.get_value<int>()); tilemap_2d.push_back(std::move(row));
}
tilemap_2d.push_back(row);
} }
// Convert to 1D flat array // Convert to 1D flat array

View File

@@ -36,9 +36,9 @@ Scoreboard::Scoreboard(std::shared_ptr<Data> data)
// Inicializa el vector de colores // Inicializa el vector de colores
const std::vector<std::string> COLORS = {"blue", "magenta", "green", "cyan", "yellow", "white", "bright_blue", "bright_magenta", "bright_green", "bright_cyan", "bright_yellow", "bright_white"}; const std::vector<std::string> COLORS = {"blue", "magenta", "green", "cyan", "yellow", "white", "bright_blue", "bright_magenta", "bright_green", "bright_cyan", "bright_yellow", "bright_white"};
for (const auto& color : COLORS) { color_.reserve(COLORS.size());
color_.push_back(stringToColor(color)); std::ranges::transform(COLORS, std::back_inserter(color_),
} [](const auto& color) { return stringToColor(color); });
} }
// Pinta el objeto en pantalla // Pinta el objeto en pantalla

View File

@@ -31,9 +31,9 @@ Ending2::Ending2()
// Inicializa el vector de colores // Inicializa el vector de colores
const std::vector<std::string> COLORS = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"}; const std::vector<std::string> COLORS = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"};
for (const auto& color : COLORS) { colors_.reserve(COLORS.size());
colors_.push_back(stringToColor(color)); std::ranges::transform(COLORS, std::back_inserter(colors_),
} [](const auto& color) { return stringToColor(color); });
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK)); // Cambia el color del borde Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK)); // Cambia el color del borde
iniSpriteList(); // Inicializa la lista de sprites iniSpriteList(); // Inicializa la lista de sprites

View File

@@ -3,6 +3,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <cmath> // Para std::sqrt, std::min #include <cmath> // Para std::sqrt, std::min
#include <numeric> // Para std::accumulate
#include <utility> #include <utility>
#include <vector> // Para vector #include <vector> // Para vector
@@ -865,14 +866,9 @@ auto Game::checkEndGame() -> bool {
// Obtiene la cantidad total de items que hay en el mapeado del juego // Obtiene la cantidad total de items que hay en el mapeado del juego
auto Game::getTotalItems() -> int { auto Game::getTotalItems() -> int {
int items = 0; const auto& rooms = Resource::Cache::get()->getRooms();
auto rooms = Resource::Cache::get()->getRooms(); return static_cast<int>(std::accumulate(rooms.begin(), rooms.end(), size_t{0},
[](size_t acc, const auto& room) { return acc + room.room->items.size(); }));
for (const auto& room : rooms) {
items += room.room->items.size();
}
return items;
} }
// Pone el juego en pausa // Pone el juego en pausa

View File

@@ -38,9 +38,9 @@ GameOver::GameOver()
// Inicializa el vector de colores (de brillante a oscuro para fade) // Inicializa el vector de colores (de brillante a oscuro para fade)
const std::vector<std::string> COLORS = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"}; const std::vector<std::string> COLORS = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"};
for (const auto& color : COLORS) { colors_.reserve(COLORS.size());
colors_.push_back(stringToColor(color)); std::ranges::transform(COLORS, std::back_inserter(colors_),
} [](const auto& color) { return stringToColor(color); });
color_ = colors_.back(); // Empieza en black color_ = colors_.back(); // Empieza en black
} }

View File

@@ -259,9 +259,8 @@ void Logo::initColors() { // NOLINT(readability-convert-member-functions-to-sta
static_cast<Uint8>(PaletteColor::CYAN), static_cast<Uint8>(PaletteColor::CYAN),
static_cast<Uint8>(PaletteColor::YELLOW), static_cast<Uint8>(PaletteColor::YELLOW),
static_cast<Uint8>(PaletteColor::BRIGHT_WHITE)}; static_cast<Uint8>(PaletteColor::BRIGHT_WHITE)};
for (const auto& color : COLORS) { color_.reserve(COLORS.size());
color_.push_back(color); std::ranges::copy(COLORS, std::back_inserter(color_));
}
} }
// Crea los sprites de cada linea // Crea los sprites de cada linea

View File

@@ -3,6 +3,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <algorithm> // Para ranges::transform #include <algorithm> // Para ranges::transform
#include <numeric> // Para std::accumulate
#include <cctype> // Para toupper #include <cctype> // Para toupper
#include <sstream> // Para std::istringstream #include <sstream> // Para std::istringstream
#include <string> // Para string #include <string> // Para string
@@ -181,8 +182,8 @@ void Console::update(float delta_time) { // NOLINT(readability-function-cogniti
// Efecto typewriter: revelar letras una a una (solo cuando ACTIVE) // Efecto typewriter: revelar letras una a una (solo cuando ACTIVE)
if (status_ == Status::ACTIVE) { if (status_ == Status::ACTIVE) {
int total_chars = 0; const int total_chars = std::accumulate(msg_lines_.begin(), msg_lines_.end(), 0,
for (const auto& line : msg_lines_) { total_chars += static_cast<int>(line.size()); } [](int acc, const auto& line) { return acc + static_cast<int>(line.size()); });
if (typewriter_chars_ < total_chars) { if (typewriter_chars_ < total_chars) {
typewriter_timer_ += delta_time; typewriter_timer_ += delta_time;
while (typewriter_timer_ >= TYPEWRITER_CHAR_DELAY && typewriter_chars_ < total_chars) { while (typewriter_timer_ >= TYPEWRITER_CHAR_DELAY && typewriter_chars_ < total_chars) {
@@ -337,11 +338,9 @@ void Console::handleEvent(const SDL_Event& event) { // NOLINT(readability-funct
const size_t SPACE_POS = upper.rfind(' '); const size_t SPACE_POS = upper.rfind(' ');
if (SPACE_POS == std::string::npos) { if (SPACE_POS == std::string::npos) {
// Modo comando: ciclar keywords visibles que empiecen por el prefijo // Modo comando: ciclar keywords visibles que empiecen por el prefijo
for (const auto& kw : registry_.getVisibleKeywords()) { const auto KEYWORDS = registry_.getVisibleKeywords();
if (upper.empty() || kw.starts_with(upper)) { std::ranges::copy_if(KEYWORDS, std::back_inserter(tab_matches_),
tab_matches_.emplace_back(kw); [&upper](const auto& kw) { return upper.empty() || kw.starts_with(upper); });
}
}
} else { } else {
const std::string BASE_CMD = upper.substr(0, SPACE_POS); const std::string BASE_CMD = upper.substr(0, SPACE_POS);
const std::string SUB_PREFIX = upper.substr(SPACE_POS + 1); const std::string SUB_PREFIX = upper.substr(SPACE_POS + 1);
@@ -357,7 +356,8 @@ void Console::handleEvent(const SDL_Event& event) { // NOLINT(readability-funct
if (tab_matches_.empty()) { break; } if (tab_matches_.empty()) { break; }
tab_index_ = (tab_index_ + 1) % static_cast<int>(tab_matches_.size()); tab_index_ = (tab_index_ + 1) % static_cast<int>(tab_matches_.size());
std::string result = tab_matches_[static_cast<size_t>(tab_index_)]; std::string result = tab_matches_[static_cast<size_t>(tab_index_)];
for (char& c : result) { c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); } std::ranges::transform(result, result.begin(),
[](char c) { return static_cast<char>(std::tolower(static_cast<unsigned char>(c))); });
input_line_ = result; input_line_ = result;
break; break;
} }
@@ -403,9 +403,8 @@ void Console::processCommand() {
// Typewriter: instantáneo si el comando lo requiere, letra a letra si no // Typewriter: instantáneo si el comando lo requiere, letra a letra si no
if (instant) { if (instant) {
int total = 0; typewriter_chars_ = std::accumulate(msg_lines_.begin(), msg_lines_.end(), 0,
for (const auto& l : msg_lines_) { total += static_cast<int>(l.size()); } [](int acc, const auto& l) { return acc + static_cast<int>(l.size()); });
typewriter_chars_ = total;
} else { } else {
typewriter_chars_ = 0; typewriter_chars_ = 0;
} }

View File

@@ -1012,9 +1012,9 @@ void CommandRegistry::registerHandlers() { // NOLINT(readability-function-cogni
dynamic_providers_["PALETTE"] = []() -> std::vector<std::string> { dynamic_providers_["PALETTE"] = []() -> std::vector<std::string> {
std::vector<std::string> result = {"NEXT", "PREV", "SORT", "DEFAULT"}; std::vector<std::string> result = {"NEXT", "PREV", "SORT", "DEFAULT"};
if (Screen::get() != nullptr) { if (Screen::get() != nullptr) {
for (const auto& name : Screen::get()->getPaletteNames()) { const auto NAMES = Screen::get()->getPaletteNames();
result.push_back(toUpper(name)); std::ranges::transform(NAMES, std::back_inserter(result),
} [](const auto& name) { return toUpper(name); });
} }
return result; return result;
}; };
@@ -1023,10 +1023,11 @@ void CommandRegistry::registerHandlers() { // NOLINT(readability-function-cogni
dynamic_providers_["SHADER PRESET"] = []() -> std::vector<std::string> { dynamic_providers_["SHADER PRESET"] = []() -> std::vector<std::string> {
std::vector<std::string> result = {"NEXT", "PREV"}; std::vector<std::string> result = {"NEXT", "PREV"};
const bool IS_CRTPI = Options::video.shader.current_shader == Rendering::ShaderType::CRTPI; const bool IS_CRTPI = Options::video.shader.current_shader == Rendering::ShaderType::CRTPI;
auto upper_name = [](const auto& p) { return toUpper(p.name); };
if (IS_CRTPI) { if (IS_CRTPI) {
for (const auto& p : Options::crtpi_presets) { result.push_back(toUpper(p.name)); } std::ranges::transform(Options::crtpi_presets, std::back_inserter(result), upper_name);
} else { } else {
for (const auto& p : Options::postfx_presets) { result.push_back(toUpper(p.name)); } std::ranges::transform(Options::postfx_presets, std::back_inserter(result), upper_name);
} }
return result; return result;
}; };
@@ -1119,7 +1120,8 @@ void CommandRegistry::load(const std::string& yaml_path) { // NOLINT(readabilit
if (cat_node.contains("scope")) { if (cat_node.contains("scope")) {
const auto& scope_node = cat_node["scope"]; const auto& scope_node = cat_node["scope"];
if (scope_node.is_sequence()) { if (scope_node.is_sequence()) {
for (const auto& s : scope_node) { cat_scopes.push_back(s.get_value<std::string>()); } std::ranges::transform(scope_node, std::back_inserter(cat_scopes),
[](const auto& s) { return s.template get_value<std::string>(); });
} else { } else {
cat_scopes.push_back(scope_node.get_value<std::string>()); cat_scopes.push_back(scope_node.get_value<std::string>());
} }
@@ -1160,9 +1162,8 @@ void CommandRegistry::load(const std::string& yaml_path) { // NOLINT(readabilit
for (auto it = completions_node.begin(); it != completions_node.end(); ++it) { for (auto it = completions_node.begin(); it != completions_node.end(); ++it) {
auto path = it.key().get_value<std::string>(); auto path = it.key().get_value<std::string>();
std::vector<std::string> opts; std::vector<std::string> opts;
for (const auto& opt : *it) { std::ranges::transform(*it, std::back_inserter(opts),
opts.push_back(opt.get_value<std::string>()); [](const auto& opt) { return opt.template get_value<std::string>(); });
}
def.completions[path] = std::move(opts); def.completions[path] = std::move(opts);
} }
} }
@@ -1181,9 +1182,8 @@ void CommandRegistry::load(const std::string& yaml_path) { // NOLINT(readabilit
for (auto it = extras_completions.begin(); it != extras_completions.end(); ++it) { for (auto it = extras_completions.begin(); it != extras_completions.end(); ++it) {
auto path = it.key().get_value<std::string>(); auto path = it.key().get_value<std::string>();
std::vector<std::string> opts; std::vector<std::string> opts;
for (const auto& opt : *it) { std::ranges::transform(*it, std::back_inserter(opts),
opts.push_back(opt.get_value<std::string>()); [](const auto& opt) { return opt.template get_value<std::string>(); });
}
def.completions[path] = std::move(opts); def.completions[path] = std::move(opts);
} }
} }
@@ -1236,10 +1236,9 @@ void CommandRegistry::load(const std::string& yaml_path) { // NOLINT(readabilit
} }
auto CommandRegistry::findCommand(const std::string& keyword) const -> const CommandDef* { auto CommandRegistry::findCommand(const std::string& keyword) const -> const CommandDef* {
for (const auto& cmd : commands_) { auto it = std::ranges::find_if(commands_,
if (cmd.keyword == keyword) { return &cmd; } [&keyword](const auto& cmd) { return cmd.keyword == keyword; });
} return (it != commands_.end()) ? &(*it) : nullptr;
return nullptr;
} }
auto CommandRegistry::execute(const std::string& keyword, const std::vector<std::string>& args) const -> std::string { auto CommandRegistry::execute(const std::string& keyword, const std::vector<std::string>& args) const -> std::string {

View File

@@ -306,8 +306,7 @@ auto Notifier::getVisibleHeight() const -> int {
auto Notifier::getCodes() -> std::vector<std::string> { auto Notifier::getCodes() -> std::vector<std::string> {
std::vector<std::string> codes; std::vector<std::string> codes;
codes.reserve(notifications_.size()); codes.reserve(notifications_.size());
for (const auto& notification : notifications_) { std::ranges::transform(notifications_, std::back_inserter(codes),
codes.emplace_back(notification.code); [](const auto& notification) { return notification.code; });
}
return codes; return codes;
} }