]> git.saurik.com Git - wxWidgets.git/commitdiff
added overloads taking pairs of const char/wchar_t pointers for wxString methods...
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 5 Apr 2007 21:55:29 +0000 (21:55 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 5 Apr 2007 21:55:29 +0000 (21:55 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45265 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/string.h
include/wx/stringimpl.h
src/common/stringimpl.cpp
tests/strings/stdstrings.cpp

index 73f0bb036b7404b00340892b2d1567dc19096b01..dd56e13c7afdfca876af5dabaae9a41dfd60429a 100644 (file)
@@ -634,7 +634,7 @@ public:
           unsigned operator-(const iterator_name& i) const                  \
             { return wxString::DiffIters(m_cur, i.m_cur); }                 \
                                                                             \
-          bool operator==(const iterator_name&i) const                      \
+          bool operator==(const iterator_name& i) const                     \
             { return m_cur == i.m_cur; }                                    \
           bool operator!=(const iterator_name& i) const                     \
             { return m_cur != i.m_cur; }                                    \
@@ -1406,13 +1406,19 @@ public:
     // take nLen chars starting at nPos
   wxString(const wxString& str, size_t nPos, size_t nLen)
       : m_impl(str.m_impl, nPos, nLen) { }
-    // take all characters from pStart to pEnd
-  wxString(const void *pStart, const void *pEnd)
-      : m_impl((const wxChar*)pStart, (const wxChar*)pEnd) { }
+    // take all characters from first to last
   wxString(const_iterator first, const_iterator last)
       : m_impl(first, last) { }
-  wxString(iterator first, iterator last)
-      : m_impl(first, last) { }
+  wxString(const char *first, const char *last)
+  {
+      SubstrBufFromMB str(ImplStr(first, last - first));
+      m_impl.assign(str.data, str.len);
+  }
+  wxString(const wchar_t *first, const wchar_t *last)
+  {
+      SubstrBufFromWC str(ImplStr(first, last - first));
+      m_impl.assign(str.data, str.len);
+  }
 
   // lib.string.modifiers
     // append elements str[pos], ..., str[pos+n]
@@ -1459,6 +1465,10 @@ public:
     // append from first to last
   wxString& append(const_iterator first, const_iterator last)
     { m_impl.append(first, last); return *this; }
+  wxString& append(const char *first, const char *last)
+    { return append(first, last - first); }
+  wxString& append(const wchar_t *first, const wchar_t *last)
+    { return append(first, last - first); }
 
     // same as `this_string = str'
   wxString& assign(const wxString& str)
@@ -1517,6 +1527,10 @@ public:
     // assign from first to last
   wxString& assign(const_iterator first, const_iterator last)
     { m_impl.assign(first, last); return *this; }
+  wxString& assign(const char *first, const char *last)
+    { return assign(first, last - first); }
+  wxString& assign(const wchar_t *first, const wchar_t *last)
+    { return assign(first, last - first); }
 
     // string comparison
   int compare(const wxString& str) const;
@@ -1577,6 +1591,10 @@ public:
     { return iterator(m_impl.insert(it, EncodeChar(ch))); }
   void insert(iterator it, const_iterator first, const_iterator last)
     { m_impl.insert(it, first, last); }
+  void insert(iterator it, const char *first, const char *last)
+    { insert(it - begin(), first, last - first); }
+  void append(iterator it, const wchar_t *first, const wchar_t *last)
+    { insert(it - begin(), first, last - first); }
   void insert(iterator it, size_type n, wxUniChar ch)
   {
 #if wxUSE_UNICODE_UTF8
@@ -1595,6 +1613,7 @@ public:
     m_impl.erase(from, len);
     return *this;
   }
+    // delete characters from first up to last
   iterator erase(iterator first, iterator last)
     { return iterator(m_impl.erase(first, last)); }
   iterator erase(iterator first)
@@ -1718,6 +1737,12 @@ public:
   wxString& replace(iterator first, iterator last,
                     const_iterator first1, const_iterator last1)
     { m_impl.replace(first, last, first1, last1); return *this; }
+  wxString& replace(iterator first, iterator last,
+                    const char *first1, const char *last1)
+    { replace(first, last, first1, last1 - first1); return *this; }
+  wxString& replace(iterator first, iterator last,
+                    const wchar_t *first1, const wchar_t *last1)
+    { replace(first, last, first1, last1 - first1); return *this; }
 
   // swap two strings
   void swap(wxString& str)
index 4173d2c69b5b778d72a78a796a802ae5d47fd51e..be477a3275a6ec86e9bc2471b400105369d50a6c 100644 (file)
@@ -177,8 +177,87 @@ public:
   typedef const value_type& const_reference;
   typedef value_type* pointer;
   typedef const value_type* const_pointer;
-  typedef value_type *iterator;
-  typedef const value_type *const_iterator;
+
+  // macro to define the bulk of iterator and const_iterator classes
+  #define WX_DEFINE_STRINGIMPL_ITERATOR(iterator_name, ref_type, ptr_type)    \
+    public:                                                                   \
+        typedef wxStringCharType value_type;                                  \
+        typedef ref_type reference;                                           \
+        typedef ptr_type pointer;                                             \
+                                                                              \
+        iterator_name(pointer ptr) : m_ptr(ptr) { }                           \
+                                                                              \
+        reference operator*() const { return *m_ptr; }                        \
+                                                                              \
+        iterator_name& operator++() { m_ptr++; return *this; }                \
+        iterator_name operator++(int)                                         \
+        {                                                                     \
+            const iterator_name tmp(*this);                                   \
+            m_ptr++;                                                          \
+            return tmp;                                                       \
+        }                                                                     \
+                                                                              \
+        iterator_name& operator--() { m_ptr--; return *this; }                \
+        iterator_name operator--(int)                                         \
+        {                                                                     \
+            const iterator_name tmp(*this);                                   \
+            m_ptr--;                                                          \
+            return tmp;                                                       \
+        }                                                                     \
+                                                                              \
+        iterator_name operator+(int n) const                                  \
+            { return iterator_name(m_ptr + n); }                              \
+        iterator_name operator-(int n) const                                  \
+            { return iterator_name(m_ptr - n); }                              \
+        iterator_name& operator+=(int n)                                      \
+            { m_ptr += n; return *this; }                                     \
+        iterator_name& operator-=(int n)                                      \
+            { m_ptr -= n; return *this; }                                     \
+                                                                              \
+        size_t operator-(const iterator_name& i) const                        \
+            { return m_ptr - i.m_ptr; }                                       \
+                                                                              \
+        bool operator==(const iterator_name& i) const                         \
+          { return m_ptr == i.m_ptr; }                                        \
+        bool operator!=(const iterator_name& i) const                         \
+          { return m_ptr != i.m_ptr; }                                        \
+                                                                              \
+        bool operator<(const iterator_name& i) const                          \
+          { return m_ptr < i.m_ptr; }                                         \
+        bool operator>(const iterator_name& i) const                          \
+          { return m_ptr > i.m_ptr; }                                         \
+        bool operator<=(const iterator_name& i) const                         \
+          { return m_ptr <= i.m_ptr; }                                        \
+        bool operator>=(const iterator_name& i) const                         \
+          { return m_ptr >= i.m_ptr; }                                        \
+                                                                              \
+    private:                                                                  \
+        /* for wxStringImpl use only */                                       \
+        operator pointer() const { return m_ptr; }                            \
+                                                                              \
+        friend class WXDLLIMPEXP_BASE wxStringImpl;                           \
+                                                                              \
+        pointer m_ptr
+
+  class iterator
+  {
+    WX_DEFINE_STRINGIMPL_ITERATOR(iterator,
+                                  wxStringCharType&,
+                                  wxStringCharType*);
+
+    friend class const_iterator;
+  };
+
+  class const_iterator
+  {
+  public:
+      const_iterator(iterator i) : m_ptr(i.m_ptr) { }
+
+      WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator,
+                                    const wxStringCharType&,
+                                    const wxStringCharType*);
+  };
+
 
   // constructors and destructor
     // ctor for an empty string
@@ -215,8 +294,8 @@ public:
     size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
     InitWith(str.c_str(), nPos, nLen);
   }
-    // take all characters from pStart to pEnd
-  wxStringImpl(const void *pStart, const void *pEnd);
+    // take everything between start and end
+  wxStringImpl(const_iterator start, const_iterator end);
 
     // dtor is not virtual, this class must not be inherited from!
   ~wxStringImpl()
@@ -397,7 +476,7 @@ public:
     // find first n characters of sz
   size_t find(const wxStringCharType* sz, size_t nStart = 0, size_t n = npos) const;
 
-    // find the first occurence of character ch after nStart
+    // find the first occurrence of character ch after nStart
   size_t find(wxStringCharType ch, size_t nStart = 0) const;
 
     // rfind() family is exactly like find() but works right to left
index f16811fa7a41c97d977f135ca43245d55b99d502..0575bd8fc59782ab9efea8d4a6c9738e07f0ac95 100644 (file)
@@ -170,17 +170,15 @@ void wxStringImpl::InitWith(const wxChar *psz, size_t nPos, size_t nLength)
   }
 }
 
-// poor man's iterators are "void *" pointers
-wxStringImpl::wxStringImpl(const void *pStart, const void *pEnd)
+wxStringImpl::wxStringImpl(const_iterator first, const_iterator last)
 {
-  if ( pEnd >= pStart )
+  if ( last >= first )
   {
-    InitWith((const wxChar *)pStart, 0,
-             (const wxChar *)pEnd - (const wxChar *)pStart);
+    InitWith(first, 0, last - first);
   }
   else
   {
-    wxFAIL_MSG( _T("pStart is not before pEnd") );
+    wxFAIL_MSG( _T("first must be before last") );
     Init();
   }
 }
index a50f65ab21f7b62672b8b36d6f4a6342c344f6d5..6f5506f47afa2a720a18fb0f7de7dabbd0e0f9d8 100644 (file)
@@ -94,13 +94,19 @@ void StdStringTestCase::StdConstructors()
     CPPUNIT_ASSERT( s6 == s1 );
     CPPUNIT_ASSERT( s7 == s1 );
     CPPUNIT_ASSERT( s8 == _T("efgh") );
+
+    const char *pc = s1;
+    WX_ASSERT_STR_EQUAL( "bcd", wxString(pc + 1, pc + 4) );
+
+    const wchar_t *pw = s2.c_str();
+    WX_ASSERT_STR_EQUAL( "a", wxString(pw, pw + 1) );
 }
 
 void StdStringTestCase::StdAppend()
 {
     wxString s1, s2, s3, s4, s5, s6, s7, s8;
 
-    s1 = s2 = s3 = s4 = s5 = s6 = s7 = _T("abc");
+    s1 = s2 = s3 = s4 = s5 = s6 = _T("abc");
     s1.append(_T("def"));
     s2.append(_T("defgh"), 3);
     s3.append(wxString(_T("abcdef")), 3, 6);
@@ -117,6 +123,14 @@ void StdStringTestCase::StdAppend()
     CPPUNIT_ASSERT( s5 == _T("abcaaaxxy") );
     CPPUNIT_ASSERT( s6 == _T("abcdef") );
 
+    const char *pc = s1.c_str() + 2;
+    s7.append(pc, pc + 4);
+    WX_ASSERT_STR_EQUAL( "cdef", s7 );
+
+    const wchar_t *pw = s2.c_str() + 2;
+    s8.append(pw, pw + 4);
+    WX_ASSERT_STR_EQUAL( "cdef", s8 );
+
     s7 = s8 = wxString(_T("null\0time"), 9);
 
     s7.append(_T("def"));
@@ -144,6 +158,14 @@ void StdStringTestCase::StdAssign()
     CPPUNIT_ASSERT( s4 == _T("def") );
     CPPUNIT_ASSERT( s5 == _T("aaa") );
     CPPUNIT_ASSERT( s6 == _T("ef") );
+
+    const char *pc = s1;
+    s7.assign(pc, pc + 2);
+    WX_ASSERT_STR_EQUAL( "de", s7 );
+
+    const wchar_t *pw = s1.c_str();
+    s8.assign(pw + 2, pw + 3);
+    WX_ASSERT_STR_EQUAL( "f", s8 );
 }
 
 void StdStringTestCase::StdCompare()