From 60824e77b99384e1e805f38b8809395016eceaf5 Mon Sep 17 00:00:00 2001 From: phosit Date: Mon, 24 Aug 2026 13:34:37 +0200 Subject: [PATCH] Remove CStr::Left It was only a wrapper around `std::string::substr`. In some places it's better to use `std::string::starts_with`. --- source/gui/CGUI.cpp | 2 +- source/gui/ObjectTypes/CInput.cpp | 20 ++++++++++---------- source/network/NetServer.cpp | 2 +- source/ps/CConsole.cpp | 2 +- source/ps/CStr.cpp | 8 -------- source/ps/CStr.h | 8 -------- source/ps/KeyName.cpp | 2 +- source/ps/scripting/JSInterface_VFS.cpp | 2 +- 8 files changed, 15 insertions(+), 31 deletions(-) diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index 50c4bcd126..d4018a0ac6 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -732,7 +732,7 @@ void CGUI::Xeromyces_ReadObject(const XMBData& xmb, XMBElement element, IGUIObje { CStr name(attr.Value); - if (name.Left(2) == "__") + if (name.starts_with("__")) { LOGERROR("GUI: Names starting with '__' are reserved for the engine (object: %s)", name.c_str()); continue; diff --git a/source/gui/ObjectTypes/CInput.cpp b/source/gui/ObjectTypes/CInput.cpp index b9c41b87b3..c6dc7745f9 100644 --- a/source/gui/ObjectTypes/CInput.cpp +++ b/source/gui/ObjectTypes/CInput.cpp @@ -293,10 +293,10 @@ void CInput::ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode) break; if (m_iBufferPos == static_cast(caption.length())) - caption = caption.Left(static_cast(caption.length()) - 1); + caption = caption.substr(0, static_cast(caption.length()) - 1); else caption = - caption.Left(m_iBufferPos - 1) + + caption.substr(0, m_iBufferPos - 1) + caption.Right(static_cast(caption.length()) - m_iBufferPos); --m_iBufferPos; @@ -320,7 +320,7 @@ void CInput::ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode) break; caption = - caption.Left(m_iBufferPos) + + caption.substr(0, m_iBufferPos) + caption.Right(static_cast(caption.length()) - (m_iBufferPos + 1)); UpdateText(m_iBufferPos, m_iBufferPos + 1, m_iBufferPos); @@ -364,7 +364,7 @@ void CInput::ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode) caption += cooked; else caption = - caption.Left(m_iBufferPos) + cooked + + caption.substr(0, m_iBufferPos) + cooked + caption.Right(static_cast(caption.length()) - m_iBufferPos); UpdateText(m_iBufferPos, m_iBufferPos, m_iBufferPos + 1); @@ -655,7 +655,7 @@ Input::Reaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) caption += text; else caption = - caption.Left(m_iBufferPos) + text + + caption.substr(0, m_iBufferPos) + text + caption.Right(static_cast(caption.length()) - m_iBufferPos); UpdateText(m_iBufferPos, m_iBufferPos, m_iBufferPos+1); @@ -691,7 +691,7 @@ Input::Reaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) virtualTo = m_iBufferPos; } - CStrW text = caption.Left(virtualTo).Right(virtualTo - virtualFrom); + CStrW text = caption.substr(virtualFrom, virtualTo - virtualFrom); SDL_SetClipboardText(text.ToUTF8().c_str()); @@ -718,7 +718,7 @@ Input::Reaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) if (!caption.empty() && m_iBufferPos != 0) { m_iBufferPos_Tail = m_iBufferPos; - CStrW searchString = caption.Left(m_iBufferPos); + CStrW searchString = caption.substr(0, m_iBufferPos); // If we are starting in whitespace, adjust position until we get a non whitespace while (m_iBufferPos > 0) @@ -801,7 +801,7 @@ Input::Reaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) if (!caption.empty() && m_iBufferPos != 0) { - CStrW searchString = caption.Left(m_iBufferPos); + CStrW searchString = caption.substr(0, m_iBufferPos); // If we are starting in whitespace, adjust position until we get a non whitespace while (m_iBufferPos > 0) @@ -2009,8 +2009,8 @@ void CInput::DeleteCurSelection() } // Silently change. - m_Caption.Set(m_Caption->Left(virtualFrom) + m_Caption->Right(static_cast(m_Caption->length()) - virtualTo), - false); + m_Caption.Set(m_Caption->substr(0, virtualFrom) + + m_Caption->Right(static_cast(m_Caption->length()) - virtualTo), false); UpdateText(virtualFrom, virtualTo, virtualFrom); diff --git a/source/network/NetServer.cpp b/source/network/NetServer.cpp index c4198a797d..447a074c98 100644 --- a/source/network/NetServer.cpp +++ b/source/network/NetServer.cpp @@ -1613,7 +1613,7 @@ CStrW CNetServerWorker::SanitisePlayerName(const CStrW& original) // Restrict the length if (name.length() > MAX_LENGTH) - name = name.Left(MAX_LENGTH); + name = name.substr(0, MAX_LENGTH); // Don't allow surrounding whitespace name.Trim(PS_TRIM_BOTH); diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp index fe2bac0346..d909be8c2a 100644 --- a/source/ps/CConsole.cpp +++ b/source/ps/CConsole.cpp @@ -643,7 +643,7 @@ void CConsole::LoadHistory() if (pos != CStrW::npos) { if (pos > 0) - m_BufHistory.push_front(str.Left(str[pos-1] == '\r' ? pos - 1 : pos)); + m_BufHistory.push_front(str.substr(0, str[pos-1] == '\r' ? pos - 1 : pos)); str.erase(0, pos + 1); } else if (str.length() > 0) diff --git a/source/ps/CStr.cpp b/source/ps/CStr.cpp index cddf86dd2a..737f12ba4a 100644 --- a/source/ps/CStr.cpp +++ b/source/ps/CStr.cpp @@ -293,14 +293,6 @@ CStr CStr::UpperCase() const return newStr; } - -// Retrieve the substring of the first n characters -CStr CStr::Left(size_t len) const -{ - ENSURE(len <= length()); - return substr(0, len); -} - // Retrieve the substring of the last n characters CStr CStr::Right(size_t len) const { diff --git a/source/ps/CStr.h b/source/ps/CStr.h index b08e6ce120..c5b820b132 100644 --- a/source/ps/CStr.h +++ b/source/ps/CStr.h @@ -161,14 +161,6 @@ public: **/ CStr UpperCase() const; - /** - * Retrieve first n characters of the CStr. - * - * @param size_t len the number of characters to retrieve. - * @return CStr retrieved substring. - **/ - CStr Left(size_t len) const; - /** * Retrieve last n characters of the CStr. * diff --git a/source/ps/KeyName.cpp b/source/ps/KeyName.cpp index 7b93c269d9..ad81ee0e42 100644 --- a/source/ps/KeyName.cpp +++ b/source/ps/KeyName.cpp @@ -109,7 +109,7 @@ SDL_Scancode FindScancode(const CStr8& keyname) return code; // Parse SYM_XX codes, see below. - if (keyname.size() > 4 && keyname.Left(4) == "SYM_") + if (keyname.size() > 4 && keyname.starts_with("SYM_")) return static_cast(CStr(keyname.substr(4)).ToInt()); return SDL_SCANCODE_UNKNOWN; diff --git a/source/ps/scripting/JSInterface_VFS.cpp b/source/ps/scripting/JSInterface_VFS.cpp index f02c170879..c104d494ae 100644 --- a/source/ps/scripting/JSInterface_VFS.cpp +++ b/source/ps/scripting/JSInterface_VFS.cpp @@ -288,7 +288,7 @@ void WriteJSONFile(const Script::Interface& scriptInterface, const std::wstring& bool DeleteCampaignSave(const CStrW& filePath) { OsPath realPath; - if (filePath.Left(16) != L"saves/campaigns/" || filePath.Right(12) != L".0adcampaign") + if (!filePath.starts_with(L"saves/campaigns/") || filePath.Right(12) != L".0adcampaign") return false; if (!VfsFileExists(filePath)) return false;