diff --git a/Source/Core/AudioCommon/CubebUtils.cpp b/Source/Core/AudioCommon/CubebUtils.cpp index ffe1e8cfbb..e4050a8bf5 100644 --- a/Source/Core/AudioCommon/CubebUtils.cpp +++ b/Source/Core/AudioCommon/CubebUtils.cpp @@ -30,7 +30,7 @@ static void LogCallback(const char* format, ...) va_start(args, format); const char* filename = va_arg(args, const char*) + s_path_cutoff_point; const int lineno = va_arg(args, int); - const std::string adapted_format(StripSpaces(format + strlen("%s:%d:"))); + const std::string adapted_format(StripWhitespace(format + strlen("%s:%d:"))); const std::string message = StringFromFormatV(adapted_format.c_str(), args); va_end(args); diff --git a/Source/Core/Common/IniFile.cpp b/Source/Core/Common/IniFile.cpp index 0925b8b4d4..41f1917fef 100644 --- a/Source/Core/Common/IniFile.cpp +++ b/Source/Core/Common/IniFile.cpp @@ -25,11 +25,11 @@ void IniFile::ParseLine(std::string_view line, std::string* keyOut, std::string* if (firstEquals != std::string::npos) { // Yes, a valid line! - *keyOut = StripSpaces(line.substr(0, firstEquals)); + *keyOut = StripWhitespace(line.substr(0, firstEquals)); if (valueOut) { - *valueOut = StripQuotes(StripSpaces(line.substr(firstEquals + 1, std::string::npos))); + *valueOut = StripQuotes(StripWhitespace(line.substr(firstEquals + 1, std::string::npos))); } } } @@ -96,7 +96,7 @@ bool IniFile::Section::GetLines(std::vector* lines, const bool remo { for (const std::string& line : m_lines) { - std::string_view stripped_line = StripSpaces(line); + std::string_view stripped_line = StripWhitespace(line); if (remove_comments) { @@ -108,7 +108,7 @@ bool IniFile::Section::GetLines(std::vector* lines, const bool remo if (commentPos != std::string::npos) { - stripped_line = StripSpaces(stripped_line.substr(0, commentPos)); + stripped_line = StripWhitespace(stripped_line.substr(0, commentPos)); } } diff --git a/Source/Core/Common/StringUtil.cpp b/Source/Core/Common/StringUtil.cpp index c498e261fd..4a5f21ddc2 100644 --- a/Source/Core/Common/StringUtil.cpp +++ b/Source/Core/Common/StringUtil.cpp @@ -219,20 +219,31 @@ std::string ArrayToString(const u8* data, u32 size, int line_len, bool spaces) return oss.str(); } -// Turns "\n\r\t hello " into "hello" (trims at the start and end but not inside). -std::string_view StripSpaces(std::string_view str) +template +static std::string_view StripEnclosingChars(std::string_view str, T chars) { - const size_t s = str.find_first_not_of(" \t\r\n"); + const size_t s = str.find_first_not_of(chars); if (str.npos != s) - return str.substr(s, str.find_last_not_of(" \t\r\n") - s + 1); + return str.substr(s, str.find_last_not_of(chars) - s + 1); else return ""; } +// Turns "\n\r\t hello " into "hello" (trims at the start and end but not inside). +std::string_view StripWhitespace(std::string_view str) +{ + return StripEnclosingChars(str, " \t\r\n"); +} + +std::string_view StripSpaces(std::string_view str) +{ + return StripEnclosingChars(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. +// ends, as done by StripWhitespace above, for example. std::string_view StripQuotes(std::string_view s) { if (!s.empty() && '\"' == s[0] && '\"' == *s.rbegin()) diff --git a/Source/Core/Common/StringUtil.h b/Source/Core/Common/StringUtil.h index 5db7cbfa37..0df7b8d119 100644 --- a/Source/Core/Common/StringUtil.h +++ b/Source/Core/Common/StringUtil.h @@ -46,6 +46,7 @@ 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_view StripWhitespace(std::string_view s); std::string_view StripSpaces(std::string_view s); std::string_view StripQuotes(std::string_view s); diff --git a/Source/Core/Core/GeckoCodeConfig.cpp b/Source/Core/Core/GeckoCodeConfig.cpp index ebb1f78160..e4fc7c4c68 100644 --- a/Source/Core/Core/GeckoCodeConfig.cpp +++ b/Source/Core/Core/GeckoCodeConfig.cpp @@ -54,7 +54,7 @@ std::vector DownloadCodes(std::string gametdb_id, bool* succeeded, bo { // Remove \r at the end of the line for files using windows line endings, std::getline only // removes \n - line = StripSpaces(line); + line = StripWhitespace(line); if (line.empty()) { @@ -74,7 +74,7 @@ std::vector DownloadCodes(std::string gametdb_id, bool* succeeded, bo std::istringstream ssline(line); // stop at [ character (beginning of contributor name) std::getline(ssline, gcode.name, '['); - gcode.name = StripSpaces(gcode.name); + gcode.name = StripWhitespace(gcode.name); gcode.user_defined = true; // read the code creator name std::getline(ssline, gcode.creator, ']'); @@ -165,7 +165,7 @@ std::vector LoadCodes(const IniFile& globalIni, const IniFile& localI ss.seekg(1, std::ios_base::cur); // read the code name std::getline(ss, gcode.name, '['); // stop at [ character (beginning of contributor name) - gcode.name = StripSpaces(gcode.name); + gcode.name = StripWhitespace(gcode.name); // read the code creator name std::getline(ss, gcode.creator, ']'); break; diff --git a/Source/Core/Core/PowerPC/PPCSymbolDB.cpp b/Source/Core/Core/PowerPC/PPCSymbolDB.cpp index 6683dd6904..d684cf64da 100644 --- a/Source/Core/Core/PowerPC/PPCSymbolDB.cpp +++ b/Source/Core/Core/PowerPC/PPCSymbolDB.cpp @@ -298,7 +298,7 @@ bool PPCSymbolDB::LoadMap(const std::string& filename, bool bad) constexpr auto is_hex_str = [](const std::string& s) { return !s.empty() && s.find_first_not_of("0123456789abcdefABCDEF") == std::string::npos; }; - const std::string stripped_line(StripSpaces(line)); + const std::string stripped_line(StripWhitespace(line)); std::istringstream iss(stripped_line); iss.imbue(std::locale::classic()); std::string word; diff --git a/Source/Core/Core/PowerPC/SignatureDB/MEGASignatureDB.cpp b/Source/Core/Core/PowerPC/SignatureDB/MEGASignatureDB.cpp index c8edb40898..b12e7f1e6f 100644 --- a/Source/Core/Core/PowerPC/SignatureDB/MEGASignatureDB.cpp +++ b/Source/Core/Core/PowerPC/SignatureDB/MEGASignatureDB.cpp @@ -52,7 +52,7 @@ bool GetFunctionName(std::istringstream* iss, std::string* name) std::getline(*iss, buffer); size_t next = buffer.find(" ^"); - *name = StripSpaces(buffer.substr(0, next)); + *name = StripWhitespace(buffer.substr(0, next)); if (name->empty()) return false; diff --git a/Source/Core/Core/TitleDatabase.cpp b/Source/Core/Core/TitleDatabase.cpp index d53a298350..391a0dc044 100644 --- a/Source/Core/Core/TitleDatabase.cpp +++ b/Source/Core/Core/TitleDatabase.cpp @@ -43,9 +43,9 @@ static Map LoadMap(const std::string& file_path) if (equals_index != std::string::npos) { const std::string_view line_view(line); - const std::string_view game_id = StripSpaces(line_view.substr(0, equals_index)); + const std::string_view game_id = StripWhitespace(line_view.substr(0, equals_index)); if (game_id.length() >= 4) - map.emplace(game_id, StripSpaces(line_view.substr(equals_index + 1))); + map.emplace(game_id, StripWhitespace(line_view.substr(equals_index + 1))); } } return map; diff --git a/Source/Core/DiscIO/VolumeVerifier.cpp b/Source/Core/DiscIO/VolumeVerifier.cpp index 4e311722d4..03b50e7d82 100644 --- a/Source/Core/DiscIO/VolumeVerifier.cpp +++ b/Source/Core/DiscIO/VolumeVerifier.cpp @@ -261,7 +261,7 @@ std::vector RedumpVerifier::ScanDatfile(const st // disc with the game ID "G96P" and the serial "DL-DOL-D96P-EUR, DL-DOL-G96P-EUR". for (const std::string& serial_str : SplitString(serials, ',')) { - const std::string_view serial = StripSpaces(serial_str); + const std::string_view serial = StripWhitespace(serial_str); // Skip the prefix, normally either "DL-DOL-" or "RVL-" (depending on the console), // but there are some exceptions like the "RVLE-SBSE-USA-B0" serial. diff --git a/Source/Core/DolphinQt/MenuBar.cpp b/Source/Core/DolphinQt/MenuBar.cpp index a5b0adca99..a119240f59 100644 --- a/Source/Core/DolphinQt/MenuBar.cpp +++ b/Source/Core/DolphinQt/MenuBar.cpp @@ -1150,7 +1150,7 @@ void MenuBar::CheckNAND() { title_listings += " - " + banner.GetName(); const std::string description = banner.GetDescription(); - if (!StripSpaces(description).empty()) + if (!StripWhitespace(description).empty()) title_listings += " - " + description; } } diff --git a/Source/Core/DolphinQt/Settings/BroadbandAdapterSettingsDialog.cpp b/Source/Core/DolphinQt/Settings/BroadbandAdapterSettingsDialog.cpp index 8a8d0c752c..6e1e404b92 100644 --- a/Source/Core/DolphinQt/Settings/BroadbandAdapterSettingsDialog.cpp +++ b/Source/Core/DolphinQt/Settings/BroadbandAdapterSettingsDialog.cpp @@ -95,7 +95,7 @@ void BroadbandAdapterSettingsDialog::InitControls() void BroadbandAdapterSettingsDialog::SaveAddress() { - const std::string bba_new_address(StripSpaces(m_address_input->text().toStdString())); + const std::string bba_new_address(StripWhitespace(m_address_input->text().toStdString())); switch (m_bba_type) { diff --git a/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp b/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp index 1024816e22..bb0888195e 100644 --- a/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp +++ b/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp @@ -124,8 +124,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 vid_string(StripWhitespace(device_vid_textbox->text().toStdString())); + const std::string pid_string(StripWhitespace(device_pid_textbox->text().toStdString())); if (!IsValidUSBIDString(vid_string)) { // i18n: Here, VID means Vendor ID (for a USB device). diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp b/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp index 59c22612f4..1f78071856 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp @@ -980,7 +980,7 @@ static std::unique_ptr ParseBarewordExpression(const std::string& st ParseResult ParseExpression(const std::string& str) { - if (StripSpaces(str).empty()) + if (StripWhitespace(str).empty()) return ParseResult::MakeEmptyResult(); auto bareword_expr = ParseBarewordExpression(str); diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp b/Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp index 1ab12af20a..aa1826ae45 100644 --- a/Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp @@ -41,7 +41,7 @@ std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device) HRESULT hr = device->GetProperty(DIPROP_PRODUCTNAME, &str.diph); if (SUCCEEDED(hr)) { - result = StripSpaces(WStringToUTF8(str.wsz)); + result = StripWhitespace(WStringToUTF8(str.wsz)); } else { diff --git a/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm b/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm index 10c30ffb21..2d0651b6bd 100644 --- a/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm +++ b/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm @@ -139,7 +139,7 @@ static std::string GetDeviceRefName(IOHIDDeviceRef inIOHIDDeviceRef) { const NSString* name = reinterpret_cast( IOHIDDeviceGetProperty(inIOHIDDeviceRef, CFSTR(kIOHIDProductKey))); - return (name != nullptr) ? std::string(StripSpaces([name UTF8String])) : "Unknown device"; + return (name != nullptr) ? std::string(StripWhitespace([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 f8ee6e5446..82ad68e205 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 ").append(StripSpaces(s.str())); + return std::string("Button ").append(StripWhitespace(s.str())); } Joystick::Axis::Axis(IOHIDElementRef element, IOHIDDeviceRef device, direction dir) @@ -210,7 +210,7 @@ Joystick::Axis::Axis(IOHIDElementRef element, IOHIDDeviceRef device, direction d std::ostringstream s; s << "CK-"; s << elementCookie; - description = StripSpaces(s.str()); + description = StripWhitespace(s.str()); break; } } diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp index 0d7494142d..0ea15251ad 100644 --- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp +++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp @@ -272,7 +272,7 @@ void PopulateDevices() } Joystick::Joystick(SDL_Joystick* const joystick, const int sdl_index) - : m_joystick(joystick), m_name(StripSpaces(GetJoystickName(sdl_index))) + : m_joystick(joystick), m_name(StripWhitespace(GetJoystickName(sdl_index))) { // really bad HACKS: // to not use SDL for an XInput device diff --git a/Source/Core/InputCommon/ControllerInterface/WGInput/WGInput.cpp b/Source/Core/InputCommon/ControllerInterface/WGInput/WGInput.cpp index b934e86e71..f755a175a5 100644 --- a/Source/Core/InputCommon/ControllerInterface/WGInput/WGInput.cpp +++ b/Source/Core/InputCommon/ControllerInterface/WGInput/WGInput.cpp @@ -784,7 +784,7 @@ void PopulateDevices() if (SUCCEEDED(hr = rgc2->get_DisplayName(&hstr)) && hstr) { device_name = - StripSpaces(WStringToUTF8(g_WindowsGetStringRawBuffer_address(hstr, nullptr))); + StripWhitespace(WStringToUTF8(g_WindowsGetStringRawBuffer_address(hstr, nullptr))); } } diff --git a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp index 6df37b47df..d3a222890b 100644 --- a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp +++ b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp @@ -52,7 +52,7 @@ protected: { if (const char* code_name = libevdev_event_code_get_name(EV_KEY, m_code)) { - const auto name = StripSpaces(code_name); + const auto name = StripWhitespace(code_name); for (auto remove_prefix : {"BTN_", "KEY_"}) { @@ -441,7 +441,7 @@ bool evdevDevice::AddNode(std::string devnode, int fd, libevdev* dev) m_nodes.emplace_back(Node{std::move(devnode), fd, dev}); // Take on the alphabetically first name. - const auto potential_new_name = StripSpaces(libevdev_get_name(dev)); + const auto potential_new_name = StripWhitespace(libevdev_get_name(dev)); if (m_name.empty() || potential_new_name < m_name) m_name = potential_new_name; diff --git a/Source/Core/InputCommon/InputProfile.cpp b/Source/Core/InputCommon/InputProfile.cpp index be0f02c975..492b45975e 100644 --- a/Source/Core/InputCommon/InputProfile.cpp +++ b/Source/Core/InputCommon/InputProfile.cpp @@ -34,7 +34,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 + std::string(StripSpaces(setting_choice)); + const std::string path = root + std::string(StripWhitespace(setting_choice)); if (File::IsDirectory(path)) { const auto files_under_directory = Common::DoFileSearch({path}, {".ini"}, true);