1
0
forked from 0ad/0ad

Remove some unnecessary string copy related to substr.

Patch By: phosit
Differential Revision: https://code.wildfiregames.com/D4772
This was SVN commit r27271.
This commit is contained in:
Vladislav Belov 2022-12-04 19:56:12 +00:00
parent b2f529c655
commit 2ef801f5d0
13 changed files with 73 additions and 40 deletions

View File

@ -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 <algorithm>
#include <string_view>
#include <unordered_map>
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;

View File

@ -635,7 +635,7 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev)
// Check max length
if (m_MaxLength != 0 && caption.length() + text.length() > static_cast<size_t>(m_MaxLength))
text = text.substr(0, static_cast<size_t>(m_MaxLength) - caption.length());
text.erase(static_cast<size_t>(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<size_t>(m_MaxLength))
caption = caption.substr(0, m_MaxLength);
caption.erase(m_MaxLength);
CStrIntern font_name(m_Font->ToUTF8());

View File

@ -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 <string>
#include <string_view>
template <typename T>
JSI_GUIProxy<T>& JSI_GUIProxy<T>::Singleton()
@ -307,7 +308,7 @@ bool JSI_GUIProxy<T>::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);

View File

@ -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 <string_view>
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<CGUIMouseEventMaskTexture> Create(const std::string& spec)
static std::unique_ptr<CGUIMouseEventMaskTexture> Create(const std::string_view spec)
{
std::shared_ptr<u8> 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;

View File

@ -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 <boost/algorithm/string.hpp>
#endif
#include <string_view>
#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<std::wstring> 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"";

View File

@ -41,6 +41,7 @@
#include "scriptinterface/ScriptInterface.h"
#include "scriptinterface/JSON.h"
#include <string_view>
#include <vector>
#include <wctype.h>
@ -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);

View File

@ -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;
}

View File

@ -28,6 +28,7 @@
#include "scriptinterface/ScriptInterface.h"
#include <libxml/parser.h>
#include <string_view>
#include <unordered_map>
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.");

View File

@ -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)))

View File

@ -36,6 +36,8 @@
#include "renderer/RenderingOptions.h"
#include "tools/atlas/GameInterface/GameLoop.h"
#include <string_view>
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)

View File

@ -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 <string_view>
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<entity_id_t> 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;
}

View File

@ -32,6 +32,8 @@
#include "simulation2/system/ParamNode.h"
#include "simulation2/system/SimContext.h"
#include <string_view>
/**
* Used for script-only message types.
*/
@ -273,26 +275,27 @@ void CComponentManager::Script_RegisterComponentType_Common(int iid, const std::
return;
}
for (std::vector<std::string>::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<std::string, MessageTypeId>::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;
}

View File

@ -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 <sstream>
#include <string_view>
#include <boost/algorithm/string.hpp>
@ -143,21 +144,31 @@ void CParamNode::ApplyLayer(const XMBData& xmb, const XMBElement& element, const
// Merge the two lists
std::vector<std::string> 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<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));
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);
}
}