fix(Core/Utilites): improve acore::String::Trim (#4704)

- Improve `acore::String::Trim`
- Delete `acore::String::Reduce`
- Skip line comment #4748
This commit is contained in:
Kargatum
2021-03-09 21:02:45 +07:00
committed by GitHub
parent ddc18fa6c2
commit 60d684282b
3 changed files with 31 additions and 35 deletions
+7 -5
View File
@@ -35,8 +35,6 @@ namespace
} }
_configOptions.emplace(optionName, optionKey); _configOptions.emplace(optionName, optionKey);
//sLog->outError("> Config: Add '%s' - '%s'\n", optionName.c_str(), optionKey.c_str());
} }
void ParseFile(std::string const& file) void ParseFile(std::string const& file)
@@ -56,19 +54,23 @@ namespace
if (line.empty()) if (line.empty())
continue; continue;
line = acore::String::Reduce(line); line = acore::String::Trim(line, in.getloc());
// comments // comments
if (line[0] == '#' || line[0] == '[') if (line[0] == '#' || line[0] == '[')
continue; continue;
size_t found = line.find_first_of('#');
if (found != std::string::npos)
line = line.substr(0, found);
auto const equal_pos = line.find('='); auto const equal_pos = line.find('=');
if (equal_pos == std::string::npos || equal_pos == line.length()) if (equal_pos == std::string::npos || equal_pos == line.length())
return; return;
auto entry = acore::String::Reduce(line.substr(0, equal_pos)); auto entry = acore::String::Trim(line.substr(0, equal_pos), in.getloc());
auto value = acore::String::Reduce(line.substr(equal_pos + 1)); auto value = acore::String::Trim(line.substr(equal_pos + 1), in.getloc());
value.erase(std::remove(value.begin(), value.end(), '"'), value.end()); value.erase(std::remove(value.begin(), value.end(), '"'), value.end());
+22 -28
View File
@@ -4,37 +4,31 @@
*/ */
#include "StringFormat.h" #include "StringFormat.h"
#include <locale>
// Taken from https://stackoverflow.com/a/1798170 template<class Str>
std::string acore::String::Trim(std::string const& str, std::string_view whitespace /*= " \t"*/) Str acore::String::Trim(const Str& s, const std::locale& loc /*= std::locale()*/)
{ {
const auto strBegin = str.find_first_not_of(whitespace); typename Str::const_iterator first = s.begin();
if (strBegin == std::string::npos) typename Str::const_iterator end = s.end();
return ""; // no content
auto const strEnd = str.find_last_not_of(whitespace); while (first != end && std::isspace(*first, loc))
auto const strRange = strEnd - strBegin + 1; ++first;
return str.substr(strBegin, strRange); if (first == end)
return Str();
typename Str::const_iterator last = end;
do
--last;
while (std::isspace(*last, loc));
if (first != s.begin() || last + 1 != end)
return Str(first, last + 1);
return s;
} }
std::string acore::String::Reduce(std::string const& str, std::string_view fill /*= " "*/, std::string_view whitespace /*= " \t"*/) // Template Trim
{ template std::string acore::String::Trim<std::string>(const std::string& s, const std::locale& loc /*= std::locale()*/);
// trim first
auto result = Trim(str, whitespace);
// replace sub ranges
auto beginSpace = result.find_first_of(whitespace);
while (beginSpace != std::string::npos)
{
const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
const auto range = endSpace - beginSpace;
result.replace(beginSpace, range, fill);
const auto newStart = beginSpace + fill.length();
beginSpace = result.find_first_of(whitespace, newStart);
}
return result;
}
+2 -2
View File
@@ -42,8 +42,8 @@ namespace acore
namespace acore::String namespace acore::String
{ {
std::string Trim(std::string const& str, std::string_view whitespace = " \t"); template<class Str>
std::string Reduce(std::string const& str, std::string_view fill = " ", std::string_view whitespace = " \t"); Str Trim(const Str& s, const std::locale& loc = std::locale());
} }
#endif #endif