X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9a6d14383a57771a334f6d0b6376dd47bf24049d..cc4d5638c66a409e421420ed7110917755a66788:/src/common/ustring.cpp?ds=sidebyside diff --git a/src/common/ustring.cpp b/src/common/ustring.cpp index cb7eb084c4..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 /////////////////////////////////////////////////////////////////////////////// @@ -15,23 +14,20 @@ #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++) { @@ -39,7 +35,7 @@ wxUString &wxUString::assignFromAscii( const char *str ) ptr++; str++; } - + return assign( buffer ); } @@ -52,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++) { @@ -63,7 +59,7 @@ wxUString &wxUString::assignFromAscii( const char *str, size_type n ) ptr++; str++; } - + return *this; } @@ -71,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): @@ -113,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) @@ -128,7 +121,7 @@ wxUString &wxUString::assignFromUTF8( const char *str ) wxU32CharBuffer buffer( ucs4_len ); wxChar32 *out = buffer.data(); - + p = str; while (*p) { @@ -181,7 +174,7 @@ wxUString &wxUString::assignFromUTF8( const char *str ) code <<= 6; code |= c & 0x3F; } - + *out = code; p++; } @@ -195,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; @@ -211,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) @@ -225,7 +218,7 @@ wxUString &wxUString::assignFromUTF8( const char *str, size_type n ) if (utf8_pos + 1 > n) break; utf8_pos++; - + *out = c; p++; } @@ -275,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() ); @@ -291,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; @@ -310,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; @@ -323,7 +316,7 @@ wxUString &wxUString::assignFromUTF16( const wxChar16* str, size_type n ) wxChar32 *out = buffer.data(); utf16_pos = 0; - + p = str; while (*p) { @@ -331,7 +324,7 @@ wxUString &wxUString::assignFromUTF16( const wxChar16* str, size_type n ) { if (utf16_pos + 1 > n) break; - + *out = *p; p++; utf16_pos++; @@ -340,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() ); } @@ -355,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) @@ -373,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) { @@ -396,7 +389,7 @@ wxUString &wxUString::assignFromUTF16( const wxChar16* str ) } out++; } - + return assign( buffer.data() ); } @@ -404,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 ); } @@ -414,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++; @@ -451,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; @@ -494,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; @@ -542,7 +532,6 @@ wxU16CharBuffer wxUString::utf16_str() const out += 2; } } - - return result; + + return result; } -