X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a99bcb5e5ed1ed59ea1975b6262aa602bd490539..931d6a47c32a5b4c283243cb553ce71ee2b535d5:/src/common/ustring.cpp diff --git a/src/common/ustring.cpp b/src/common/ustring.cpp index c0cae8f58d..ab70ce5ae2 100644 --- a/src/common/ustring.cpp +++ b/src/common/ustring.cpp @@ -3,7 +3,6 @@ // Purpose: wxUString class // Author: Robert Roebling // Created: 2008-07-25 -// RCS-ID: $Id:$ // Copyright: (c) 2008 Robert Roebling // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// @@ -25,10 +24,10 @@ 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++) { @@ -36,7 +35,7 @@ wxUString &wxUString::assignFromAscii( const char *str ) ptr++; str++; } - + return assign( buffer ); } @@ -49,10 +48,10 @@ wxUString &wxUString::assignFromAscii( const char *str, size_type n ) len++; s++; } - + wxU32CharBuffer buffer( len ); wxChar32 *ptr = buffer.data(); - + size_type i; for (i = 0; i < len; i++) { @@ -60,7 +59,7 @@ wxUString &wxUString::assignFromAscii( const char *str, size_type n ) ptr++; str++; } - + return *this; } @@ -68,9 +67,6 @@ wxUString &wxUString::assignFromAscii( const char *str, size_type n ) // 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): @@ -110,7 +106,7 @@ wxUString &wxUString::assignFromUTF8( const char *str ) { if (!str) return assign( wxUString() ); - + size_type ucs4_len = 0; const char *p = str; while (*p) @@ -125,7 +121,7 @@ wxUString &wxUString::assignFromUTF8( const char *str ) wxU32CharBuffer buffer( ucs4_len ); wxChar32 *out = buffer.data(); - + p = str; while (*p) { @@ -178,7 +174,7 @@ wxUString &wxUString::assignFromUTF8( const char *str ) code <<= 6; code |= c & 0x3F; } - + *out = code; p++; } @@ -192,7 +188,7 @@ wxUString &wxUString::assignFromUTF8( const char *str, size_type n ) { if (!str) return assign( wxUString() ); - + size_type ucs4_len = 0; size_type utf8_pos = 0; const char *p = str; @@ -208,10 +204,10 @@ wxUString &wxUString::assignFromUTF8( const char *str, size_type n ) ucs4_len ++; p += len; } - + wxU32CharBuffer buffer( ucs4_len ); wxChar32 *out = buffer.data(); - + utf8_pos = 0; p = str; while (*p) @@ -222,7 +218,7 @@ wxUString &wxUString::assignFromUTF8( const char *str, size_type n ) if (utf8_pos + 1 > n) break; utf8_pos++; - + *out = c; p++; } @@ -272,13 +268,13 @@ wxUString &wxUString::assignFromUTF8( const char *str, size_type n ) code <<= 6; code |= c & 0x3F; } - + *out = code; p++; } out++; } - + *out = 0; return assign( buffer.data() ); @@ -288,7 +284,7 @@ wxUString &wxUString::assignFromUTF16( const wxChar16* str, size_type n ) { if (!str) return assign( wxUString() ); - + size_type ucs4_len = 0; size_type utf16_pos = 0; const wxChar16 *p = str; @@ -307,10 +303,10 @@ wxUString &wxUString::assignFromUTF16( const wxChar16* str, size_type n ) { len = 2; } - + if (utf16_pos + len > n) break; - + ucs4_len++; p += len; utf16_pos += len; @@ -320,7 +316,7 @@ wxUString &wxUString::assignFromUTF16( const wxChar16* str, size_type n ) wxChar32 *out = buffer.data(); utf16_pos = 0; - + p = str; while (*p) { @@ -328,7 +324,7 @@ wxUString &wxUString::assignFromUTF16( const wxChar16* str, size_type n ) { if (utf16_pos + 1 > n) break; - + *out = *p; p++; utf16_pos++; @@ -337,14 +333,14 @@ wxUString &wxUString::assignFromUTF16( const wxChar16* str, size_type n ) { if (utf16_pos + 2 > n) break; - + *out = ((p[0] - 0xd7c0) << 10) + (p[1] - 0xdc00); p += 2; utf16_pos += 2; } out++; } - + return assign( buffer.data() ); } @@ -352,7 +348,7 @@ wxUString &wxUString::assignFromUTF16( const wxChar16* str ) { if (!str) return assign( wxUString() ); - + size_type ucs4_len = 0; const wxChar16 *p = str; while (*p) @@ -370,14 +366,14 @@ wxUString &wxUString::assignFromUTF16( const wxChar16* str ) { len = 2; } - + ucs4_len++; p += len; } wxU32CharBuffer buffer( ucs4_len ); wxChar32 *out = buffer.data(); - + p = str; while (*p) { @@ -393,7 +389,7 @@ wxUString &wxUString::assignFromUTF16( const wxChar16* str ) } out++; } - + return assign( buffer.data() ); } @@ -401,9 +397,9 @@ wxUString &wxUString::assignFromCString( const char* str ) { if (!str) return assign( wxUString() ); - - wxWCharBuffer buffer = wxConvLibc.cMB2WC( str ); - + + wxScopedWCharBuffer buffer = wxConvLibc.cMB2WC( str ); + return assign( buffer ); } @@ -411,22 +407,22 @@ wxUString &wxUString::assignFromCString( const char* str, const wxMBConv &conv ) { 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++; @@ -448,17 +444,17 @@ wxCharBuffer wxUString::utf8_str() const // 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; @@ -491,42 +487,39 @@ wxCharBuffer wxUString::utf8_str() const } } - 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; @@ -539,19 +532,6 @@ wxU16CharBuffer wxUString::utf16_str() const out += 2; } } - - return result; -} - -#if SIZEOF_WCHAR_T != 2 -template<> -wxCharTypeBuffer::Data -wxCharTypeBuffer::NullData(NULL); -#endif - -#if SIZEOF_WCHAR_T != 4 -template<> -wxCharTypeBuffer::Data -wxCharTypeBuffer::NullData(NULL); -#endif + return result; +}