]> git.saurik.com Git - wxWidgets.git/blobdiff - include/wx/string.h
normalize printf/scanf format strings correctly on all platforms, while accounting...
[wxWidgets.git] / include / wx / string.h
index 4edb8d1a39cf20566ed40e508103e1f0ae302315..7cccebcad3b9ef04c1002ef088fbb8a79022d4b8 100644 (file)
@@ -51,7 +51,7 @@
     #include <StringMgr.h>
 #endif
 
-#include "wx/wxcrt.h"       // for wxChar, wxStrlen() etc.
+#include "wx/wxcrtbase.h"   // for wxChar, wxStrlen() etc.
 #include "wx/strvararg.h"
 #include "wx/buffer.h"      // for wxCharBuffer
 #include "wx/strconv.h"     // for wxConvertXXX() macros and wxMBConv classes
@@ -390,6 +390,10 @@ struct WXDLLIMPEXP_BASE wxStringIteratorNode
     wxStringImpl::const_iterator *m_citer;
     wxStringImpl::iterator *m_iter;
     wxStringIteratorNode *m_prev, *m_next;
+
+    // the node belongs to a particular iterator instance, it's not copied
+    // when a copy of the iterator is made
+    DECLARE_NO_COPY_CLASS(wxStringIteratorNode)
 };
 #endif // wxUSE_UNICODE_UTF8
 
@@ -639,7 +643,7 @@ public:
       private:                                                              \
           underlying_iterator m_cur
 
-  class const_iterator;
+  class WXDLLIMPEXP_BASE const_iterator;
 
 #if wxUSE_UNICODE_UTF8
   // NB: In UTF-8 build, (non-const) iterator needs to keep reference
@@ -650,15 +654,21 @@ public:
   //
   //     Furthermore, the replace() call may invalid all iterators for the
   //     string, so we have to keep track of outstanding iterators and update
-  //     them if it happens.
+  //     them if replace() happens.
+  //
+  //     This is implemented by maintaining linked list of iterators for every
+  //     string and traversing it in wxUniCharRef::operator=(). Head of the
+  //     list is stored in wxString. (FIXME-UTF8)
 
-  class iterator
+  class WXDLLIMPEXP_BASE iterator
   {
       WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
 
   public:
       iterator(const iterator& i)
           : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
+      iterator& operator=(const iterator& i)
+        { m_cur = i.m_cur; return *this; }
 
       reference operator*()
         { return wxUniCharRef::CreateForString(m_node, m_cur); }
@@ -675,8 +685,6 @@ public:
   private:
       iterator(wxString *str, underlying_iterator ptr)
           : m_cur(ptr), m_node(str, &m_cur) {}
-      iterator(wxString& str, underlying_iterator ptr)
-          : m_cur(ptr), m_node(&str, &m_cur) {}
 
       wxString* str() const { return wx_const_cast(wxString*, m_node.m_str); }
 
@@ -685,7 +693,7 @@ public:
       friend class const_iterator;
   };
 
-  class const_iterator
+  class WXDLLIMPEXP_BASE const_iterator
   {
       // NB: reference_type is intentionally value, not reference, the character
       //     may be encoded differently in wxString data:
@@ -697,6 +705,11 @@ public:
       const_iterator(const iterator& i)
           : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
 
+      const_iterator& operator=(const const_iterator& i)
+        { m_cur = i.m_cur; return *this; }
+      const_iterator& operator=(const iterator& i)
+        { m_cur = i.m_cur; return *this; }
+
       reference operator*() const
         { return wxStringOperations::DecodeChar(m_cur); }
 
@@ -713,8 +726,6 @@ public:
       // for internal wxString use only:
       const_iterator(const wxString *str, underlying_iterator ptr)
           : m_cur(ptr), m_node(str, &m_cur) {}
-      const_iterator(const wxString& str, underlying_iterator ptr)
-          : m_cur(ptr), m_node(&str, &m_cur) {}
 
       const wxString* str() const { return m_node.m_str; }
 
@@ -726,7 +737,7 @@ public:
 
 #else // !wxUSE_UNICODE_UTF8
 
-  class iterator
+  class WXDLLIMPEXP_BASE iterator
   {
       WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
 
@@ -753,7 +764,7 @@ public:
       friend class const_iterator;
   };
 
-  class const_iterator
+  class WXDLLIMPEXP_BASE const_iterator
   {
       // NB: reference_type is intentionally value, not reference, the character
       //     may be encoded differently in wxString data:
@@ -1191,24 +1202,11 @@ public:
     const wxStringCharType *wx_str() const { return m_impl.c_str(); }
 
     // conversion to *non-const* multibyte or widestring buffer; modifying
-    // returned buffer may or may not affect the string, these methods are only
-    // useful for passing values to const-incorrect functions
+    // returned buffer won't affect the string, these methods are only useful
+    // for passing values to const-incorrect functions
     wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const
-    {
-#if wxUSE_UNICODE
-        return mb_str(conv);
-#else
-        return wxWritableCharBuffer::CreateNonOwned(mb_str(conv));
-#endif
-    }
-    wxWritableWCharBuffer wchar_str() const
-    {
-#if wxUSE_UNICODE_WCHAR
-        return wxWritableWCharBuffer::CreateNonOwned(wc_str());
-#else
-        return wc_str();
-#endif
-    }
+        { return mb_str(conv); }
+    wxWritableWCharBuffer wchar_str() const { return wc_str(); }
 
     // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
     // converting numbers or strings which are certain not to contain special
@@ -1230,11 +1228,19 @@ public:
 #if wxUSE_UNICODE_UTF8
     static wxString FromUTF8(const char *utf8)
     {
+      if ( !utf8 )
+          return wxEmptyString;
+
       wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
       return FromImpl(wxStringImpl(utf8));
     }
     static wxString FromUTF8(const char *utf8, size_t len)
     {
+      if ( !utf8 )
+          return wxEmptyString;
+      if ( len == npos )
+          return FromUTF8(utf8);
+
       wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
       return FromImpl(wxStringImpl(utf8, len));
     }
@@ -1533,11 +1539,7 @@ public:
   bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const
     { return IsSameAs(str.data(), compareWithCase); }
     // comparison with a single character: returns true if equal
-  bool IsSameAs(wxUniChar c, bool compareWithCase = true) const
-    {
-      return (length() == 1) && (compareWithCase ? GetChar(0u) == c
-                              : wxToupper(GetChar(0u)) == wxToupper(c));
-    }
+  bool IsSameAs(wxUniChar c, bool compareWithCase = true) const;
   // FIXME-UTF8: remove these overloads
   bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
     { return IsSameAs(wxUniChar(c), compareWithCase); }
@@ -2547,6 +2549,12 @@ private:
 private:
   wxStringImpl m_impl;
 
+#ifdef __VISUALC__
+    // "struct 'ConvertedBuffer<T>' needs to have dll-interface to be used by
+    // clients of class 'wxString'" - this is private, we don't care
+    #pragma warning (disable:4251)
+#endif
+
   // buffers for compatibility conversion from (char*)c_str() and
   // (wchar_t*)c_str():
   // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
@@ -2575,6 +2583,10 @@ private:
   ConvertedBuffer<wchar_t> m_convertedToWChar;
 #endif
 
+#ifdef __VISUALC__
+    #pragma warning (default:4251)
+#endif
+
 #if wxUSE_UNICODE_UTF8
   // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
   //             assigning to character pointer to by wxString::interator may
@@ -2584,6 +2596,10 @@ private:
   {
       wxStringIteratorNodeHead() : ptr(NULL) {}
       wxStringIteratorNode *ptr;
+
+      // copying is disallowed as it would result in more than one pointer into
+      // the same linked list
+      DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead)
   };
 
   wxStringIteratorNodeHead m_iterators;
@@ -2853,7 +2869,15 @@ inline bool operator<=(const wxString& s1, const wxString& s2)
 inline bool operator>=(const wxString& s1, const wxString& s2)
     { return s1.Cmp(s2) >= 0; }
 
-#if wxUSE_UNICODE
+inline bool operator==(const wxString& s1, const wxCStrData& s2)
+    { return s1 == s2.AsString(); }
+inline bool operator==(const wxCStrData& s1, const wxString& s2)
+    { return s1.AsString() == s2; }
+inline bool operator!=(const wxString& s1, const wxCStrData& s2)
+    { return s1 != s2.AsString(); }
+inline bool operator!=(const wxCStrData& s1, const wxString& s2)
+    { return s1.AsString() != s2; }
+
 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
     { return (s1.Cmp((const wchar_t *)s2) == 0); }
 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
@@ -2862,7 +2886,7 @@ inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
     { return (s1.Cmp((const wchar_t *)s2) != 0); }
 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
     { return (s2.Cmp((const wchar_t *)s1) != 0); }
-#else // !wxUSE_UNICODE
+
 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
     { return (s1.Cmp((const char *)s2) == 0); }
 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
@@ -2871,19 +2895,16 @@ inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
     { return (s1.Cmp((const char *)s2) != 0); }
 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
     { return (s2.Cmp((const char *)s1) != 0); }
-#endif // wxUSE_UNICODE/!wxUSE_UNICODE
 
-#if wxUSE_UNICODE
 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
     { return string + (const wchar_t *)buf; }
 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
     { return (const wchar_t *)buf + string; }
-#else // !wxUSE_UNICODE
+
 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
     { return string + (const char *)buf; }
 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
     { return (const char *)buf + string; }
-#endif // wxUSE_UNICODE/!wxUSE_UNICODE
 
 // comparison with char
 inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
@@ -3116,11 +3137,11 @@ wxStringIteratorNode::~wxStringIteratorNode()
 #endif // wxUSE_UNICODE_UTF8
 
 #if WXWIN_COMPATIBILITY_2_8
-    // lot of code out there doesn't explicitly include wx/wxchar.h, but uses
+    // lot of code out there doesn't explicitly include wx/crt.h, but uses
     // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
     // so let's include this header now that wxString is defined and it's safe
     // to do it:
-    #include "wx/wxchar.h"
+    #include "wx/crt.h"
 #endif
 
 #endif  // _WX_WXSTRING_H_