// Purpose: wxUString class
// Author: Robert Roebling
// Created: 2008-07-25
-// RCS-ID: $Id:$
// Copyright: (c) 2008 Robert Roebling
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#pragma hdrstop
#endif
+#include "wx/ustring.h"
+
#ifndef WX_PRECOMP
- #include "wx/strconv.h" // wxConvLibc
+ #include "wx/crt.h"
#include "wx/log.h"
#endif
-#include "wx/ustring.h"
-#include "wx/unichar.h"
-#include "wx/string.h"
-
-
wxUString &wxUString::assignFromAscii( const char *str )
{
size_type len = wxStrlen( str );
-
+
wxU32CharBuffer buffer( len );
wxChar32 *ptr = buffer.data();
-
+
size_type i;
for (i = 0; i < len; i++)
{
ptr++;
str++;
}
-
+
return assign( buffer );
}
len++;
s++;
}
-
+
wxU32CharBuffer buffer( len );
wxChar32 *ptr = buffer.data();
-
+
size_type i;
for (i = 0; i < len; i++)
{
ptr++;
str++;
}
-
+
return *this;
}
// UTF-8
// ----------------------------------------------------------------------------
-static const wxUint32 utf8_max[]=
- { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
-
// this table gives the length of the UTF-8 encoding from its first character:
const unsigned char tableUtf8Lengths[256] = {
// single-byte sequences (ASCII):
{
if (!str)
return assign( wxUString() );
-
+
size_type ucs4_len = 0;
const char *p = str;
while (*p)
wxU32CharBuffer buffer( ucs4_len );
wxChar32 *out = buffer.data();
-
+
p = str;
while (*p)
{
code <<= 6;
code |= c & 0x3F;
}
-
+
*out = code;
p++;
}
{
if (!str)
return assign( wxUString() );
-
+
size_type ucs4_len = 0;
size_type utf8_pos = 0;
const char *p = str;
ucs4_len ++;
p += len;
}
-
+
wxU32CharBuffer buffer( ucs4_len );
wxChar32 *out = buffer.data();
-
+
utf8_pos = 0;
p = str;
while (*p)
if (utf8_pos + 1 > n)
break;
utf8_pos++;
-
+
*out = c;
p++;
}
code <<= 6;
code |= c & 0x3F;
}
-
+
*out = code;
p++;
}
out++;
}
-
+
*out = 0;
return assign( buffer.data() );
{
if (!str)
return assign( wxUString() );
-
+
size_type ucs4_len = 0;
size_type utf16_pos = 0;
const wxChar16 *p = str;
{
len = 2;
}
-
+
if (utf16_pos + len > n)
break;
-
+
ucs4_len++;
p += len;
utf16_pos += len;
wxChar32 *out = buffer.data();
utf16_pos = 0;
-
+
p = str;
while (*p)
{
{
if (utf16_pos + 1 > n)
break;
-
+
*out = *p;
p++;
utf16_pos++;
{
if (utf16_pos + 2 > n)
break;
-
+
*out = ((p[0] - 0xd7c0) << 10) + (p[1] - 0xdc00);
p += 2;
utf16_pos += 2;
}
out++;
}
-
+
return assign( buffer.data() );
}
{
if (!str)
return assign( wxUString() );
-
+
size_type ucs4_len = 0;
const wxChar16 *p = str;
while (*p)
{
len = 2;
}
-
+
ucs4_len++;
p += len;
}
wxU32CharBuffer buffer( ucs4_len );
wxChar32 *out = buffer.data();
-
+
p = str;
while (*p)
{
}
out++;
}
-
+
return assign( buffer.data() );
}
{
if (!str)
return assign( wxUString() );
-
- wxWCharBuffer buffer = wxConvLibc.cMB2WC( str );
-
+
+ wxScopedWCharBuffer buffer = wxConvLibc.cMB2WC( str );
+
return assign( buffer );
}
{
if (!str)
return assign( wxUString() );
-
- wxWCharBuffer buffer = conv.cMB2WC( str );
-
+
+ wxScopedWCharBuffer buffer = conv.cMB2WC( str );
+
return assign( buffer );
}
-wxCharBuffer wxUString::utf8_str() const
+wxScopedCharBuffer wxUString::utf8_str() const
{
size_type utf8_length = 0;
const wxChar32 *ptr = data();
-
+
while (*ptr)
{
wxChar32 code = *ptr;
ptr++;
-
+
if ( code <= 0x7F )
{
utf8_length++;
// invalid range, skip
}
}
-
+
wxCharBuffer result( utf8_length );
-
+
char *out = result.data();
-
+
ptr = data();
while (*ptr)
{
wxChar32 code = *ptr;
ptr++;
-
+
if ( code <= 0x7F )
{
out[0] = (char)code;
}
}
- wxPrintf( "utf8_str %s len %d\n", result, wxStrlen( result.data() ) );
- wxPrintf( "utf8_str %s len %d\n", result, wxStrlen( result.data() ) );
-
return result;
}
-
-wxU16CharBuffer wxUString::utf16_str() const
+
+wxScopedU16CharBuffer wxUString::utf16_str() const
{
size_type utf16_length = 0;
const wxChar32 *ptr = data();
-
+
while (*ptr)
{
wxChar32 code = *ptr;
ptr++;
-
+
// TODO: error range checks
-
+
if (code < 0x10000)
utf16_length++;
else
utf16_length += 2;
}
-
+
wxU16CharBuffer result( utf16_length );
wxChar16 *out = result.data();
-
+
ptr = data();
-
+
while (*ptr)
{
wxChar32 code = *ptr;
ptr++;
-
+
// TODO: error range checks
-
+
if (code < 0x10000)
{
out[0] = code;
out += 2;
}
}
-
- return result;
+
+ return result;
}
-