diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2014-01-19 21:46:02 -0800 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2014-01-20 14:03:52 -0800 |
commit | 9c1799033d0c17fbefb52a9b2695922f5a715133 (patch) | |
tree | f2859d02f87081a9166e3253c4e7c4973b82fcea /src/strings/fstring.cpp | |
parent | b9ac1c6033a0b32ca9984f23223d9fc167415b10 (diff) | |
download | tmwa-9c1799033d0c17fbefb52a9b2695922f5a715133.tar.gz tmwa-9c1799033d0c17fbefb52a9b2695922f5a715133.tar.bz2 tmwa-9c1799033d0c17fbefb52a9b2695922f5a715133.tar.xz tmwa-9c1799033d0c17fbefb52a9b2695922f5a715133.zip |
Implement FString from scratch instead of hackishly
Diffstat (limited to 'src/strings/fstring.cpp')
-rw-r--r-- | src/strings/fstring.cpp | 52 |
1 files changed, 47 insertions, 5 deletions
diff --git a/src/strings/fstring.cpp b/src/strings/fstring.cpp index 32d19fe..97ef4b0 100644 --- a/src/strings/fstring.cpp +++ b/src/strings/fstring.cpp @@ -1,7 +1,7 @@ #include "fstring.hpp" // strings/fstring.cpp - Functions for fstring.hpp // -// Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com> +// Copyright © 2013-2014 Ben Longbons <b.r.longbons@gmail.com> // // This file is part of The Mana World (Athena server) // @@ -27,35 +27,77 @@ namespace strings { + uint8_t FString::empty_string_rep[sizeof(Rep) + 1]; + FString::FString() + : owned(reinterpret_cast<Rep *>(&empty_string_rep)) + { + owned->count++; + } + + FString::FString(const FString& r) + : owned(r.owned) + { + owned->count++; + } + FString::FString(FString&& r) + : owned(reinterpret_cast<Rep *>(&empty_string_rep)) + { + std::swap(owned, r.owned); + r.owned->count++; + } + FString& FString::operator = (const FString& r) + { + // order important for self-assign + r.owned->count++; + // owned can be NULL from ctors + // TODO do ctors *properly* (requires gcc 4.7 to stay sane) + if (owned && !owned->count--) + ::operator delete(owned); + owned = r.owned; + return *this; + } + FString& FString::operator = (FString&& r) + { + std::swap(owned, r.owned); + return *this; + } + FString::~FString() { - const char *sadness = ""; - _assign(sadness, sadness); + if (owned && !owned->count--) + ::operator delete(owned); + owned = nullptr; } FString::FString(const MString& s) + : owned(nullptr) { _assign(s.begin(), s.end()); } FString::FString(XPair p) + : owned(nullptr) { _assign(p.begin(), p.end()); } FString::FString(const TString& t) + : owned(nullptr) { *this = XString(t); } FString::FString(const SString& s) + : owned(nullptr) { *this = XString(s); } FString::FString(ZString z) + : owned(nullptr) { *this = XString(z); } FString::FString(XString x) + : owned(nullptr) { const FString *f = x.base(); const char *xb = &*x.begin(); @@ -70,11 +112,11 @@ namespace strings FString::iterator FString::begin() const { - return &_hack2->begin()[0]; + return owned->body; } FString::iterator FString::end() const { - return &_hack2->end()[-1]; + return owned->body + owned->size; } const FString *FString::base() const { |