summaryrefslogtreecommitdiff
path: root/src/strings
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2014-10-19 22:22:08 -0700
committerBen Longbons <b.r.longbons@gmail.com>2014-10-26 14:21:48 -0700
commit6800761863dd45b6055768febc6ace6a20120dc7 (patch)
tree73b416ca6507d9bb4f950252d55ead8e8cda34b5 /src/strings
parent0edf563dfc14a2b9db33a92f0eced28950bdf1aa (diff)
downloadtmwa-6800761863dd45b6055768febc6ace6a20120dc7.tar.gz
tmwa-6800761863dd45b6055768febc6ace6a20120dc7.tar.bz2
tmwa-6800761863dd45b6055768febc6ace6a20120dc7.tar.xz
tmwa-6800761863dd45b6055768febc6ace6a20120dc7.zip
New ast module for for npc parsing
Will eventually put most/all parsers there.
Diffstat (limited to 'src/strings')
-rw-r--r--src/strings/rstring.cpp19
-rw-r--r--src/strings/strings2_test.cpp9
2 files changed, 21 insertions, 7 deletions
diff --git a/src/strings/rstring.cpp b/src/strings/rstring.cpp
index e74d1d5..5675935 100644
--- a/src/strings/rstring.cpp
+++ b/src/strings/rstring.cpp
@@ -58,13 +58,18 @@ namespace strings
}
RString& RString::operator = (const RString& r)
{
- // order important for self-assign
- if (!r.maybe_end)
- r.u.owned->count++;
- if (!maybe_end && !u.owned->count--)
- ::operator delete(u.owned);
- u = r.u;
- maybe_end = r.maybe_end;
+ // this turns out to be a win
+ // certain callers end up needing to do self-assignment a *lot*,
+ // leading to pointless ++,--s
+ if (this->u.owned != r.u.owned)
+ {
+ if (!r.maybe_end)
+ r.u.owned->count++;
+ if (!maybe_end && !u.owned->count--)
+ ::operator delete(u.owned);
+ u = r.u;
+ maybe_end = r.maybe_end;
+ }
return *this;
}
RString& RString::operator = (RString&& r)
diff --git a/src/strings/strings2_test.cpp b/src/strings/strings2_test.cpp
index 8ac8482..8b91306 100644
--- a/src/strings/strings2_test.cpp
+++ b/src/strings/strings2_test.cpp
@@ -228,4 +228,13 @@ TEST(StringTests, rlong)
EXPECT_EQ(&*r.begin(), &*r3.begin());
EXPECT_EQ(&*a.begin(), &*a3.begin());
}
+
+TEST(StringTest, rself)
+{
+ // force dynamic allocation; valgrind will check for memory errors
+ RString r = XString("foo bar baz"_s);
+ RString r2 = r;
+ r = r;
+ r = r2;
+}
} // namespace tmwa