From a2a1e04fc9c26c7d4ae8af5d1229a906c0ae066d Mon Sep 17 00:00:00 2001 From: JosJuice Date: Mon, 8 Jul 2019 13:35:53 +0200 Subject: [PATCH] StringUtil: Use std::string_view more --- Source/Core/AudioCommon/CubebUtils.cpp | 2 +- Source/Core/Common/IniFile.cpp | 28 ++++---- Source/Core/Common/IniFile.h | 2 +- Source/Core/Common/StringUtil.cpp | 64 +++++++++---------- Source/Core/Common/StringUtil.h | 36 +++++------ Source/Core/Core/PowerPC/PPCSymbolDB.cpp | 3 +- Source/Core/Core/TitleDatabase.cpp | 4 +- .../USBDeviceAddToWhitelistDialog.cpp | 12 ++-- .../ControllerInterface/OSX/OSX.mm | 2 +- .../ControllerInterface/OSX/OSXJoystick.mm | 2 +- .../ControllerInterface/evdev/evdev.cpp | 2 +- Source/Core/InputCommon/InputProfile.cpp | 2 +- Source/Core/VideoCommon/PostProcessing.cpp | 16 ++--- 13 files changed, 92 insertions(+), 83 deletions(-) diff --git a/Source/Core/AudioCommon/CubebUtils.cpp b/Source/Core/AudioCommon/CubebUtils.cpp index 07f4f530d6..5eaaaf62cd 100644 --- a/Source/Core/AudioCommon/CubebUtils.cpp +++ b/Source/Core/AudioCommon/CubebUtils.cpp @@ -26,7 +26,7 @@ static void LogCallback(const char* format, ...) const char* filename = va_arg(args, const char*) + s_path_cutoff_point; int lineno = va_arg(args, int); - std::string adapted_format = StripSpaces(format + strlen("%s:%d:")); + std::string adapted_format(StripSpaces(format + strlen("%s:%d:"))); LogManager::GetInstance()->LogWithFullPath(LogTypes::LNOTICE, LogTypes::AUDIO, filename, lineno, adapted_format.c_str(), args); diff --git a/Source/Core/Common/IniFile.cpp b/Source/Core/Common/IniFile.cpp index c0b4f2be28..b1b9bcba6d 100644 --- a/Source/Core/Common/IniFile.cpp +++ b/Source/Core/Common/IniFile.cpp @@ -9,13 +9,14 @@ #include #include #include +#include #include #include #include "Common/FileUtil.h" #include "Common/StringUtil.h" -void IniFile::ParseLine(const std::string& line, std::string* keyOut, std::string* valueOut) +void IniFile::ParseLine(std::string_view line, std::string* keyOut, std::string* valueOut) { if (line.empty() || line.front() == '#') return; @@ -96,7 +97,7 @@ bool IniFile::Section::GetLines(std::vector* lines, const bool remo { for (const std::string& line : m_lines) { - std::string stripped_line = StripSpaces(line); + std::string_view stripped_line = StripSpaces(line); if (remove_comments) { @@ -112,7 +113,7 @@ bool IniFile::Section::GetLines(std::vector* lines, const bool remo } } - lines->push_back(std::move(stripped_line)); + lines->emplace_back(stripped_line); } return true; @@ -251,9 +252,8 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data) bool first_line = true; while (!in.eof()) { - std::string line; - - if (!std::getline(in, line)) + std::string line_str; + if (!std::getline(in, line_str)) { if (in.eof()) return true; @@ -261,17 +261,17 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data) return false; } + std::string_view line = line_str; + // Skips the UTF-8 BOM at the start of files. Notepad likes to add this. if (first_line && line.substr(0, 3) == "\xEF\xBB\xBF") - line = line.substr(3); + line.remove_prefix(3); first_line = false; #ifndef _WIN32 // Check for CRLF eol and convert it to LF if (!line.empty() && line.back() == '\r') - { - line.pop_back(); - } + line.remove_suffix(1); #endif if (!line.empty()) @@ -283,7 +283,7 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data) if (endpos != std::string::npos) { // New section! - std::string sub = line.substr(1, endpos - 1); + std::string_view sub = line.substr(1, endpos - 1); current_section = GetOrCreateSection(sub); } } @@ -299,9 +299,13 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data) // INI is a hack anyway. if ((key.empty() && value.empty()) || (!line.empty() && (line[0] == '$' || line[0] == '+' || line[0] == '*'))) - current_section->m_lines.push_back(line); + { + current_section->m_lines.emplace_back(line); + } else + { current_section->Set(key, value); + } } } } diff --git a/Source/Core/Common/IniFile.h b/Source/Core/Common/IniFile.h index 38afa2631c..422b30aa9a 100644 --- a/Source/Core/Common/IniFile.h +++ b/Source/Core/Common/IniFile.h @@ -158,7 +158,7 @@ public: // This function is related to parsing data from lines of INI files // It's used outside of IniFile, which is why it is exposed publicly // In particular it is used in PostProcessing for its configuration - static void ParseLine(const std::string& line, std::string* keyOut, std::string* valueOut); + static void ParseLine(std::string_view line, std::string* keyOut, std::string* valueOut); const std::list
& GetSections() const { return sections; } diff --git a/Source/Core/Common/StringUtil.cpp b/Source/Core/Common/StringUtil.cpp index 275c1e2af0..ba5acca363 100644 --- a/Source/Core/Common/StringUtil.cpp +++ b/Source/Core/Common/StringUtil.cpp @@ -214,7 +214,7 @@ std::string ArrayToString(const u8* data, u32 size, int line_len, bool spaces) } // Turns " hello " into "hello". Also handles tabs. -std::string StripSpaces(const std::string& str) +std::string_view StripSpaces(std::string_view str) { const size_t s = str.find_first_not_of(" \t\r\n"); @@ -227,7 +227,7 @@ std::string StripSpaces(const std::string& str) // "\"hello\"" is turned to "hello" // This one assumes that the string has already been space stripped in both // ends, as done by StripSpaces above, for example. -std::string StripQuotes(const std::string& s) +std::string_view StripQuotes(std::string_view s) { if (!s.empty() && '\"' == s[0] && '\"' == *s.rbegin()) return s.substr(1, s.size() - 2); @@ -334,7 +334,7 @@ std::string ValueToString(bool value) return value ? "True" : "False"; } -bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, +bool SplitPath(std::string_view full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension) { if (full_path.empty()) @@ -367,8 +367,8 @@ bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _ return true; } -void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, - const std::string& _Filename) +void BuildCompleteFilename(std::string& _CompleteFilename, std::string_view _Path, + std::string_view _Filename) { _CompleteFilename = _Path; @@ -407,19 +407,18 @@ std::string JoinStrings(const std::vector& strings, const std::stri return joined.substr(0, joined.length() - delimiter.length()); } -std::string TabsToSpaces(int tab_size, const std::string& in) +std::string TabsToSpaces(int tab_size, std::string str) { const std::string spaces(tab_size, ' '); - std::string out(in); size_t i = 0; - while (out.npos != (i = out.find('\t'))) - out.replace(i, 1, spaces); + while (str.npos != (i = str.find('\t'))) + str.replace(i, 1, spaces); - return out; + return str; } -std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) +std::string ReplaceAll(std::string result, std::string_view src, std::string_view dest) { size_t pos = 0; @@ -435,12 +434,12 @@ std::string ReplaceAll(std::string result, const std::string& src, const std::st return result; } -bool StringBeginsWith(const std::string& str, const std::string& begin) +bool StringBeginsWith(std::string_view str, std::string_view begin) { return str.size() >= begin.size() && std::equal(begin.begin(), begin.end(), str.begin()); } -bool StringEndsWith(const std::string& str, const std::string& end) +bool StringEndsWith(std::string_view str, std::string_view end) { return str.size() >= end.size() && std::equal(end.rbegin(), end.rend(), str.rbegin()); } @@ -453,7 +452,7 @@ void StringPopBackIf(std::string* s, char c) #ifdef _WIN32 -std::wstring CPToUTF16(u32 code_page, const std::string& input) +std::wstring CPToUTF16(u32 code_page, std::string_view input) { auto const size = MultiByteToWideChar(code_page, 0, input.data(), static_cast(input.size()), nullptr, 0); @@ -471,7 +470,7 @@ std::wstring CPToUTF16(u32 code_page, const std::string& input) return output; } -std::string UTF16ToCP(u32 code_page, const std::wstring& input) +std::string UTF16ToCP(u32 code_page, std::wstring_view input) { std::string output; @@ -487,7 +486,8 @@ std::string UTF16ToCP(u32 code_page, const std::wstring& input) &output[0], static_cast(output.size()), nullptr, false)) { const DWORD error_code = GetLastError(); - ERROR_LOG(COMMON, "WideCharToMultiByte Error in String '%s': %lu", input.c_str(), error_code); + ERROR_LOG(COMMON, "WideCharToMultiByte Error in String '%s': %lu", + std::wstring(input).c_str(), error_code); output.clear(); } } @@ -495,27 +495,27 @@ std::string UTF16ToCP(u32 code_page, const std::wstring& input) return output; } -std::wstring UTF8ToUTF16(const std::string& input) +std::wstring UTF8ToUTF16(std::string_view input) { return CPToUTF16(CP_UTF8, input); } -std::string UTF16ToUTF8(const std::wstring& input) +std::string UTF16ToUTF8(std::wstring_view input) { return UTF16ToCP(CP_UTF8, input); } -std::string SHIFTJISToUTF8(const std::string& input) +std::string SHIFTJISToUTF8(std::string_view input) { return UTF16ToUTF8(CPToUTF16(CODEPAGE_SHIFT_JIS, input)); } -std::string UTF8ToSHIFTJIS(const std::string& input) +std::string UTF8ToSHIFTJIS(std::string_view input) { return UTF16ToCP(CODEPAGE_SHIFT_JIS, UTF8ToUTF16(input)); } -std::string CP1252ToUTF8(const std::string& input) +std::string CP1252ToUTF8(std::string_view input) { return UTF16ToUTF8(CPToUTF16(CODEPAGE_WINDOWS_1252, input)); } @@ -531,7 +531,7 @@ std::string UTF16BEToUTF8(const char16_t* str, size_t max_size) #else template -std::string CodeTo(const char* tocode, const char* fromcode, const std::basic_string& input) +std::string CodeTo(const char* tocode, const char* fromcode, std::basic_string_view input) { std::string result; @@ -548,9 +548,9 @@ std::string CodeTo(const char* tocode, const char* fromcode, const std::basic_st std::string out_buffer; out_buffer.resize(out_buffer_size); - auto src_buffer = &input[0]; + auto src_buffer = input.data(); size_t src_bytes = in_bytes; - auto dst_buffer = &out_buffer[0]; + auto dst_buffer = out_buffer.data(); size_t dst_bytes = out_buffer.size(); while (src_bytes != 0) @@ -587,39 +587,39 @@ std::string CodeTo(const char* tocode, const char* fromcode, const std::basic_st } template -std::string CodeToUTF8(const char* fromcode, const std::basic_string& input) +std::string CodeToUTF8(const char* fromcode, std::basic_string_view input) { return CodeTo("UTF-8", fromcode, input); } -std::string CP1252ToUTF8(const std::string& input) +std::string CP1252ToUTF8(std::string_view input) { // return CodeToUTF8("CP1252//TRANSLIT", input); // return CodeToUTF8("CP1252//IGNORE", input); return CodeToUTF8("CP1252", input); } -std::string SHIFTJISToUTF8(const std::string& input) +std::string SHIFTJISToUTF8(std::string_view input) { // return CodeToUTF8("CP932", input); return CodeToUTF8("SJIS", input); } -std::string UTF8ToSHIFTJIS(const std::string& input) +std::string UTF8ToSHIFTJIS(std::string_view input) { return CodeTo("SJIS", "UTF-8", input); } -std::string UTF16ToUTF8(const std::wstring& input) +std::string UTF16ToUTF8(std::wstring_view input) { std::wstring_convert, wchar_t> converter; - return converter.to_bytes(input); + return converter.to_bytes(input.data(), input.data() + input.size()); } std::string UTF16BEToUTF8(const char16_t* str, size_t max_size) { const char16_t* str_end = std::find(str, str + max_size, '\0'); - return CodeToUTF8("UTF-16BE", std::u16string(str, static_cast(str_end - str))); + return CodeToUTF8("UTF-16BE", std::u16string_view(str, static_cast(str_end - str))); } #endif @@ -629,7 +629,7 @@ std::string UTF16BEToUTF8(const char16_t* str, size_t max_size) std::filesystem::path StringToPath(std::string_view path) { #ifdef _MSC_VER - return std::filesystem::path(UTF8ToUTF16(std::string(path))); + return std::filesystem::path(UTF8ToUTF16(path)); #else return std::filesystem::path(path); #endif diff --git a/Source/Core/Common/StringUtil.h b/Source/Core/Common/StringUtil.h index a1adf70b28..b0a7034a0e 100644 --- a/Source/Core/Common/StringUtil.h +++ b/Source/Core/Common/StringUtil.h @@ -44,8 +44,8 @@ inline void CharArrayFromFormat(char (&out)[Count], const char* format, ...) // Good std::string ArrayToString(const u8* data, u32 size, int line_len = 20, bool spaces = true); -std::string StripSpaces(const std::string& s); -std::string StripQuotes(const std::string& s); +std::string_view StripSpaces(std::string_view s); +std::string_view StripQuotes(std::string_view s); bool TryParse(const std::string& str, bool* output); bool TryParse(const std::string& str, u16* output); @@ -107,50 +107,50 @@ std::string HexDump(const u8* data, size_t size); // TODO: kill this bool AsciiToHex(const std::string& _szValue, u32& result); -std::string TabsToSpaces(int tab_size, const std::string& in); +std::string TabsToSpaces(int tab_size, std::string str); std::vector SplitString(const std::string& str, char delim); std::string JoinStrings(const std::vector& strings, const std::string& delimiter); // "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe" -bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, +bool SplitPath(std::string_view full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension); -void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, - const std::string& _Filename); -std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest); +void BuildCompleteFilename(std::string& _CompleteFilename, std::string_view _Path, + std::string_view _Filename); +std::string ReplaceAll(std::string result, std::string_view src, std::string_view dest); -bool StringBeginsWith(const std::string& str, const std::string& begin); -bool StringEndsWith(const std::string& str, const std::string& end); +bool StringBeginsWith(std::string_view str, std::string_view begin); +bool StringEndsWith(std::string_view str, std::string_view end); void StringPopBackIf(std::string* s, char c); -std::string CP1252ToUTF8(const std::string& str); -std::string SHIFTJISToUTF8(const std::string& str); -std::string UTF8ToSHIFTJIS(const std::string& str); -std::string UTF16ToUTF8(const std::wstring& str); +std::string CP1252ToUTF8(std::string_view str); +std::string SHIFTJISToUTF8(std::string_view str); +std::string UTF8ToSHIFTJIS(std::string_view str); +std::string UTF16ToUTF8(std::wstring_view str); std::string UTF16BEToUTF8(const char16_t* str, size_t max_size); // Stops at \0 #ifdef _WIN32 -std::wstring UTF8ToUTF16(const std::string& str); +std::wstring UTF8ToUTF16(std::string_view str); #ifdef _UNICODE -inline std::string TStrToUTF8(const std::wstring& str) +inline std::string TStrToUTF8(std::wstring_view str) { return UTF16ToUTF8(str); } -inline std::wstring UTF8ToTStr(const std::string& str) +inline std::wstring UTF8ToTStr(std::string_view str) { return UTF8ToUTF16(str); } #else -inline std::string TStrToUTF8(const std::string& str) +inline std::string TStrToUTF8(std::string_view str) { return str; } -inline std::string UTF8ToTStr(const std::string& str) +inline std::string UTF8ToTStr(std::string_view str) { return str; } diff --git a/Source/Core/Core/PowerPC/PPCSymbolDB.cpp b/Source/Core/Core/PowerPC/PPCSymbolDB.cpp index 1412533bf4..fc02b54bbb 100644 --- a/Source/Core/Core/PowerPC/PPCSymbolDB.cpp +++ b/Source/Core/Core/PowerPC/PPCSymbolDB.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -298,7 +299,7 @@ bool PPCSymbolDB::LoadMap(const std::string& filename, bool bad) // Detect two columns with three columns fallback if (column_count == 0) { - const std::string stripped_line = StripSpaces(line); + const std::string_view stripped_line = StripSpaces(line); if (std::count(stripped_line.begin(), stripped_line.end(), ' ') == 1) column_count = 2; else diff --git a/Source/Core/Core/TitleDatabase.cpp b/Source/Core/Core/TitleDatabase.cpp index c75da55ff3..963bdeeb73 100644 --- a/Source/Core/Core/TitleDatabase.cpp +++ b/Source/Core/Core/TitleDatabase.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include @@ -39,7 +41,7 @@ static Map LoadMap(const std::string& file_path) const size_t equals_index = line.find('='); if (equals_index != std::string::npos) { - const std::string game_id = StripSpaces(line.substr(0, equals_index)); + const std::string_view game_id = StripSpaces(line.substr(0, equals_index)); if (game_id.length() >= 4) map.emplace(game_id, StripSpaces(line.substr(equals_index + 1))); } diff --git a/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp b/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp index 1340fccb14..b51068ad25 100644 --- a/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp +++ b/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp @@ -4,6 +4,8 @@ #include "DolphinQt/Settings/USBDeviceAddToWhitelistDialog.h" +#include + #include #include #include @@ -27,7 +29,7 @@ #include "UICommon/USBUtils.h" -static bool IsValidUSBIDString(const std::string& string) +static bool IsValidUSBIDString(std::string_view string) { if (string.empty() || string.length() > 4) return false; @@ -124,8 +126,8 @@ void USBDeviceAddToWhitelistDialog::RefreshDeviceList() void USBDeviceAddToWhitelistDialog::AddUSBDeviceToWhitelist() { - const std::string vid_string = StripSpaces(device_vid_textbox->text().toStdString()); - const std::string pid_string = StripSpaces(device_pid_textbox->text().toStdString()); + const std::string_view vid_string = StripSpaces(device_vid_textbox->text().toStdString()); + const std::string_view pid_string = StripSpaces(device_pid_textbox->text().toStdString()); if (!IsValidUSBIDString(vid_string)) { // i18n: Here, VID means Vendor ID (for a USB device). @@ -151,8 +153,8 @@ void USBDeviceAddToWhitelistDialog::AddUSBDeviceToWhitelist() return; } - const u16 vid = static_cast(std::stoul(vid_string, nullptr, 16)); - const u16 pid = static_cast(std::stoul(pid_string, nullptr, 16)); + const u16 vid = static_cast(std::stoul(std::string(vid_string), nullptr, 16)); + const u16 pid = static_cast(std::stoul(std::string(pid_string), nullptr, 16)); if (SConfig::GetInstance().IsUSBDeviceWhitelisted({vid, pid})) { diff --git a/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm b/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm index 716a56b61e..37b1fa8cfd 100644 --- a/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm +++ b/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm @@ -141,7 +141,7 @@ static std::string GetDeviceRefName(IOHIDDeviceRef inIOHIDDeviceRef) { const NSString* name = reinterpret_cast( IOHIDDeviceGetProperty(inIOHIDDeviceRef, CFSTR(kIOHIDProductKey))); - return (name != nullptr) ? StripSpaces([name UTF8String]) : "Unknown device"; + return (name != nullptr) ? std::string(StripSpaces([name UTF8String])) : "Unknown device"; } static void DeviceRemovalCallback(void* inContext, IOReturn inResult, void* inSender, diff --git a/Source/Core/InputCommon/ControllerInterface/OSX/OSXJoystick.mm b/Source/Core/InputCommon/ControllerInterface/OSX/OSXJoystick.mm index 9ab06c91db..3b59d5b2ec 100644 --- a/Source/Core/InputCommon/ControllerInterface/OSX/OSXJoystick.mm +++ b/Source/Core/InputCommon/ControllerInterface/OSX/OSXJoystick.mm @@ -164,7 +164,7 @@ std::string Joystick::Button::GetName() const { std::ostringstream s; s << IOHIDElementGetUsage(m_element); - return std::string("Button ") + StripSpaces(s.str()); + return std::string("Button ").append(StripSpaces(s.str())); } Joystick::Axis::Axis(IOHIDElementRef element, IOHIDDeviceRef device, direction dir) diff --git a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp index 4443eaad3a..de14875f56 100644 --- a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp +++ b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp @@ -313,7 +313,7 @@ std::string evdevDevice::Button::GetName() const { const char* name = libevdev_event_code_get_name(EV_KEY, m_code); if (name) - return StripSpaces(name); + return std::string(StripSpaces(name)); } // But controllers use codes above 0x100, and the standard label often doesn't match. // We are better off with Button 0 and so on. diff --git a/Source/Core/InputCommon/InputProfile.cpp b/Source/Core/InputCommon/InputProfile.cpp index 65f4069cf9..2fbdf39706 100644 --- a/Source/Core/InputCommon/InputProfile.cpp +++ b/Source/Core/InputCommon/InputProfile.cpp @@ -32,7 +32,7 @@ std::vector GetProfilesFromSetting(const std::string& setting, cons std::vector result; for (const std::string& setting_choice : setting_choices) { - const std::string path = root + StripSpaces(setting_choice); + const std::string path = root + std::string(StripSpaces(setting_choice)); if (File::IsDirectory(path)) { const auto files_under_directory = Common::DoFileSearch({path}, {".ini"}, true); diff --git a/Source/Core/VideoCommon/PostProcessing.cpp b/Source/Core/VideoCommon/PostProcessing.cpp index 77f77cb9ce..859715542b 100644 --- a/Source/Core/VideoCommon/PostProcessing.cpp +++ b/Source/Core/VideoCommon/PostProcessing.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "Common/Assert.h" #include "Common/CommonPaths.h" @@ -117,16 +118,15 @@ void PostProcessingConfiguration::LoadOptions(const std::string& code) GLSLStringOption* current_strings = nullptr; while (!in.eof()) { - std::string line; - - if (std::getline(in, line)) + std::string line_str; + if (std::getline(in, line_str)) { + std::string_view line = line_str; + #ifndef _WIN32 // Check for CRLF eol and convert it to LF if (!line.empty() && line.at(line.size() - 1) == '\r') - { - line.erase(line.size() - 1); - } + line.remove_suffix(1); #endif if (!line.empty()) @@ -138,8 +138,8 @@ void PostProcessingConfiguration::LoadOptions(const std::string& code) if (endpos != std::string::npos) { // New section! - std::string sub = line.substr(1, endpos - 1); - option_strings.push_back({sub}); + std::string_view sub = line.substr(1, endpos - 1); + option_strings.push_back({std::string(sub)}); current_strings = &option_strings.back(); } }