Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/system/ParamNode.cpp
/* Copyright (C) 2021 Wildfire Games. | /* Copyright (C) 2022 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 13 Lines | |||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/CStr.h" | #include "ps/CStr.h" | ||||
#include "ps/CStrIntern.h" | #include "ps/CStrIntern.h" | ||||
#include "ps/Filesystem.h" | #include "ps/Filesystem.h" | ||||
#include "ps/XML/Xeromyces.h" | #include "ps/XML/Xeromyces.h" | ||||
#include "scriptinterface/ScriptRequest.h" | #include "scriptinterface/ScriptRequest.h" | ||||
#include <sstream> | #include <sstream> | ||||
#include <string_view> | |||||
#include <boost/algorithm/string.hpp> | #include <boost/algorithm/string.hpp> | ||||
static CParamNode g_NullNode(false); | static CParamNode g_NullNode(false); | ||||
CParamNode::CParamNode(bool isOk) : | CParamNode::CParamNode(bool isOk) : | ||||
m_IsOk(isOk) | m_IsOk(isOk) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | XERO_ITER_ATTR(element, attr) | ||||
std::vector<std::string> newTokens; | std::vector<std::string> newTokens; | ||||
if (!replacing && !node.m_Value.empty()) // ignore the old tokens if replace="" was given | if (!replacing && !node.m_Value.empty()) // ignore the old tokens if replace="" was given | ||||
boost::algorithm::split(oldTokens, node.m_Value, boost::algorithm::is_space(), boost::algorithm::token_compress_on); | boost::algorithm::split(oldTokens, node.m_Value, boost::algorithm::is_space(), boost::algorithm::token_compress_on); | ||||
if (!value.empty()) | if (!value.empty()) | ||||
boost::algorithm::split(newTokens, value, boost::algorithm::is_space(), boost::algorithm::token_compress_on); | boost::algorithm::split(newTokens, value, boost::algorithm::is_space(), boost::algorithm::token_compress_on); | ||||
// Merge the two lists | // Merge the two lists | ||||
std::vector<std::string> tokens = oldTokens; | std::vector<std::string> tokens = oldTokens; | ||||
for (size_t i = 0; i < newTokens.size(); ++i) | for (const std::string& newToken : newTokens) | ||||
Stan: Is this loop the most optimized? Maybe something in algorithm? | |||||
Done Inline Actionsfor big ranges first partition the range on newToken[0] == '-' and then iterating over it would be more efficient. phosit: for big ranges first partition the range on `newToken[0] == '-'` and then iterating over it… | |||||
{ | { | ||||
if (newTokens[i][0] == '-') | if (newToken[0] == '-') | ||||
{ | { | ||||
std::vector<std::string>::iterator tokenIt = std::find(tokens.begin(), tokens.end(), newTokens[i].substr(1)); | std::vector<std::string>::iterator tokenIt = | ||||
std::find(tokens.begin(), tokens.end(), | |||||
std::string_view{newToken}.substr(1)); | |||||
vladislavbelovUnsubmitted Done Inline ActionsWrong indentation. vladislavbelov: Wrong indentation. | |||||
if (tokenIt != tokens.end()) | if (tokenIt != tokens.end()) | ||||
tokens.erase(tokenIt); | tokens.erase(tokenIt); | ||||
else | else | ||||
LOGWARNING("[ParamNode] Could not remove token '%s' from node '%s'%s; not present in list nor inherited (possible typo?)", | LOGWARNING("[ParamNode] Could not remove token " | ||||
newTokens[i].substr(1), name, sourceIdentifier ? (" in '" + utf8_from_wstring(sourceIdentifier) + "'").c_str() : ""); | "'%s' from node '%s'%s; not present in " | ||||
"list nor inherited (possible typo?)", | |||||
std::string_view{newToken}.substr(1), name, | |||||
sourceIdentifier ? | |||||
(" in '" + utf8_from_wstring( | |||||
Not Done Inline ActionsA bit incorrect indentation. vladislavbelov: A bit incorrect indentation. | |||||
sourceIdentifier) + "'").c_str() : ""); | |||||
vladislavbelovUnsubmitted Done Inline ActionsI'd suggest to use a variable, because it doesn't seem more readable. vladislavbelov: I'd suggest to use a variable, because it doesn't seem more readable. | |||||
} | } | ||||
else | else | ||||
{ | { | ||||
if (std::find(oldTokens.begin(), oldTokens.end(), newTokens[i]) == oldTokens.end()) | if (std::find(oldTokens.begin(), oldTokens.end(), newToken) == oldTokens.end()) | ||||
tokens.push_back(newTokens[i]); | tokens.push_back(newToken); | ||||
} | } | ||||
} | } | ||||
node.m_Value = boost::algorithm::join(tokens, " "); | node.m_Value = boost::algorithm::join(tokens, " "); | ||||
hasSetValue = true; | hasSetValue = true; | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 268 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Is this loop the most optimized? Maybe something in algorithm?