mirror of
https://github.com/taglib/taglib.git
synced 2025-05-27 21:20:26 -04:00
Reduce duplicate code between String::ctor() and operator=().
It's safer to have only one pair of ref()/deref() than to have a several pairs.
This commit is contained in:
parent
a533b9c665
commit
1caaa8a020
@ -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());
|
||||
|
||||
// If we hit a null in the ByteVector, shrink the string again.
|
||||
d->data.resize(::wcslen(d->data.c_str()));
|
||||
|
||||
String(v).swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void String::swap(String &s)
|
||||
{
|
||||
using std::swap;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user