Browse Source

Clean up some useless junk from plString

Michael Hansen 12 years ago
parent
commit
02eebfa9a1
  1. 57
      Sources/Plasma/CoreLib/plString.cpp
  2. 109
      Sources/Plasma/CoreLib/plString.h

57
Sources/Plasma/CoreLib/plString.cpp

@ -163,7 +163,7 @@ void plString::IConvertFromUtf16(const uint16_t *utf16, size_t size)
while (sp < utf16 + size) { while (sp < utf16 + size) {
if (*sp >= 0xD800 && *sp <= 0xDFFF) { if (*sp >= 0xD800 && *sp <= 0xDFFF) {
// Surrogate pair // Surrogate pair
unsigned int unichar = 0x10000; UniChar unichar = 0x10000;
if (sp + 1 >= utf16 + size) { if (sp + 1 >= utf16 + size) {
hsAssert(0, "Incomplete surrogate pair in UTF-16 data"); hsAssert(0, "Incomplete surrogate pair in UTF-16 data");
@ -332,7 +332,7 @@ plStringBuffer<uint16_t> plString::ToUtf16() const
uint16_t *dp = ustr; uint16_t *dp = ustr;
sp = utf8; sp = utf8;
while (sp < utf8 + srcSize) { while (sp < utf8 + srcSize) {
unsigned int unichar; UniChar unichar;
if ((*sp & 0xF8) == 0xF0) { if ((*sp & 0xF8) == 0xF0) {
unichar = (*sp++ & 0x07) << 18; unichar = (*sp++ & 0x07) << 18;
unichar |= (*sp++ & 0x3F) << 12; unichar |= (*sp++ & 0x3F) << 12;
@ -366,11 +366,18 @@ plStringBuffer<wchar_t> plString::ToWchar() const
plStringBuffer<uint16_t> utf16 = ToUtf16(); plStringBuffer<uint16_t> utf16 = ToUtf16();
return *reinterpret_cast<plStringBuffer<wchar_t>*>(&utf16); return *reinterpret_cast<plStringBuffer<wchar_t>*>(&utf16);
#else #else
plStringBuffer<wchar_t> result; plUnicodeBuffer utf32 = GetUnicodeArray();
return *reinterpret_cast<plStringBuffer<wchar_t>*)(&utf32);
#endif
}
plStringBuffer<char> plString::ToIso8859_1() const
{
plStringBuffer<char> result;
if (IsEmpty()) if (IsEmpty())
return result; return result;
// Calculate the UCS-4 size // Calculate the ASCII size
size_t convlen = 0; size_t convlen = 0;
const char *utf8 = fUtf8Buffer.GetData(); const char *utf8 = fUtf8Buffer.GetData();
const char *sp = utf8; const char *sp = utf8;
@ -388,11 +395,11 @@ plStringBuffer<wchar_t> plString::ToWchar() const
} }
// And perform the actual conversion // And perform the actual conversion
wchar_t *wstr = result.CreateWritableBuffer(convlen); char *astr = result.CreateWritableBuffer(convlen);
wchar_t *dp = wstr; char *dp = astr;
sp = utf8; sp = utf8;
while (sp < utf8 + srcSize) { while (sp < utf8 + srcSize) {
unsigned int unichar; UniChar unichar;
if ((*sp & 0xF8) == 0xF0) { if ((*sp & 0xF8) == 0xF0) {
unichar = (*sp++ & 0x07) << 18; unichar = (*sp++ & 0x07) << 18;
unichar |= (*sp++ & 0x3F) << 12; unichar |= (*sp++ & 0x3F) << 12;
@ -408,21 +415,20 @@ plStringBuffer<wchar_t> plString::ToWchar() const
} else { } else {
unichar = *sp++; unichar = *sp++;
} }
*dp++ = unichar; *dp++ = (unichar < 0xFF) ? unichar : '?';
} }
wstr[convlen] = 0; astr[convlen] = 0;
return result; return result;
#endif
} }
plStringBuffer<char> plString::ToIso8859_1() const plUnicodeBuffer plString::GetUnicodeArray() const
{ {
plStringBuffer<char> result; plUnicodeBuffer result;
if (IsEmpty()) if (IsEmpty())
return result; return result;
// Calculate the ASCII size // Calculate the UCS-4 size
size_t convlen = 0; size_t convlen = 0;
const char *utf8 = fUtf8Buffer.GetData(); const char *utf8 = fUtf8Buffer.GetData();
const char *sp = utf8; const char *sp = utf8;
@ -440,11 +446,11 @@ plStringBuffer<char> plString::ToIso8859_1() const
} }
// And perform the actual conversion // And perform the actual conversion
char *astr = result.CreateWritableBuffer(convlen); UniChar *ustr = result.CreateWritableBuffer(convlen);
char *dp = astr; UniChar *dp = ustr;
sp = utf8; sp = utf8;
while (sp < utf8 + srcSize) { while (sp < utf8 + srcSize) {
unsigned int unichar; UniChar unichar;
if ((*sp & 0xF8) == 0xF0) { if ((*sp & 0xF8) == 0xF0) {
unichar = (*sp++ & 0x07) << 18; unichar = (*sp++ & 0x07) << 18;
unichar |= (*sp++ & 0x3F) << 12; unichar |= (*sp++ & 0x3F) << 12;
@ -460,25 +466,8 @@ plStringBuffer<char> plString::ToIso8859_1() const
} else { } else {
unichar = *sp++; unichar = *sp++;
} }
*dp++ = (unichar < 0xFF) ? unichar : '?'; *dp++ = unichar;
}
astr[convlen] = 0;
return result;
} }
plUnicodeBuffer plString::GetUnicodeArray() const
{
plUnicodeBuffer result;
if (IsEmpty())
return result;
size_t convlen = GetUniCharCount();
UniChar *ustr = result.CreateWritableBuffer(convlen);
iterator iter = GetIterator();
size_t dp = 0;
while (!iter.AtEnd())
ustr[dp++] = *iter++;
ustr[convlen] = 0; ustr[convlen] = 0;
return result; return result;

109
Sources/Plasma/CoreLib/plString.h

@ -324,115 +324,6 @@ public:
{ return _L.Compare(_R, kCaseInsensitive) == 0; } { return _L.Compare(_R, kCaseInsensitive) == 0; }
}; };
public:
struct iterator
{
iterator() : m_ptr(nil), m_end(nil) { }
iterator(const iterator &copy) : m_ptr(copy.m_ptr), m_end(copy.m_end) { }
iterator &operator=(const iterator &copy)
{ m_ptr = copy.m_ptr; m_end = copy.m_end; return *this; }
iterator &operator++()
{
if ((*m_ptr & 0xF8) == 0xF0)
m_ptr += 4;
else if ((*m_ptr & 0xF0) == 0xE0)
m_ptr += 3;
else if ((*m_ptr & 0xE0) == 0xC0)
m_ptr += 2;
else
m_ptr += 1;
return *this;
}
iterator operator++(int)
{
iterator iter_save = *this;
(void) operator++();
return iter_save;
}
iterator &operator+=(size_t delta)
{
while (delta) {
operator++();
--delta;
}
return *this;
}
iterator operator+(size_t delta) const
{
iterator copy(*this);
copy += delta;
return copy;
}
int operator-(const iterator &other) const
{
return (int)(m_ptr - other.m_ptr);
}
bool operator==(const iterator &other) const { return m_ptr == other.m_ptr; }
bool operator!=(const iterator &other) const { return m_ptr != other.m_ptr; }
bool operator<(const iterator &other) const { return m_ptr < other.m_ptr; }
bool operator>(const iterator &other) const { return m_ptr > other.m_ptr; }
bool operator<=(const iterator &other) const { return m_ptr <= other.m_ptr; }
bool operator>=(const iterator &other) const { return m_ptr >= other.m_ptr; }
UniChar operator*() const
{
UniChar ch;
if ((*m_ptr & 0xF8) == 0xF0) {
ch = (m_ptr[0] & 0x07) << 18;
ch |= (m_ptr[1] & 0x3F) << 12;
ch |= (m_ptr[2] & 0x3F) << 6;
ch |= (m_ptr[3] & 0x3F);
} else if ((*m_ptr & 0xF0) == 0xE0) {
ch = (m_ptr[0] & 0x0F) << 12;
ch |= (m_ptr[1] & 0x3F) << 6;
ch |= (m_ptr[2] & 0x3F);
} else if ((*m_ptr & 0xE0) == 0xC0) {
ch = (m_ptr[0] & 0x1F) << 6;
ch |= (m_ptr[1] & 0x3F);
} else {
ch = m_ptr[0];
}
return ch;
}
UniChar operator[](size_t offset) const
{
iterator copy(*this);
copy += offset;
return *copy;
}
bool AtEnd() const { return m_ptr >= m_end; }
bool IsValid() const { return m_ptr != 0; }
private:
friend class plString;
iterator(const char *ptr, size_t size) : m_ptr(ptr), m_end(ptr + size) { }
const char *m_ptr;
const char *m_end;
};
iterator GetIterator() const { return iterator(c_str(), GetSize()); }
size_t GetUniCharCount() const
{
iterator iter = GetIterator();
size_t count = 0;
while (!iter.AtEnd()) {
++iter;
++count;
}
return count;
}
private: private:
friend plString operator+(const plString &left, const plString &right); friend plString operator+(const plString &left, const plString &right);
friend plString operator+(const plString &left, const char *right); friend plString operator+(const plString &left, const char *right);

Loading…
Cancel
Save