]> granicus.if.org Git - taglib/commitdiff
Reduce duplicate code between String::ctor() and operator=().
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Wed, 18 Nov 2015 05:03:45 +0000 (14:03 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Wed, 18 Nov 2015 05:03:45 +0000 (14:03 +0900)
It's safer to have only one pair of ref()/deref() than to have a several pairs.

taglib/toolkit/tstring.cpp
taglib/toolkit/tstring.h

index 024744a3ac4508efcc3d9f2747e19327824baf94..3255f564cf575a45ea6ff2345eeab7592230b818 100644 (file)
@@ -234,12 +234,6 @@ public:
   {
   }
 
-  StringPrivate(const wstring &s)
-    : RefCounter()
-    , data(s)
-  {
-  }
-
   StringPrivate(uint n, wchar_t c)
     : RefCounter()
     , data(static_cast<size_t>(n), c)
@@ -746,90 +740,62 @@ String &String::operator+=(char c)
 
 String &String::operator=(const String &s)
 {
-  if(&s == this)
-    return *this;
-
-  if(d->deref())
-    delete d;
-  d = s.d;
-  d->ref();
+  String(s).swap(*this);
   return *this;
 }
 
 String &String::operator=(const std::string &s)
 {
-  if(d->deref())
-    delete d;
-
-  d = new StringPrivate;
-  copyFromLatin1(d->data, s.c_str(), s.length());
-
+  String(s).swap(*this);
   return *this;
 }
 
 String &String::operator=(const wstring &s)
 {
-  if(d->deref())
-    delete d;
-  d = new StringPrivate(s);
+  String(s).swap(*this);
   return *this;
 }
 
 String &String::operator=(const wchar_t *s)
 {
-  if(d->deref())
-    delete d;
-
-  d = new StringPrivate(s);
+  String(s).swap(*this);
   return *this;
 }
 
 String &String::operator=(char c)
 {
-  if(d->deref())
-    delete d;
-
-  d = new StringPrivate(1, static_cast<uchar>(c));
+  String(c).swap(*this);
   return *this;
 }
 
 String &String::operator=(wchar_t c)
 {
-  if(d->deref())
-    delete d;
-
-  d = new StringPrivate(1, c);
+  String(c, wcharByteOrder()).swap(*this);
   return *this;
 }
 
 String &String::operator=(const char *s)
 {
-  if(d->deref())
-    delete d;
-
-  d = new StringPrivate;
-  copyFromLatin1(d->data, s, ::strlen(s));
-
+  String(s).swap(*this);
   return *this;
 }
 
 String &String::operator=(const ByteVector &v)
 {
-  if(d->deref())
-    delete d;
-
-  d = new StringPrivate;
-  copyFromLatin1(d->data, v.data(), v.size());
+  String(v).swap(*this);
+  return *this;
+}
 
-  // If we hit a null in the ByteVector, shrink the string again.
-  d->data.resize(::wcslen(d->data.c_str()));
+void String::swap(String &s)
+{
+  using std::swap;
 
-  return *this;
+  swap(d, s.d);
 }
 
 bool String::operator<(const String &s) const
 {
-  return d->data < s.d->data;
+  return (d->data < s.d->data);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -838,10 +804,8 @@ bool String::operator<(const String &s) const
 
 void String::detach()
 {
-  if(d->count() > 1) {
-    d->deref();
-    d = new StringPrivate(d->data);
-  }
+  if(d->count() > 1)
+    String(d->data.c_str()).swap(*this);
 }
 }
 
index 85acc5286bc6bcb83d6c96592f029f75b9e99a27..b7314bda97877362ad4b5616d9309277cc429567 100644 (file)
@@ -494,6 +494,11 @@ namespace TagLib {
      */
     String &operator=(const ByteVector &v);
 
+    /*!
+     * Exchanges the content of the String by the content of \a s.
+     */
+    void swap(String &s);
+
     /*!
      * To be able to use this class in a Map, this operator needed to be
      * implemented.  Returns true if \a s is less than this string in a byte-wise