diff --git a/src/engine/internal/engine.cpp b/src/engine/internal/engine.cpp index e1270c60..38b3987c 100644 --- a/src/engine/internal/engine.cpp +++ b/src/engine/internal/engine.cpp @@ -755,24 +755,22 @@ std::shared_ptr Engine::broadcastAt(int index) const int Engine::findBroadcast(const std::string &broadcastName) const { - int i = 0; - for (auto broadcast : m_broadcasts) { - if (broadcast->name() == broadcastName) - return i; - i++; - } - return -1; + auto it = std::find_if(m_broadcasts.begin(), m_broadcasts.end(), [broadcastName](std::shared_ptr broadcast) { return broadcast->name() == broadcastName; }); + + if (it == m_broadcasts.end()) + return -1; + else + return it - m_broadcasts.begin(); } int Engine::findBroadcastById(const std::string &broadcastId) const { - int i = 0; - for (auto broadcast : m_broadcasts) { - if (broadcast->id() == broadcastId) - return i; - i++; - } - return -1; + auto it = std::find_if(m_broadcasts.begin(), m_broadcasts.end(), [broadcastId](std::shared_ptr broadcast) { return broadcast->id() == broadcastId; }); + + if (it == m_broadcasts.end()) + return -1; + else + return it - m_broadcasts.begin(); } void Engine::addGreenFlagScript(std::shared_ptr hatBlock) @@ -856,13 +854,14 @@ Target *Engine::targetAt(int index) const int Engine::findTarget(const std::string &targetName) const { - int i = 0; - for (auto target : m_targets) { - if ((target->isStage() && targetName == "_stage_") || (!target->isStage() && target->name() == targetName)) - return i; - i++; - } - return -1; + auto it = std::find_if(m_targets.begin(), m_targets.end(), [targetName](std::shared_ptr target) { + return ((target->isStage() && targetName == "_stage_") || (!target->isStage() && target->name() == targetName)); + }); + + if (it == m_targets.end()) + return -1; + else + return it - m_targets.begin(); } void Engine::moveSpriteToFront(Sprite *sprite) diff --git a/src/scratch/block.cpp b/src/scratch/block.cpp index 0dea5897..da65155b 100644 --- a/src/scratch/block.cpp +++ b/src/scratch/block.cpp @@ -186,13 +186,12 @@ std::shared_ptr Block::inputAt(int index) const /*! Returns the index of the input with the given name. */ int Block::findInput(const std::string &inputName) const { - int i = 0; - for (auto input : impl->inputs) { - if (input->name() == inputName) - return i; - i++; - } - return -1; + auto it = std::find_if(impl->inputs.begin(), impl->inputs.end(), [inputName](std::shared_ptr input) { return input->name() == inputName; }); + + if (it == impl->inputs.end()) + return -1; + else + return it - impl->inputs.begin(); } /*! Returns the input with the given ID. */ @@ -248,13 +247,12 @@ std::shared_ptr Block::fieldAt(int index) const /*! Returns the index of the field with the given name. */ int Block::findField(const std::string &fieldName) const { - int i = 0; - for (auto field : impl->fields) { - if (field->name() == fieldName) - return i; - i++; - } - return -1; + auto it = std::find_if(impl->fields.begin(), impl->fields.end(), [fieldName](std::shared_ptr field) { return field->name() == fieldName; }); + + if (it == impl->fields.end()) + return -1; + else + return it - impl->fields.begin(); } /*! Returns the index of the field with the given ID. */ diff --git a/src/scratch/target.cpp b/src/scratch/target.cpp index 8d6ca0d3..817f6a2f 100644 --- a/src/scratch/target.cpp +++ b/src/scratch/target.cpp @@ -61,25 +61,23 @@ std::shared_ptr Target::variableAt(int index) const /*! Returns the index of the variable with the given name. */ int Target::findVariable(const std::string &variableName) const { - int i = 0; - for (auto var : impl->variables) { - if (var->name() == variableName) - return i; - i++; - } - return -1; + auto it = std::find_if(impl->variables.begin(), impl->variables.end(), [variableName](std::shared_ptr variable) { return variable->name() == variableName; }); + + if (it == impl->variables.end()) + return -1; + else + return it - impl->variables.begin(); } /*! Returns the index of the variable with the given ID. */ int Target::findVariableById(const std::string &id) const { - int i = 0; - for (auto var : impl->variables) { - if (var->id() == id) - return i; - i++; - } - return -1; + auto it = std::find_if(impl->variables.begin(), impl->variables.end(), [id](std::shared_ptr variable) { return variable->id() == id; }); + + if (it == impl->variables.end()) + return -1; + else + return it - impl->variables.begin(); } /*! Returns the list of Scratch lists. */ @@ -114,25 +112,23 @@ std::shared_ptr Target::listAt(int index) const /*! Returns the index of the list with the given name. */ int Target::findList(const std::string &listName) const { - int i = 0; - for (auto list : impl->lists) { - if (list->name() == listName) - return i; - i++; - } - return -1; + auto it = std::find_if(impl->lists.begin(), impl->lists.end(), [listName](std::shared_ptr list) { return list->name() == listName; }); + + if (it == impl->lists.end()) + return -1; + else + return it - impl->lists.begin(); } /*! Returns the index of the list with the given ID. */ int Target::findListById(const std::string &id) const { - int i = 0; - for (auto list : impl->lists) { - if (list->id() == id) - return i; - i++; - } - return -1; + auto it = std::find_if(impl->lists.begin(), impl->lists.end(), [id](std::shared_ptr list) { return list->id() == id; }); + + if (it == impl->lists.end()) + return -1; + else + return it - impl->lists.begin(); } /*! Returns the list of blocks. */ @@ -177,13 +173,12 @@ int Target::findBlock(const std::string &id) const if (Target *source = dataSource()) return source->findBlock(id); - int i = 0; - for (auto block : impl->blocks) { - if (block->id() == id) - return i; - i++; - } - return -1; + auto it = std::find_if(impl->blocks.begin(), impl->blocks.end(), [id](std::shared_ptr block) { return block->id() == id; }); + + if (it == impl->blocks.end()) + return -1; + else + return it - impl->blocks.begin(); } /*! Returns list of all "when green flag clicked" blocks. */ @@ -242,16 +237,12 @@ int Target::findComment(const std::string &id) const if (Target *source = dataSource()) return source->findComment(id); - int i = 0; + auto it = std::find_if(impl->comments.begin(), impl->comments.end(), [id](std::shared_ptr comment) { return comment->id() == id; }); - for (auto comment : impl->comments) { - if (comment->id() == id) - return i; - - i++; - } - - return -1; + if (it == impl->comments.end()) + return -1; + else + return it - impl->comments.begin(); } /*! Returns the index of the current costume. */ @@ -320,13 +311,12 @@ int Target::findCostume(const std::string &costumeName) const if (Target *source = dataSource()) return source->findCostume(costumeName); - int i = 0; - for (auto costume : impl->costumes) { - if (costume->name() == costumeName) - return i; - i++; - } - return -1; + auto it = std::find_if(impl->costumes.begin(), impl->costumes.end(), [costumeName](std::shared_ptr costume) { return costume->name() == costumeName; }); + + if (it == impl->costumes.end()) + return -1; + else + return it - impl->costumes.begin(); } /*! Returns the list of sounds. */ @@ -376,13 +366,12 @@ int Target::findSound(const std::string &soundName) const if (Target *source = dataSource()) return source->findSound(soundName); - int i = 0; - for (auto sound : impl->sounds) { - if (sound->name() == soundName) - return i; - i++; - } - return -1; + auto it = std::find_if(impl->sounds.begin(), impl->sounds.end(), [soundName](std::shared_ptr sound) { return sound->name() == soundName; }); + + if (it == impl->sounds.end()) + return -1; + else + return it - impl->sounds.begin(); } /*! Returns the layer number. */