{
}
- StringPrivate(const wstring &s)
- : RefCounter()
- , data(s)
- {
- }
-
StringPrivate(uint n, wchar_t c)
: RefCounter()
, data(static_cast<size_t>(n), 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);
}
////////////////////////////////////////////////////////////////////////////////
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);
}
}