]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/string.cpp
Make wxRenderer::DrawItemSelectionRect() draw a focus outline of wxCONTROL_CURRENT...
[wxWidgets.git] / src / common / string.cpp
index d0a6a445691b1b242f75676a89aaeb1d821a2a10..9b05497c373daeb489defb129657e14f64556626 100644 (file)
@@ -72,11 +72,10 @@ const size_t wxString::npos = (size_t) -1;
 
 wxSTD ostream& operator<<(wxSTD ostream& os, const wxCStrData& str)
 {
 
 wxSTD ostream& operator<<(wxSTD ostream& os, const wxCStrData& str)
 {
-// FIXME-UTF8: always, not only if wxUSE_UNICODE
-#if wxUSE_UNICODE && !defined(__BORLANDC__)
-    return os << (const wchar_t*)str.AsWCharBuf();
+#if wxUSE_UNICODE && !wxUSE_UNICODE_UTF8
+    return os << (const char *)str.AsCharBuf();
 #else
 #else
-    return os << (const char*)str.AsCharBuf();
+    return os << str.AsInternal();
 #endif
 }
 
 #endif
 }
 
@@ -97,6 +96,25 @@ wxSTD ostream& operator<<(wxSTD ostream& os, const wxWCharBuffer& str)
 }
 #endif
 
 }
 #endif
 
+#if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
+
+wxSTD wostream& operator<<(wxSTD wostream& wos, const wxString& str)
+{
+    return wos << str.wc_str();
+}
+
+wxSTD wostream& operator<<(wxSTD wostream& wos, const wxCStrData& str)
+{
+    return wos << str.AsWChar();
+}
+
+wxSTD wostream& operator<<(wxSTD wostream& wos, const wxWCharBuffer& str)
+{
+    return wos << str.data();
+}
+
+#endif  // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
+
 #endif // wxUSE_STD_IOSTREAM
 
 // ===========================================================================
 #endif // wxUSE_STD_IOSTREAM
 
 // ===========================================================================
@@ -233,9 +251,35 @@ const char* wxCStrData::AsChar() const
     wxString *str = wxConstCast(m_str, wxString);
 
     // convert the string:
     wxString *str = wxConstCast(m_str, wxString);
 
     // convert the string:
+    //
+    // FIXME-UTF8: we'd like to do the conversion in the existing buffer (if we
+    //             have it) but it's unfortunately not obvious to implement
+    //             because we don't know how big buffer do we need for the
+    //             given string length (in case of multibyte encodings, e.g.
+    //             ISO-2022-JP or UTF-8 when internal representation is wchar_t)
+    //
+    //             One idea would be to store more than just m_convertedToChar
+    //             in wxString: then we could record the length of the string
+    //             which was converted the last time and try to reuse the same
+    //             buffer if the current length is not greater than it (this
+    //             could still fail because string could have been modified in
+    //             place but it would work most of the time, so we'd do it and
+    //             only allocate the new buffer if in-place conversion returned
+    //             an error). We could also store a bit saying if the string
+    //             was modified since the last conversion (and update it in all
+    //             operation modifying the string, of course) to avoid unneeded
+    //             consequential conversions. But both of these ideas require
+    //             adding more fields to wxString and require profiling results
+    //             to be sure that we really gain enough from them to justify
+    //             doing it.
     wxCharBuffer buf(str->mb_str());
 
     wxCharBuffer buf(str->mb_str());
 
-    // FIXME-UTF8: do the conversion in-place in the existing buffer
+    // if it failed, return empty string and not NULL to avoid crashes in code
+    // written with either wxWidgets 2 wxString or std::string behaviour in
+    // mind: neither of them ever returns NULL and so we shouldn't neither
+    if ( !buf )
+        return "";
+
     if ( str->m_convertedToChar &&
          strlen(buf) == strlen(str->m_convertedToChar) )
     {
     if ( str->m_convertedToChar &&
          strlen(buf) == strlen(str->m_convertedToChar) )
     {
@@ -261,6 +305,10 @@ const wchar_t* wxCStrData::AsWChar() const
     // convert the string:
     wxWCharBuffer buf(str->wc_str());
 
     // convert the string:
     wxWCharBuffer buf(str->wc_str());
 
+    // notice that here, unlike above in AsChar(), conversion can't fail as our
+    // internal UTF-8 is always well-formed -- or the string was corrupted and
+    // all bets are off anyhow
+
     // FIXME-UTF8: do the conversion in-place in the existing buffer
     if ( str->m_convertedToWChar &&
          wxWcslen(buf) == wxWcslen(str->m_convertedToWChar) )
     // FIXME-UTF8: do the conversion in-place in the existing buffer
     if ( str->m_convertedToWChar &&
          wxWcslen(buf) == wxWcslen(str->m_convertedToWChar) )
@@ -945,13 +993,12 @@ int wxString::CmpNoCase(const wxString& s) const
 {
     // FIXME-UTF8: use wxUniChar::ToLower/ToUpper once added
 
 {
     // FIXME-UTF8: use wxUniChar::ToLower/ToUpper once added
 
-    size_t idx = 0;
     const_iterator i1 = begin();
     const_iterator end1 = end();
     const_iterator i2 = s.begin();
     const_iterator end2 = s.end();
 
     const_iterator i1 = begin();
     const_iterator end1 = end();
     const_iterator i2 = s.begin();
     const_iterator end2 = s.end();
 
-    for ( ; i1 != end1 && i2 != end2; ++idx, ++i1, ++i2 )
+    for ( ; i1 != end1 && i2 != end2; ++i1, ++i2 )
     {
         wxUniChar lower1 = (wxChar)wxTolower(*i1);
         wxUniChar lower2 = (wxChar)wxTolower(*i2);
     {
         wxUniChar lower1 = (wxChar)wxTolower(*i1);
         wxUniChar lower2 = (wxChar)wxTolower(*i2);
@@ -1323,7 +1370,7 @@ wxString& wxString::Trim(bool bFromRight)
             // find last non-space character
             reverse_iterator psz = rbegin();
             while ( (psz != rend()) && wxSafeIsspace(*psz) )
             // find last non-space character
             reverse_iterator psz = rbegin();
             while ( (psz != rend()) && wxSafeIsspace(*psz) )
-                psz++;
+                ++psz;
 
             // truncate at trailing space start
             erase(psz.base(), end());
 
             // truncate at trailing space start
             erase(psz.base(), end());
@@ -1333,7 +1380,7 @@ wxString& wxString::Trim(bool bFromRight)
             // find first non-space character
             iterator psz = begin();
             while ( (psz != end()) && wxSafeIsspace(*psz) )
             // find first non-space character
             iterator psz = begin();
             while ( (psz != end()) && wxSafeIsspace(*psz) )
-                psz++;
+                ++psz;
 
             // fix up data and length
             erase(begin(), psz);
 
             // fix up data and length
             erase(begin(), psz);