Add a zstr() method to StringView that returns a zero terminated string holder

This commit is contained in:
Maxime Coste 2014-04-20 12:16:32 +01:00
parent c4295b7e30
commit 63a0b9d22a

View File

@ -104,6 +104,24 @@ public:
operator String() const { return str(); } // to remove operator String() const { return str(); } // to remove
struct ZeroTerminatedString
{
ZeroTerminatedString(const char* begin, const char* end)
{
if (*end == '\0')
unowned = begin;
else
owned = std::string(begin, end);
}
operator const char*() const { return unowned ? unowned : owned.c_str(); }
private:
std::string owned;
const char* unowned = nullptr;
};
ZeroTerminatedString zstr() const { return ZeroTerminatedString{begin(), end()}; }
private: private:
const char* m_data; const char* m_data;
ByteCount m_length; ByteCount m_length;