diff --git a/src/common/Configuration/Config.cpp b/src/common/Configuration/Config.cpp index 253fbf18c..0d436bef1 100644 --- a/src/common/Configuration/Config.cpp +++ b/src/common/Configuration/Config.cpp @@ -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()); diff --git a/src/common/Utilities/StringFormat.cpp b/src/common/Utilities/StringFormat.cpp index 64d3a2212..47537e732 100644 --- a/src/common/Utilities/StringFormat.cpp +++ b/src/common/Utilities/StringFormat.cpp @@ -4,37 +4,31 @@ */ #include "StringFormat.h" +#include -// Taken from https://stackoverflow.com/a/1798170 -std::string acore::String::Trim(std::string const& str, std::string_view whitespace /*= " \t"*/) +template +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(const std::string& s, const std::locale& loc /*= std::locale()*/); diff --git a/src/common/Utilities/StringFormat.h b/src/common/Utilities/StringFormat.h index 0af0e492e..11cd91b0d 100644 --- a/src/common/Utilities/StringFormat.h +++ b/src/common/Utilities/StringFormat.h @@ -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 + Str Trim(const Str& s, const std::locale& loc = std::locale()); } #endif