diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2014-04-20 12:16:32 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2014-04-21 00:26:20 +0100 |
| commit | 63a0b9d22a44954f861989df62e10222da84bfc6 (patch) | |
| tree | 482f9a5152733146b9021e0bf693fa8c38245feb /src/string.hh | |
| parent | c4295b7e30d5fe9527fd7f3a20bc12ccfca8b94c (diff) | |
Add a zstr() method to StringView that returns a zero terminated string holder
Diffstat (limited to 'src/string.hh')
| -rw-r--r-- | src/string.hh | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/string.hh b/src/string.hh index 5e0446e2..94429e28 100644 --- a/src/string.hh +++ b/src/string.hh @@ -104,6 +104,24 @@ public: 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: const char* m_data; ByteCount m_length; |
