diff options
| author | Shawn Hoffman <godisgovernment@gmail.com> | 2022-07-19 15:13:26 -0700 |
|---|---|---|
| committer | Shawn Hoffman <godisgovernment@gmail.com> | 2022-07-25 18:40:12 -0700 |
| commit | f92541fbd9da5091636aac09fdabd1a5fd0191ce (patch) | |
| tree | 51f4add6038f41c213f392559501a6121ee28ec3 /Source/Core/Common | |
| parent | e4ff49769c687e892219dab628ad5c7850efa49d (diff) | |
StripSpaces: only strip spaces
StripWhitespace maintains old behavior
Diffstat (limited to 'Source/Core/Common')
| -rw-r--r-- | Source/Core/Common/IniFile.cpp | 8 | ||||
| -rw-r--r-- | Source/Core/Common/StringUtil.cpp | 21 | ||||
| -rw-r--r-- | Source/Core/Common/StringUtil.h | 1 |
3 files changed, 21 insertions, 9 deletions
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<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)); } } 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 <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()) 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); |
