Merge pull request #10893 from shuffle2/spaces

StripSpaces: only strip spaces
This commit is contained in:
Admiral H. Curtiss 2022-07-26 04:23:24 +02:00 committed by GitHub
commit 8723b1a42e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 44 additions and 32 deletions

View File

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

View File

@ -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<std::string>* 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<std::string>* 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));
}
}

View File

@ -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 <typename T>
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())

View File

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

View File

@ -54,7 +54,7 @@ std::vector<GeckoCode> 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<GeckoCode> 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<GeckoCode> 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;

View File

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

View File

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

View File

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

View File

@ -261,7 +261,7 @@ std::vector<RedumpVerifier::PotentialMatch> 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.

View File

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

View File

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

View File

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

View File

@ -980,7 +980,7 @@ static std::unique_ptr<Expression> 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);

View File

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

View File

@ -139,7 +139,7 @@ static std::string GetDeviceRefName(IOHIDDeviceRef inIOHIDDeviceRef)
{
const NSString* name = reinterpret_cast<const NSString*>(
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,

View File

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

View File

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

View File

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

View File

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

View File

@ -34,7 +34,7 @@ std::vector<std::string> GetProfilesFromSetting(const std::string& setting, cons
std::vector<std::string> 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);