From 2ef801f5d0f292aa898ff6f68ab77127e2ea21ae Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Sun, 4 Dec 2022 19:56:12 +0000 Subject: [PATCH] Remove some unnecessary string copy related to substr. Patch By: phosit Differential Revision: https://code.wildfiregames.com/D4772 This was SVN commit r27271. --- source/gui/ObjectBases/IGUIObject.cpp | 5 ++-- source/gui/ObjectTypes/CInput.cpp | 4 +-- .../gui/Scripting/JSInterface_GUIProxy_impl.h | 5 ++-- source/gui/SettingTypes/MouseEventMask.cpp | 9 ++++--- source/lib/sysdep/os/win/wsysdep.cpp | 11 +++++--- source/ps/CConsole.cpp | 6 +++-- source/ps/TemplateLoader.cpp | 2 +- source/ps/XMB/XMBStorage.cpp | 4 ++- source/ps/tests/test_CLogger.h | 4 +-- source/renderer/PostprocManager.cpp | 7 +++-- source/simulation2/helpers/Selection.cpp | 10 ++++--- .../simulation2/system/ComponentManager.cpp | 19 +++++++------ source/simulation2/system/ParamNode.cpp | 27 +++++++++++++------ 13 files changed, 73 insertions(+), 40 deletions(-) diff --git a/source/gui/ObjectBases/IGUIObject.cpp b/source/gui/ObjectBases/IGUIObject.cpp index be2d2d1713..0b3658ff93 100644 --- a/source/gui/ObjectBases/IGUIObject.cpp +++ b/source/gui/ObjectBases/IGUIObject.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -33,6 +33,7 @@ #include "soundmanager/ISoundManager.h" #include +#include #include const CStr IGUIObject::EventNameMouseEnter = "MouseEnter"; @@ -461,7 +462,7 @@ CStr IGUIObject::GetPresentableName() const if (m_Name.length() <= 12) return m_Name; - if (m_Name.substr(0, 10) == "__internal") + if (std::string_view{m_Name}.substr(0, 10) == "__internal") return CStr("[unnamed object]"); else return m_Name; diff --git a/source/gui/ObjectTypes/CInput.cpp b/source/gui/ObjectTypes/CInput.cpp index f543e36085..616c1c4d98 100644 --- a/source/gui/ObjectTypes/CInput.cpp +++ b/source/gui/ObjectTypes/CInput.cpp @@ -635,7 +635,7 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev) // Check max length if (m_MaxLength != 0 && caption.length() + text.length() > static_cast(m_MaxLength)) - text = text.substr(0, static_cast(m_MaxLength) - caption.length()); + text.erase(static_cast(m_MaxLength) - caption.length()); if (SelectingText()) DeleteCurSelection(); @@ -1552,7 +1552,7 @@ void CInput::UpdateText(int from, int to_before, int to_after) CStrW& caption = m_Caption.GetMutable(); if (m_MaxLength != 0 && caption.length() > static_cast(m_MaxLength)) - caption = caption.substr(0, m_MaxLength); + caption.erase(m_MaxLength); CStrIntern font_name(m_Font->ToUTF8()); diff --git a/source/gui/Scripting/JSInterface_GUIProxy_impl.h b/source/gui/Scripting/JSInterface_GUIProxy_impl.h index e997868dc2..8cb5bf33e6 100644 --- a/source/gui/Scripting/JSInterface_GUIProxy_impl.h +++ b/source/gui/Scripting/JSInterface_GUIProxy_impl.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -28,6 +28,7 @@ #include "scriptinterface/ScriptRequest.h" #include +#include template JSI_GUIProxy& JSI_GUIProxy::Singleton() @@ -307,7 +308,7 @@ bool JSI_GUIProxy::delete_(JSContext* cx, JS::HandleObject proxy, JS::HandleI return result.fail(JSMSG_BAD_PROP_ID); // event handlers - if (propName.substr(0, 2) == "on") + if (std::string_view{propName}.substr(0, 2) == "on") { CStr eventName(propName.substr(2)); e->UnsetScriptHandler(eventName); diff --git a/source/gui/SettingTypes/MouseEventMask.cpp b/source/gui/SettingTypes/MouseEventMask.cpp index 53b52d7ac2..6485a02ebc 100644 --- a/source/gui/SettingTypes/MouseEventMask.cpp +++ b/source/gui/SettingTypes/MouseEventMask.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -29,6 +29,8 @@ #include "ps/CStr.h" #include "scriptinterface/ScriptConversions.h" +#include + class IGUIObject; class IGUISetting; @@ -83,11 +85,12 @@ public: static constexpr std::string_view identifier = "texture:"; static constexpr size_t specOffset = identifier.size(); - static std::unique_ptr Create(const std::string& spec) + static std::unique_ptr Create(const std::string_view spec) { std::shared_ptr shapeFile; CCacheLoader loader(g_VFS, L".dds"); - VfsPath sourcePath = VfsPath("art") / L"textures" / L"ui" / spec.substr(specOffset); + VfsPath sourcePath = VfsPath("art") / L"textures" / L"ui" / + std::string{spec.substr(specOffset)}; VfsPath archivePath = loader.ArchiveCachePath(sourcePath); Status status; size_t size; diff --git a/source/lib/sysdep/os/win/wsysdep.cpp b/source/lib/sysdep/os/win/wsysdep.cpp index fa0f0e841d..fca5253b7a 100644 --- a/source/lib/sysdep/os/win/wsysdep.cpp +++ b/source/lib/sysdep/os/win/wsysdep.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -43,6 +43,8 @@ # include #endif +#include + #if MSC_VERSION #pragma comment(lib, "shell32.lib") // for sys_pick_directory SH* calls @@ -538,9 +540,10 @@ static std::wstring parse_proxy(const std::wstring& input) std::vector parts; split(parts, input, boost::algorithm::is_any_of("; \t\r\n"), boost::algorithm::token_compress_on); - for(size_t i = 0; i < parts.size(); ++i) - if(boost::algorithm::starts_with(parts[i], "http=")) - return parts[i].substr(5); + constexpr std::wstring_view http{L"http="}; + for(const std::wstring& part : parts) + if(std::wstring_view{part}.substr(0, http.size()) == http) + return part.substr(http.size()); // If we got this far, proxies were only set for non-HTTP protocols return L""; diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp index 4df1624b17..4d01bac426 100644 --- a/source/ps/CConsole.cpp +++ b/source/ps/CConsole.cpp @@ -41,6 +41,7 @@ #include "scriptinterface/ScriptInterface.h" #include "scriptinterface/JSON.h" +#include #include #include @@ -548,7 +549,8 @@ void CConsole::InsertMessage(const std::string& message) m_MsgHistory.push_front(wrapAround.substr(oldNewline, distance)); oldNewline += distance+1; } - m_MsgHistory.push_front(wrapAround.substr(oldNewline)); + wrapAround.erase(0, oldNewline); + m_MsgHistory.push_front(std::move(wrapAround)); } } @@ -615,7 +617,7 @@ void CConsole::LoadHistory() { if (pos > 0) m_BufHistory.push_front(str.Left(str[pos-1] == '\r' ? pos - 1 : pos)); - str = str.substr(pos + 1); + str.erase(0, pos + 1); } else if (str.length() > 0) m_BufHistory.push_front(str); diff --git a/source/ps/TemplateLoader.cpp b/source/ps/TemplateLoader.cpp index c3fdaf96c3..cba5527a0a 100644 --- a/source/ps/TemplateLoader.cpp +++ b/source/ps/TemplateLoader.cpp @@ -40,7 +40,7 @@ bool CTemplateLoader::LoadTemplateFile(CParamNode& node, std::string_view templa // Handle infinite loops more gracefully than running out of stack space and crashing if (depth > 100) { - LOGERROR("Probable infinite inheritance loop in entity template '%s'", std::string(templateName)); + LOGERROR("Probable infinite inheritance loop in entity template '%s'", templateName); return false; } diff --git a/source/ps/XMB/XMBStorage.cpp b/source/ps/XMB/XMBStorage.cpp index a8a24bdd50..6d24fa2e74 100644 --- a/source/ps/XMB/XMBStorage.cpp +++ b/source/ps/XMB/XMBStorage.cpp @@ -28,6 +28,7 @@ #include "scriptinterface/ScriptInterface.h" #include +#include #include const char* XMBStorage::HeaderMagicStr = "XMB0"; @@ -235,7 +236,8 @@ bool JSNodeData::Setup(XMBStorageWriter& xmb, JS::HandleValue value) std::string_view name = prop; if (!attrib && !prop.empty() && prop.back() == '@') { - size_t idx = prop.substr(0, prop.size()-1).find_last_of('@'); + const size_t idx = std::string_view{prop}.substr(0, prop.size() - 1) + .find_last_of('@'); if (idx == std::string::npos) { LOGERROR("Object key name cannot end with an '@' unless it is an index specifier."); diff --git a/source/ps/tests/test_CLogger.h b/source/ps/tests/test_CLogger.h index e90e0e7868..78785eb717 100644 --- a/source/ps/tests/test_CLogger.h +++ b/source/ps/tests/test_CLogger.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2009 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -85,7 +85,7 @@ public: std::string s = mainlog->str(); size_t start = s.find(header_end); TS_ASSERT_DIFFERS(start, s.npos); - s = s.substr(start + header_end.length()); + s.erase(0, start + header_end.length()); size_t n = 0, m; while (s.npos != (m = s.find('\n', n))) diff --git a/source/renderer/PostprocManager.cpp b/source/renderer/PostprocManager.cpp index db96124520..cf39f62b08 100644 --- a/source/renderer/PostprocManager.cpp +++ b/source/renderer/PostprocManager.cpp @@ -36,6 +36,8 @@ #include "renderer/RenderingOptions.h" #include "tools/atlas/GameInterface/GameLoop.h" +#include + CPostprocManager::CPostprocManager() : m_IsInitialized(false), m_PostProcEffect(L"default"), m_WhichBuffer(true), m_Sharpness(0.3f), m_UsingMultisampleBuffer(false), m_MultisampleCount(0) @@ -617,12 +619,13 @@ void CPostprocManager::UpdateAntiAliasingTechnique() // We have to hardcode names in the engine, because anti-aliasing // techinques strongly depend on the graphics pipeline. // We might use enums in future though. - const CStr msaaPrefix = "msaa"; + constexpr std::string_view msaaPrefix{"msaa"}; if (m_AAName == "fxaa") { m_AATech = g_Renderer.GetShaderManager().LoadEffect(CStrIntern("fxaa")); } - else if (m_AAName.size() > msaaPrefix.size() && m_AAName.substr(0, msaaPrefix.size()) == msaaPrefix) + else if (m_AAName.size() > msaaPrefix.size() && + std::string_view{m_AAName}.substr(0, msaaPrefix.size()) == msaaPrefix) { // We don't want to enable MSAA in Atlas, because it uses wxWidgets and its canvas. if (g_AtlasGameLoop && g_AtlasGameLoop->running) diff --git a/source/simulation2/helpers/Selection.cpp b/source/simulation2/helpers/Selection.cpp index 5b6b3f52b1..c5f25c2858 100644 --- a/source/simulation2/helpers/Selection.cpp +++ b/source/simulation2/helpers/Selection.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -31,6 +31,8 @@ #include "simulation2/components/ICmpUnitRenderer.h" #include "simulation2/system/ComponentManager.h" +#include + entity_id_t EntitySelection::PickEntityAtPoint(CSimulation2& simulation, const CCamera& camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables) { PROFILE2("PickEntityAtPoint"); @@ -215,9 +217,11 @@ std::vector EntitySelection::PickSimilarEntities(CSimulation2& simu if (matchRank) { // Exact template name matching, optionally also allowing foundations - std::string curTemplateName = cmpTemplateManager->GetCurrentTemplateName(ent); + const std::string curTemplateName = cmpTemplateManager->GetCurrentTemplateName(ent); bool matches = (curTemplateName == templateName || - (allowFoundations && curTemplateName.substr(0, 11) == "foundation|" && curTemplateName.substr(11) == templateName)); + (allowFoundations && + std::string_view{curTemplateName}.substr(0, 11) == "foundation|" && + std::string_view{curTemplateName}.substr(11) == templateName)); if (!matches) continue; } diff --git a/source/simulation2/system/ComponentManager.cpp b/source/simulation2/system/ComponentManager.cpp index 80e17802cc..81d316f254 100644 --- a/source/simulation2/system/ComponentManager.cpp +++ b/source/simulation2/system/ComponentManager.cpp @@ -32,6 +32,8 @@ #include "simulation2/system/ParamNode.h" #include "simulation2/system/SimContext.h" +#include + /** * Used for script-only message types. */ @@ -273,26 +275,27 @@ void CComponentManager::Script_RegisterComponentType_Common(int iid, const std:: return; } - for (std::vector::const_iterator it = methods.begin(); it != methods.end(); ++it) + for (const std::string& method : methods) { - // TODO C++17: string_view - if (strncmp((it->c_str()), "On", 2) != 0) + if (std::string_view{method}.substr(0, 2) != "On") continue; - std::string name = (*it).substr(2); // strip the "On" prefix + std::string_view name{std::string_view{method}.substr(2)}; // strip the "On" prefix // Handle "OnGlobalFoo" functions specially bool isGlobal = false; - if (strncmp(name.c_str(), "Global", 6) == 0) + if (std::string_view{name}.substr(0, 6) == "Global") { isGlobal = true; - name = name.substr(6); + name.remove_prefix(6); } - std::map::const_iterator mit = m_MessageTypeIdsByName.find(name); + auto mit = m_MessageTypeIdsByName.find(std::string{name}); if (mit == m_MessageTypeIdsByName.end()) { - ScriptException::Raise(rq, "Registered component has unrecognized '%s' message handler method", it->c_str()); + ScriptException::Raise(rq, + "Registered component has unrecognized '%s' message handler method", + method.c_str()); return; } diff --git a/source/simulation2/system/ParamNode.cpp b/source/simulation2/system/ParamNode.cpp index 1a834dc5f4..84163ed23e 100644 --- a/source/simulation2/system/ParamNode.cpp +++ b/source/simulation2/system/ParamNode.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -28,6 +28,7 @@ #include "scriptinterface/ScriptRequest.h" #include +#include #include @@ -143,21 +144,31 @@ void CParamNode::ApplyLayer(const XMBData& xmb, const XMBElement& element, const // Merge the two lists std::vector tokens = oldTokens; - for (size_t i = 0; i < newTokens.size(); ++i) + for (const std::string& newToken : newTokens) { - if (newTokens[i][0] == '-') + if (newToken[0] == '-') { - std::vector::iterator tokenIt = std::find(tokens.begin(), tokens.end(), newTokens[i].substr(1)); + std::vector::iterator tokenIt = + std::find(tokens.begin(), tokens.end(), + std::string_view{newToken}.substr(1)); if (tokenIt != tokens.end()) tokens.erase(tokenIt); else - LOGWARNING("[ParamNode] Could not remove token '%s' from node '%s'%s; not present in list nor inherited (possible typo?)", - newTokens[i].substr(1), name, sourceIdentifier ? (" in '" + utf8_from_wstring(sourceIdentifier) + "'").c_str() : ""); + { + const std::string identifier{ + sourceIdentifier ? (" in '" + + utf8_from_wstring(sourceIdentifier) + "'") : ""}; + LOGWARNING("[ParamNode] Could not remove token " + "'%s' from node '%s'%s; not present in " + "list nor inherited (possible typo?)", + std::string_view{newToken}.substr(1), name, + identifier); + } } else { - if (std::find(oldTokens.begin(), oldTokens.end(), newTokens[i]) == oldTokens.end()) - tokens.push_back(newTokens[i]); + if (std::find(oldTokens.begin(), oldTokens.end(), newToken) == oldTokens.end()) + tokens.push_back(newToken); } }