mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-13 09:17:18 +00:00
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:
@@ -35,8 +35,6 @@ namespace
|
||||
}
|
||||
|
||||
_configOptions.emplace(optionName, optionKey);
|
||||
|
||||
//sLog->outError("> Config: Add '%s' - '%s'\n", optionName.c_str(), optionKey.c_str());
|
||||
}
|
||||
|
||||
void ParseFile(std::string const& file)
|
||||
@@ -56,19 +54,23 @@ namespace
|
||||
if (line.empty())
|
||||
continue;
|
||||
|
||||
line = acore::String::Reduce(line);
|
||||
line = acore::String::Trim(line, in.getloc());
|
||||
|
||||
// comments
|
||||
if (line[0] == '#' || line[0] == '[')
|
||||
continue;
|
||||
|
||||
size_t found = line.find_first_of('#');
|
||||
if (found != std::string::npos)
|
||||
line = line.substr(0, found);
|
||||
|
||||
auto const equal_pos = line.find('=');
|
||||
|
||||
if (equal_pos == std::string::npos || equal_pos == line.length())
|
||||
return;
|
||||
|
||||
auto entry = acore::String::Reduce(line.substr(0, equal_pos));
|
||||
auto value = acore::String::Reduce(line.substr(equal_pos + 1));
|
||||
auto entry = acore::String::Trim(line.substr(0, equal_pos), in.getloc());
|
||||
auto value = acore::String::Trim(line.substr(equal_pos + 1), in.getloc());
|
||||
|
||||
value.erase(std::remove(value.begin(), value.end(), '"'), value.end());
|
||||
|
||||
|
||||
@@ -4,37 +4,31 @@
|
||||
*/
|
||||
|
||||
#include "StringFormat.h"
|
||||
#include <locale>
|
||||
|
||||
// Taken from https://stackoverflow.com/a/1798170
|
||||
std::string acore::String::Trim(std::string const& str, std::string_view whitespace /*= " \t"*/)
|
||||
template<class Str>
|
||||
Str acore::String::Trim(const Str& s, const std::locale& loc /*= std::locale()*/)
|
||||
{
|
||||
const auto strBegin = str.find_first_not_of(whitespace);
|
||||
if (strBegin == std::string::npos)
|
||||
return ""; // no content
|
||||
typename Str::const_iterator first = s.begin();
|
||||
typename Str::const_iterator end = s.end();
|
||||
|
||||
auto const strEnd = str.find_last_not_of(whitespace);
|
||||
auto const strRange = strEnd - strBegin + 1;
|
||||
while (first != end && std::isspace(*first, loc))
|
||||
++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"*/)
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
// Template Trim
|
||||
template std::string acore::String::Trim<std::string>(const std::string& s, const std::locale& loc /*= std::locale()*/);
|
||||
|
||||
@@ -42,8 +42,8 @@ namespace acore
|
||||
|
||||
namespace acore::String
|
||||
{
|
||||
std::string Trim(std::string const& str, std::string_view whitespace = " \t");
|
||||
std::string Reduce(std::string const& str, std::string_view fill = " ", std::string_view whitespace = " \t");
|
||||
template<class Str>
|
||||
Str Trim(const Str& s, const std::locale& loc = std::locale());
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user