Files
Gleb Mazovetskiy b8ab6d2faa Migrate from SimpleIni to our own implementation
Our implementation has a more modern interface and only
supports the features that we care about.

It always outputs `\n` as newlines and does not output BOM.

The modern interface eliminates awkward `c_str()/data()` conversions.

This implementation preserves comments and the file order of sections
and keys. New keys are written in insertion order.

We now also support modifying and adding default comments,
which may be a useful thing to do for the especially tricky
ini options (this PR doesn't add any but adds the ability to do so).

Sadly, this increases the RG99 binary size by 24 KiB.
I'm guessing this is because the map implementation generates
quite a bit of code.

Note that while it might seem that using `std::string` for every key and
value would do a lot of allocations, most of these strings are
small and thus benefit from Small String Optimization (= no allocations).
2024-11-05 10:12:24 +00:00

64 lines
1.1 KiB
C++

#pragma once
#include <string>
#include <string_view>
#include <utility>
#include <variant>
namespace devilution {
class StringOrView {
public:
StringOrView()
: rep_ { std::string_view {} }
{
}
StringOrView(const StringOrView &) = default;
StringOrView(StringOrView &&) noexcept = default;
StringOrView(std::string &&str)
: rep_ { std::move(str) }
{
}
StringOrView(std::string_view str)
: rep_ { str }
{
}
StringOrView &operator=(StringOrView &&) noexcept = default;
StringOrView &operator=(std::string &&value) noexcept
{
rep_ = std::move(value);
return *this;
}
StringOrView &operator=(std::string_view value) noexcept
{
rep_ = value;
return *this;
}
bool empty() const
{
return std::visit([](auto &&val) -> bool { return val.empty(); }, rep_);
}
std::string_view str() const
{
return std::visit([](auto &&val) -> std::string_view { return val; }, rep_);
}
operator std::string_view() const
{
return str();
}
private:
std::variant<std::string, std::string_view> rep_;
};
} // namespace devilution