]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/string.cpp
fix wrong clear selection (modified patch 1763916)
[wxWidgets.git] / src / common / string.cpp
index 8bb9f91edc6b2f0c8ba95d43ef9a5f451dff5766..d36f85bd5a2cb4259e40df65fdd87a9cc3bfb7f8 100644 (file)
@@ -340,7 +340,7 @@ wxString::SubstrBufFromMB wxString::ConvertStr(const char *psz, size_t nLength,
         return SubstrBufFromMB("", 0);
 
     // and then to UTF-8:
         return SubstrBufFromMB("", 0);
 
     // and then to UTF-8:
-    SubstrBufFromMB buf(ConvertStr(wcBuf, wcLen, wxMBConvUTF8()));
+    SubstrBufFromMB buf(ConvertStr(wcBuf, wcLen, wxMBConvStrictUTF8()));
     // widechar -> UTF-8 conversion isn't supposed to ever fail:
     wxASSERT_MSG( buf.data, _T("conversion to UTF-8 failed") );
 
     // widechar -> UTF-8 conversion isn't supposed to ever fail:
     wxASSERT_MSG( buf.data, _T("conversion to UTF-8 failed") );
 
@@ -382,9 +382,12 @@ const wxCharBuffer wxString::mb_str(const wxMBConv& conv) const
 
 const wxWCharBuffer wxString::wc_str() const
 {
 
 const wxWCharBuffer wxString::wc_str() const
 {
-    return wxMBConvUTF8().cMB2WC(m_impl.c_str(),
-                                 m_impl.length() + 1 /* size, not length */,
-                                 NULL);
+    return wxMBConvStrictUTF8().cMB2WC
+                                (
+                                    m_impl.c_str(),
+                                    m_impl.length() + 1, // size, not length
+                                    NULL
+                                );
 }
 
 const wxCharBuffer wxString::mb_str(const wxMBConv& conv) const
 }
 
 const wxCharBuffer wxString::mb_str(const wxMBConv& conv) const
@@ -395,10 +398,12 @@ const wxCharBuffer wxString::mb_str(const wxMBConv& conv) const
     // FIXME-UTF8: use wc_str() here once we have buffers with length
 
     size_t wcLen;
     // FIXME-UTF8: use wc_str() here once we have buffers with length
 
     size_t wcLen;
-    wxWCharBuffer wcBuf(
-            wxMBConvUTF8().cMB2WC(m_impl.c_str(),
-                                  m_impl.length() + 1 /* size, not length */,
-                                  &wcLen));
+    wxWCharBuffer wcBuf(wxMBConvStrictUTF8().cMB2WC
+                                             (
+                                                m_impl.c_str(),
+                                                m_impl.length() + 1, // size
+                                                &wcLen
+                                             ));
     if ( !wcLen )
         return wxCharBuffer("");
 
     if ( !wcLen )
         return wxCharBuffer("");
 
@@ -973,35 +978,35 @@ int wxString::CmpNoCase(const wxString& s) const
 #endif
 #endif
 
 #endif
 #endif
 
-wxString wxString::FromAscii(const char *ascii)
+wxString wxString::FromAscii(const char *ascii, size_t len)
 {
 {
-    if (!ascii)
+    if (!ascii || len == 0)
        return wxEmptyString;
 
        return wxEmptyString;
 
-    size_t len = strlen(ascii);
     wxString res;
 
     wxString res;
 
-    if ( len )
     {
         wxImplStringBuffer buf(res, len);
         wxStringCharType *dest = buf;
 
     {
         wxImplStringBuffer buf(res, len);
         wxStringCharType *dest = buf;
 
-        for ( ;; )
+        for ( ; len > 0; --len )
         {
             unsigned char c = (unsigned char)*ascii++;
             wxASSERT_MSG( c < 0x80,
                           _T("Non-ASCII value passed to FromAscii().") );
 
             *dest++ = (wchar_t)c;
         {
             unsigned char c = (unsigned char)*ascii++;
             wxASSERT_MSG( c < 0x80,
                           _T("Non-ASCII value passed to FromAscii().") );
 
             *dest++ = (wchar_t)c;
-
-            if ( c == '\0' )
-                break;
         }
     }
 
     return res;
 }
 
         }
     }
 
     return res;
 }
 
+wxString wxString::FromAscii(const char *ascii)
+{
+    return FromAscii(ascii, wxStrlen(ascii));
+}
+
 wxString wxString::FromAscii(const char ascii)
 {
     // What do we do with '\0' ?
 wxString wxString::FromAscii(const char ascii)
 {
     // What do we do with '\0' ?
@@ -1566,6 +1571,11 @@ static int DoStringPrintfV(wxString& str,
         if ( !buf )
         {
             // out of memory
         if ( !buf )
         {
             // out of memory
+
+            // in UTF-8 build, leaving uninitialized junk in the buffer
+            // could result in invalid non-empty UTF-8 string, so just
+            // reset the string to empty on failure:
+            buf[0] = '\0';
             return -1;
         }
 
             return -1;
         }
 
@@ -1594,6 +1604,7 @@ static int DoStringPrintfV(wxString& str,
             // we know that our own implementation of wxVsnprintf() returns -1
             // only for a format error - thus there's something wrong with
             // the user's format string
             // we know that our own implementation of wxVsnprintf() returns -1
             // only for a format error - thus there's something wrong with
             // the user's format string
+            buf[0] = '\0';
             return -1;
 #else // possibly using system version
             // assume it only returns error if there is not enough space, but
             return -1;
 #else // possibly using system version
             // assume it only returns error if there is not enough space, but