1
/***************************************************************************
2
copyright : (C) 2002 - 2008 by Scott Wheeler
3
email : wheeler@kde.org
4
***************************************************************************/
6
/***************************************************************************
7
* This library is free software; you can redistribute it and/or modify *
8
* it under the terms of the GNU Lesser General Public License version *
9
* 2.1 as published by the Free Software Foundation. *
11
* This library is distributed in the hope that it will be useful, but *
12
* WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14
* Lesser General Public License for more details. *
16
* You should have received a copy of the GNU Lesser General Public *
17
* License along with this library; if not, write to the Free Software *
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
21
* Alternatively, this file is available under the Mozilla Public *
22
* License Version 1.1. You may obtain a copy of the License at *
23
* http://www.mozilla.org/MPL/ *
24
***************************************************************************/
36
inline unsigned short byteSwap(unsigned short x)
38
return ((x) >> 8) & 0xff | ((x) & 0xff) << 8;
41
inline unsigned short combine(unsigned char c1, unsigned char c2)
43
return (c1 << 8) | c2;
47
using namespace TagLib;
49
class String::StringPrivate : public RefCounter
52
StringPrivate(const wstring &s) :
68
* This is only used to hold the a pointer to the most recent value of
76
////////////////////////////////////////////////////////////////////////////////
80
d = new StringPrivate;
83
String::String(const String &s) : d(s.d)
88
String::String(const std::string &s, Type t)
90
d = new StringPrivate;
92
if(t == UTF16 || t == UTF16BE || t == UTF16LE) {
93
debug("String::String() -- A std::string should not contain UTF16.");
97
int length = s.length();
98
d->data.resize(length);
99
wstring::iterator targetIt = d->data.begin();
101
for(std::string::const_iterator it = s.begin(); it != s.end(); it++) {
102
*targetIt = uchar(*it);
109
String::String(const wstring &s, Type t)
111
d = new StringPrivate(s);
115
String::String(const wchar_t *s, Type t)
117
d = new StringPrivate(s);
121
String::String(const char *s, Type t)
123
d = new StringPrivate;
125
if(t == UTF16 || t == UTF16BE || t == UTF16LE) {
126
debug("String::String() -- A const char * should not contain UTF16.");
130
int length = ::strlen(s);
131
d->data.resize(length);
133
wstring::iterator targetIt = d->data.begin();
135
for(int i = 0; i < length; i++) {
136
*targetIt = uchar(s[i]);
143
String::String(wchar_t c, Type t)
145
d = new StringPrivate;
150
String::String(char c, Type t)
152
d = new StringPrivate;
154
if(t == UTF16 || t == UTF16BE || t == UTF16LE) {
155
debug("String::String() -- A std::string should not contain UTF16.");
163
String::String(const ByteVector &v, Type t)
165
d = new StringPrivate;
170
if(t == Latin1 || t == UTF8) {
173
d->data.resize(v.size());
174
wstring::iterator targetIt = d->data.begin();
175
for(ByteVector::ConstIterator it = v.begin(); it != v.end() && (*it); ++it) {
176
*targetIt = uchar(*it);
180
d->data.resize(length);
183
d->data.resize(v.size() / 2);
184
wstring::iterator targetIt = d->data.begin();
186
for(ByteVector::ConstIterator it = v.begin();
187
it != v.end() && it + 1 != v.end() && combine(*it, *(it + 1));
190
*targetIt = combine(*it, *(it + 1));
197
////////////////////////////////////////////////////////////////////////////////
205
std::string String::to8Bit(bool unicode) const
208
s.resize(d->data.size());
211
std::string::iterator targetIt = s.begin();
212
for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) {
213
*targetIt = char(*it);
219
const int outputBufferSize = d->data.size() * 3 + 1;
221
Unicode::UTF16 *sourceBuffer = new Unicode::UTF16[d->data.size() + 1];
222
Unicode::UTF8 *targetBuffer = new Unicode::UTF8[outputBufferSize];
224
for(unsigned int i = 0; i < d->data.size(); i++)
225
sourceBuffer[i] = Unicode::UTF16(d->data[i]);
227
const Unicode::UTF16 *source = sourceBuffer;
228
Unicode::UTF8 *target = targetBuffer;
230
Unicode::ConversionResult result =
231
Unicode::ConvertUTF16toUTF8(&source, sourceBuffer + d->data.size(),
232
&target, targetBuffer + outputBufferSize,
233
Unicode::lenientConversion);
235
if(result != Unicode::conversionOK)
236
debug("String::to8Bit() - Unicode conversion error.");
238
int newSize = target - targetBuffer;
240
targetBuffer[newSize] = 0;
242
s = (char *) targetBuffer;
244
delete [] sourceBuffer;
245
delete [] targetBuffer;
250
TagLib::wstring String::toWString() const
255
const char *String::toCString(bool unicode) const
257
delete [] d->CString;
259
std::string buffer = to8Bit(unicode);
260
d->CString = new char[buffer.size() + 1];
261
strcpy(d->CString, buffer.c_str());
266
String::Iterator String::begin()
268
return d->data.begin();
271
String::ConstIterator String::begin() const
273
return d->data.begin();
276
String::Iterator String::end()
278
return d->data.end();
281
String::ConstIterator String::end() const
283
return d->data.end();
286
int String::find(const String &s, int offset) const
288
wstring::size_type position = d->data.find(s.d->data, offset);
290
if(position != wstring::npos)
296
bool String::startsWith(const String &s) const
298
if(s.length() > length())
301
return substr(0, s.length()) == s;
304
String String::substr(uint position, uint n) const
306
if(n > position + d->data.size())
307
n = d->data.size() - position;
310
s.d->data = d->data.substr(position, n);
314
String &String::append(const String &s)
317
d->data += s.d->data;
321
String String::upper() const
325
static int shift = 'A' - 'a';
327
for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); ++it) {
328
if(*it >= 'a' && *it <= 'z')
329
s.d->data.push_back(*it + shift);
331
s.d->data.push_back(*it);
337
TagLib::uint String::size() const
339
return d->data.size();
342
TagLib::uint String::length() const
347
bool String::isEmpty() const
349
return d->data.size() == 0;
352
bool String::isNull() const
357
ByteVector String::data(Type t) const
365
for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++)
371
std::string s = to8Bit(true);
372
v.setData(s.c_str(), s.length());
377
// Assume that if we're doing UTF16 and not UTF16BE that we want little
378
// endian encoding. (Byte Order Mark)
380
v.append(char(0xff));
381
v.append(char(0xfe));
383
for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) {
385
char c1 = *it & 0xff;
395
for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) {
398
char c2 = *it & 0xff;
407
for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) {
409
char c1 = *it & 0xff;
422
int String::toInt() const
426
bool negative = d->data[0] == '-';
427
uint i = negative ? 1 : 0;
429
for(; i < d->data.size() && d->data[i] >= '0' && d->data[i] <= '9'; i++)
430
value = value * 10 + (d->data[i] - '0');
438
String String::stripWhiteSpace() const
440
wstring::const_iterator begin = d->data.begin();
441
wstring::const_iterator end = d->data.end();
443
while(begin != end &&
444
(*begin == '\t' || *begin == '\n' || *begin == '\f' ||
445
*begin == '\r' || *begin == ' '))
453
// There must be at least one non-whitespace character here for us to have
454
// gotten this far, so we should be safe not doing bounds checking.
458
} while(*end == '\t' || *end == '\n' ||
459
*end == '\f' || *end == '\r' || *end == ' ');
461
return String(wstring(begin, end + 1));
464
bool String::isLatin1() const
466
for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) {
473
bool String::isAscii() const
475
for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) {
482
String String::number(int n) // static
489
bool negative = n < 0;
495
int remainder = n % 10;
496
charStack += char(remainder + '0');
497
n = (n - remainder) / 10;
505
for(int i = charStack.d->data.size() - 1; i >= 0; i--)
506
s += charStack.d->data[i];
511
TagLib::wchar &String::operator[](int i)
516
const TagLib::wchar &String::operator[](int i) const
521
bool String::operator==(const String &s) const
523
return d == s.d || d->data == s.d->data;
526
String &String::operator+=(const String &s)
530
d->data += s.d->data;
534
String &String::operator+=(const wchar_t *s)
542
String &String::operator+=(const char *s)
546
for(int i = 0; s[i] != 0; i++)
547
d->data += uchar(s[i]);
551
String &String::operator+=(wchar_t c)
559
String &String::operator+=(char c)
565
String &String::operator=(const String &s)
577
String &String::operator=(const std::string &s)
582
d = new StringPrivate;
584
d->data.resize(s.size());
586
wstring::iterator targetIt = d->data.begin();
587
for(std::string::const_iterator it = s.begin(); it != s.end(); it++) {
588
*targetIt = uchar(*it);
595
String &String::operator=(const wstring &s)
599
d = new StringPrivate(s);
603
String &String::operator=(const wchar_t *s)
607
d = new StringPrivate(s);
611
String &String::operator=(char c)
615
d = new StringPrivate;
620
String &String::operator=(wchar_t c)
624
d = new StringPrivate;
629
String &String::operator=(const char *s)
634
d = new StringPrivate;
636
int length = ::strlen(s);
637
d->data.resize(length);
639
wstring::iterator targetIt = d->data.begin();
640
for(int i = 0; i < length; i++) {
641
*targetIt = uchar(s[i]);
648
String &String::operator=(const ByteVector &v)
653
d = new StringPrivate;
654
d->data.resize(v.size());
655
wstring::iterator targetIt = d->data.begin();
659
for(ByteVector::ConstIterator it = v.begin(); it != v.end() && (*it); ++it) {
660
*targetIt = uchar(*it);
665
// If we hit a null in the ByteVector, shrink the string again.
672
bool String::operator<(const String &s) const
674
return d->data < s.d->data;
677
////////////////////////////////////////////////////////////////////////////////
679
////////////////////////////////////////////////////////////////////////////////
681
void String::detach()
685
d = new StringPrivate(d->data);
689
////////////////////////////////////////////////////////////////////////////////
691
////////////////////////////////////////////////////////////////////////////////
693
void String::prepare(Type t)
698
if(d->data.size() >= 1 && (d->data[0] == 0xfeff || d->data[0] == 0xfffe)) {
699
bool swap = d->data[0] != 0xfeff;
700
d->data.erase(d->data.begin(), d->data.begin() + 1);
702
for(uint i = 0; i < d->data.size(); i++)
703
d->data[i] = byteSwap((unsigned short)d->data[i]);
707
debug("String::prepare() - Invalid UTF16 string.");
708
d->data.erase(d->data.begin(), d->data.end());
714
int bufferSize = d->data.size() + 1;
715
Unicode::UTF8 *sourceBuffer = new Unicode::UTF8[bufferSize];
716
Unicode::UTF16 *targetBuffer = new Unicode::UTF16[bufferSize];
719
for(; i < d->data.size(); i++)
720
sourceBuffer[i] = Unicode::UTF8(d->data[i]);
723
const Unicode::UTF8 *source = sourceBuffer;
724
Unicode::UTF16 *target = targetBuffer;
726
Unicode::ConversionResult result =
727
Unicode::ConvertUTF8toUTF16(&source, sourceBuffer + bufferSize,
728
&target, targetBuffer + bufferSize,
729
Unicode::lenientConversion);
731
if(result != Unicode::conversionOK)
732
debug("String::prepare() - Unicode conversion error.");
735
int newSize = target != targetBuffer ? target - targetBuffer - 1 : 0;
736
d->data.resize(newSize);
738
for(int i = 0; i < newSize; i++)
739
d->data[i] = targetBuffer[i];
741
delete [] sourceBuffer;
742
delete [] targetBuffer;
748
for(uint i = 0; i < d->data.size(); i++)
749
d->data[i] = byteSwap((unsigned short)d->data[i]);
757
////////////////////////////////////////////////////////////////////////////////
759
////////////////////////////////////////////////////////////////////////////////
761
const TagLib::String operator+(const TagLib::String &s1, const TagLib::String &s2)
768
const TagLib::String operator+(const char *s1, const TagLib::String &s2)
775
const TagLib::String operator+(const TagLib::String &s1, const char *s2)
782
std::ostream &operator<<(std::ostream &s, const String &str)