t7x/src/common/utils/info_string.cpp

72 lines
1.5 KiB
C++
Raw Normal View History

2022-05-21 12:04:08 +02:00
#include "info_string.hpp"
#include "string.hpp"
namespace utils
{
info_string::info_string(const std::string& buffer)
{
this->parse(buffer);
}
info_string::info_string(const std::string_view& buffer)
: info_string(std::string{buffer})
{
}
2022-10-28 22:16:14 +02:00
info_string::info_string(const std::basic_string_view<uint8_t>& buffer)
: info_string(std::string_view(reinterpret_cast<const char*>(buffer.data()), buffer.size()))
{
}
2022-05-21 12:04:08 +02:00
void info_string::set(const std::string& key, const std::string& value)
{
this->key_value_pairs_[key] = value;
}
std::string info_string::get(const std::string& key) const
{
const auto value = this->key_value_pairs_.find(key);
if (value != this->key_value_pairs_.end())
{
return value->second;
}
return {};
2022-05-21 12:04:08 +02:00
}
void info_string::parse(std::string buffer)
{
if (buffer[0] == '\\')
{
buffer = buffer.substr(1);
}
2023-04-02 19:46:02 +02:00
const auto key_values = string::split(buffer, '\\');
2022-05-21 12:04:08 +02:00
for (size_t i = 0; !key_values.empty() && i < (key_values.size() - 1); i += 2)
{
const auto& key = key_values[i];
const auto& value = key_values[i + 1];
2023-04-02 19:46:02 +02:00
if (!this->key_value_pairs_.contains(key))
{
this->key_value_pairs_[key] = value;
}
2022-05-21 12:04:08 +02:00
}
}
std::string info_string::build() const
{
std::string info_string;
for (auto i = this->key_value_pairs_.begin(); i != this->key_value_pairs_.end(); ++i)
{
info_string.append("\\");
info_string.append(i->first); // Key
info_string.append("\\");
info_string.append(i->second); // Value
}
return info_string;
}
}