From 9c1799033d0c17fbefb52a9b2695922f5a715133 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Sun, 19 Jan 2014 21:46:02 -0800 Subject: Implement FString from scratch instead of hackishly --- src/strings/fstring.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) (limited to 'src/strings/fstring.cpp') 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 +// Copyright © 2013-2014 Ben Longbons // // 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(&empty_string_rep)) + { + owned->count++; + } + + FString::FString(const FString& r) + : owned(r.owned) + { + owned->count++; + } + FString::FString(FString&& r) + : owned(reinterpret_cast(&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 { -- cgit v1.2.3-60-g2f50