Refactor some utils

This commit is contained in:
FutureRave
2022-02-26 22:50:53 +00:00
parent cba376a4dd
commit a5cc395b65
14 changed files with 41 additions and 46 deletions

View File

@ -71,7 +71,7 @@ namespace Utils
if (!buffer.empty())
{
auto rows = Utils::String::Explode(buffer, '\n');
auto rows = Utils::String::Split(buffer, '\n');
for (auto& row : rows)
{

View File

@ -9,9 +9,10 @@ namespace Utils
std::string InfoString::get(const std::string& key)
{
if (this->keyValuePairs.find(key) != this->keyValuePairs.end())
const auto value = this->keyValuePairs.find(key);
if (value != this->keyValuePairs.end())
{
return this->keyValuePairs[key];
return value->second;
}
return "";
@ -24,11 +25,13 @@ namespace Utils
buffer = buffer.substr(1);
}
std::vector<std::string> KeyValues = Utils::String::Explode(buffer, '\\');
auto KeyValues = Utils::String::Split(buffer, '\\');
for (unsigned int i = 0; i < (KeyValues.size() - 1); i += 2)
for (size_t i = 0; !KeyValues.empty() && i < (KeyValues.size() - 1); i += 2)
{
this->keyValuePairs[KeyValues[i]] = KeyValues[i + 1];
const auto& key = KeyValues[i];
const auto& value = KeyValues[i + 1];
this->keyValuePairs[key] = value;
}
}
@ -36,16 +39,16 @@ namespace Utils
{
std::string infoString;
bool first = true;
auto first = true;
for (auto i = this->keyValuePairs.begin(); i != this->keyValuePairs.end(); ++i)
for (const auto& [key, value] : this->keyValuePairs)
{
if (first) first = false;
else infoString.append("\\");
infoString.append(i->first); // Key
infoString.append(key);
infoString.append("\\");
infoString.append(i->second); // Value
infoString.append(value);
}
return infoString;
@ -53,9 +56,9 @@ namespace Utils
void InfoString::dump()
{
for (auto i = this->keyValuePairs.begin(); i != this->keyValuePairs.end(); ++i)
for (const auto& [key, value] : this->keyValuePairs)
{
OutputDebugStringA(Utils::String::VA("%s: %s", i->first.data(), i->second.data()));
OutputDebugStringA(Utils::String::VA("%s: %s\n", key.data(), value.data()));
}
}

View File

@ -7,11 +7,9 @@ namespace Utils
public:
InfoString() {};
InfoString(const std::string& buffer) : InfoString() { this->parse(buffer); };
InfoString(const InfoString &obj) : keyValuePairs(obj.keyValuePairs) {};
void set(const std::string& key, const std::string& value);
std::string get(const std::string& key);
std::string build();
void dump();

View File

@ -62,25 +62,18 @@ namespace Utils
return str;
}
std::vector<std::string> Explode(const std::string& str, char delim)
std::vector<std::string> Split(const std::string& str, const char delim)
{
std::vector<std::string> result;
std::istringstream iss(str);
std::stringstream ss(str);
std::string item;
std::vector<std::string> elems;
for (std::string token; std::getline(iss, token, delim);)
while (std::getline(ss, item, delim))
{
std::string _entry = std::move(token);
// Remove trailing 0x0 bytes
while (_entry.size() && !_entry.back())
{
_entry = _entry.substr(0, _entry.size() - 1);
}
result.push_back(_entry);
elems.push_back(item); // elems.push_back(std::move(item)); // if C++11 (based on comment from S1x)
}
return result;
return elems;
}
void Replace(std::string &string, const std::string& find, const std::string& replace)

View File

@ -78,7 +78,7 @@ namespace Utils
std::string ToLower(std::string input);
std::string ToUpper(std::string input);
bool EndsWith(const std::string& haystack, const std::string& needle);
std::vector<std::string> Explode(const std::string& str, char delim);
std::vector<std::string> Split(const std::string& str, const char delim);
void Replace(std::string &string, const std::string& find, const std::string& replace);
bool StartsWith(const std::string& haystack, const std::string& needle);
std::string &LTrim(std::string &s);