Index: source/gui/ObjectBases/IGUIObject.cpp =================================================================== --- source/gui/ObjectBases/IGUIObject.cpp +++ 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 @@ 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; Index: source/gui/ObjectTypes/CInput.cpp =================================================================== --- source/gui/ObjectTypes/CInput.cpp +++ source/gui/ObjectTypes/CInput.cpp @@ -635,7 +635,7 @@ // 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 @@ 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()); Index: source/gui/Scripting/JSInterface_GUIProxy_impl.h =================================================================== --- source/gui/Scripting/JSInterface_GUIProxy_impl.h +++ 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 @@ 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); Index: source/gui/SettingTypes/MouseEventMask.cpp =================================================================== --- source/gui/SettingTypes/MouseEventMask.cpp +++ 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 @@ 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; Index: source/lib/sysdep/os/win/wsysdep.cpp =================================================================== --- source/lib/sysdep/os/win/wsysdep.cpp +++ 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,9 @@ 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); + for(const std::wstring& part : parts) + if(std::wstring_view{part}.substr(0, 5) == L"http=") + return part.substr(5); // If we got this far, proxies were only set for non-HTTP protocols return L""; Index: source/ps/CConsole.cpp =================================================================== --- source/ps/CConsole.cpp +++ source/ps/CConsole.cpp @@ -41,6 +41,7 @@ #include "scriptinterface/ScriptInterface.h" #include "scriptinterface/JSON.h" +#include #include #include @@ -548,7 +549,7 @@ m_MsgHistory.push_front(wrapAround.substr(oldNewline, distance)); oldNewline += distance+1; } - m_MsgHistory.push_front(wrapAround.substr(oldNewline)); + m_MsgHistory.push_front(std::move(wrapAround).substr(oldNewline)); } } @@ -615,7 +616,7 @@ { if (pos > 0) m_BufHistory.push_front(str.Left(str[pos-1] == '\r' ? pos - 1 : pos)); - str = str.substr(pos + 1); + str.erase(str.begin(), str.begin() + pos + 1); } else if (str.length() > 0) m_BufHistory.push_front(str); Index: source/ps/TemplateLoader.cpp =================================================================== --- source/ps/TemplateLoader.cpp +++ source/ps/TemplateLoader.cpp @@ -40,7 +40,7 @@ // 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; } Index: source/ps/XMB/XMBStorage.cpp =================================================================== --- source/ps/XMB/XMBStorage.cpp +++ 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 @@ 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."); Index: source/ps/tests/test_CLogger.h =================================================================== --- source/ps/tests/test_CLogger.h +++ 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 @@ 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(s.begin(), s.begin() + start + header_end.length()); size_t n = 0, m; while (s.npos != (m = s.find('\n', n))) Index: source/renderer/PostprocManager.cpp =================================================================== --- source/renderer/PostprocManager.cpp +++ 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) @@ -592,12 +594,13 @@ // 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) Index: source/simulation2/helpers/Selection.cpp =================================================================== --- source/simulation2/helpers/Selection.cpp +++ 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"); @@ -217,7 +219,9 @@ // Exact template name matching, optionally also allowing foundations 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; } Index: source/simulation2/system/ComponentManager.cpp =================================================================== --- source/simulation2/system/ComponentManager.cpp +++ 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,26 @@ 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; } Index: source/simulation2/system/ParamNode.cpp =================================================================== --- source/simulation2/system/ParamNode.cpp +++ 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,28 @@ // 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() : ""); + 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, + sourceIdentifier ? + (" in '" + utf8_from_wstring( + sourceIdentifier) + "'").c_str() : ""); } 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); } }