]> git.saurik.com Git - wxWidgets.git/blobdiff - include/wx/stringimpl.h
add safe wxStrlcpy() function and replaced all wxStrncpy() calls by it
[wxWidgets.git] / include / wx / stringimpl.h
index 1a581de5d1831a04963c1b6fdfcd839e4020b01f..00d24e22424cba32fd01f294cd131b9bf1703fb8 100644 (file)
@@ -53,7 +53,18 @@ extern WXDLLIMPEXP_DATA_BASE(const wxStringCharType*) wxEmptyStringImpl;
 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
 // ----------------------------------------------------------------------------
 
 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
 // ----------------------------------------------------------------------------
 
-#define wxUSE_STL_BASED_WXSTRING  wxUSE_STL
+// using STL implies using std::string
+#if wxUSE_STL
+    #undef wxUSE_STD_STRING
+    #define wxUSE_STD_STRING 1
+#endif
+
+// we use STL-based string internally if we use std::string at all now, there
+// should be no reason to prefer our internal implement but if you really need
+// it you can predefine wxUSE_STL_BASED_WXSTRING as 0 when building the library
+#ifndef wxUSE_STL_BASED_WXSTRING
+    #define wxUSE_STL_BASED_WXSTRING  wxUSE_STD_STRING
+#endif
 
 // in both cases we need to define wxStdString
 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
 
 // in both cases we need to define wxStdString
 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
@@ -198,6 +209,7 @@ public:
         typedef ptr_type pointer;                                             \
         typedef int difference_type;                                          \
                                                                               \
         typedef ptr_type pointer;                                             \
         typedef int difference_type;                                          \
                                                                               \
+        iterator_name() : m_ptr(NULL) { }                                     \
         iterator_name(pointer ptr) : m_ptr(ptr) { }                           \
                                                                               \
         reference operator*() const { return *m_ptr; }                        \
         iterator_name(pointer ptr) : m_ptr(ptr) { }                           \
                                                                               \
         reference operator*() const { return *m_ptr; }                        \
@@ -218,21 +230,13 @@ public:
             return tmp;                                                       \
         }                                                                     \
                                                                               \
             return tmp;                                                       \
         }                                                                     \
                                                                               \
-        iterator_name operator+(int n) const                                  \
-            { return iterator_name(m_ptr + n); }                              \
-        iterator_name operator+(size_t n) const                               \
+        iterator_name operator+(ptrdiff_t n) const                            \
             { return iterator_name(m_ptr + n); }                              \
             { return iterator_name(m_ptr + n); }                              \
-        iterator_name operator-(int n) const                                  \
+        iterator_name operator-(ptrdiff_t n) const                            \
             { return iterator_name(m_ptr - n); }                              \
             { return iterator_name(m_ptr - n); }                              \
-        iterator_name operator-(size_t n) const                               \
-            { return iterator_name(m_ptr - n); }                              \
-        iterator_name& operator+=(int n)                                      \
-            { m_ptr += n; return *this; }                                     \
-        iterator_name& operator+=(size_t n)                                   \
+        iterator_name& operator+=(ptrdiff_t n)                                \
             { m_ptr += n; return *this; }                                     \
             { m_ptr += n; return *this; }                                     \
-        iterator_name& operator-=(int n)                                      \
-            { m_ptr -= n; return *this; }                                     \
-        iterator_name& operator-=(size_t n)                                   \
+        iterator_name& operator-=(ptrdiff_t n)                                \
             { m_ptr -= n; return *this; }                                     \
                                                                               \
         difference_type operator-(const iterator_name& i) const               \
             { m_ptr -= n; return *this; }                                     \
                                                                               \
         difference_type operator-(const iterator_name& i) const               \
@@ -254,18 +258,18 @@ public:
                                                                               \
     private:                                                                  \
         /* for wxStringImpl use only */                                       \
                                                                               \
     private:                                                                  \
         /* for wxStringImpl use only */                                       \
-        operator pointer() const { return m_ptr; }                            \
+        pointer GetPtr() const { return m_ptr; }                              \
                                                                               \
                                                                               \
-        friend class WXDLLIMPEXP_BASE wxStringImpl;                           \
+        friend class wxStringImpl;                                            \
                                                                               \
         pointer m_ptr
 
   // we need to declare const_iterator in wxStringImpl scope, the friend
   // declaration inside iterator class itself is not enough, or at least not
   // for g++ 3.4 (g++ 4 is ok)
                                                                               \
         pointer m_ptr
 
   // we need to declare const_iterator in wxStringImpl scope, the friend
   // declaration inside iterator class itself is not enough, or at least not
   // for g++ 3.4 (g++ 4 is ok)
-  class const_iterator;
+  class WXDLLIMPEXP_FWD_BASE const_iterator;
 
 
-  class iterator
+  class WXDLLIMPEXP_BASE iterator
   {
     WX_DEFINE_STRINGIMPL_ITERATOR(iterator,
                                   wxStringCharType&,
   {
     WX_DEFINE_STRINGIMPL_ITERATOR(iterator,
                                   wxStringCharType&,
@@ -274,7 +278,7 @@ public:
     friend class const_iterator;
   };
 
     friend class const_iterator;
   };
 
-  class const_iterator
+  class WXDLLIMPEXP_BASE const_iterator
   {
   public:
       const_iterator(iterator i) : m_ptr(i.m_ptr) { }
   {
   public:
       const_iterator(iterator i) : m_ptr(i.m_ptr) { }
@@ -378,9 +382,11 @@ public:
 
   // lib.string.access
     // return the character at position n
 
   // lib.string.access
     // return the character at position n
+  value_type operator[](size_type n) const { return m_pchData[n]; }
   value_type at(size_type n) const
     { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
     // returns the writable character at position n
   value_type at(size_type n) const
     { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
     // returns the writable character at position n
+  reference operator[](size_type n) { CopyBeforeWrite(); return m_pchData[n]; }
   reference at(size_type n)
   {
     wxASSERT_VALID_INDEX( n );
   reference at(size_type n)
   {
     wxASSERT_VALID_INDEX( n );
@@ -408,25 +414,25 @@ public:
   wxStringImpl& append(size_t n, wxStringCharType ch);
     // append from first to last
   wxStringImpl& append(const_iterator first, const_iterator last)
   wxStringImpl& append(size_t n, wxStringCharType ch);
     // append from first to last
   wxStringImpl& append(const_iterator first, const_iterator last)
-    { ConcatSelf(last - first, first); return *this; }
+    { ConcatSelf(last - first, first.GetPtr()); return *this; }
 
     // same as `this_string = str'
   wxStringImpl& assign(const wxStringImpl& str)
     { return *this = str; }
     // same as ` = str[pos..pos + n]
   wxStringImpl& assign(const wxStringImpl& str, size_t pos, size_t n)
 
     // same as `this_string = str'
   wxStringImpl& assign(const wxStringImpl& str)
     { return *this = str; }
     // same as ` = str[pos..pos + n]
   wxStringImpl& assign(const wxStringImpl& str, size_t pos, size_t n)
-    { clear(); return append(str, pos, n); }
+    { return replace(0, npos, str, pos, n); }
     // same as `= first n (or all if n == npos) characters of sz'
   wxStringImpl& assign(const wxStringCharType *sz)
     // same as `= first n (or all if n == npos) characters of sz'
   wxStringImpl& assign(const wxStringCharType *sz)
-    { clear(); return append(sz, wxStrlen(sz)); }
+    { return replace(0, npos, sz, wxStrlen(sz)); }
   wxStringImpl& assign(const wxStringCharType *sz, size_t n)
   wxStringImpl& assign(const wxStringCharType *sz, size_t n)
-    { clear(); return append(sz, n); }
+    { return replace(0, npos, sz, n); }
     // same as `= n copies of ch'
   wxStringImpl& assign(size_t n, wxStringCharType ch)
     // same as `= n copies of ch'
   wxStringImpl& assign(size_t n, wxStringCharType ch)
-    { clear(); return append(n, ch); }
+    { return replace(0, npos, n, ch); }
     // assign from first to last
   wxStringImpl& assign(const_iterator first, const_iterator last)
     // assign from first to last
   wxStringImpl& assign(const_iterator first, const_iterator last)
-    { clear(); return append(first, last); }
+    { return replace(begin(), end(), first, last); }
 
     // first valid index position
   const_iterator begin() const { return m_pchData; }
 
     // first valid index position
   const_iterator begin() const { return m_pchData; }
@@ -458,7 +464,7 @@ public:
   iterator insert(iterator it, wxStringCharType ch)
     { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
   void insert(iterator it, const_iterator first, const_iterator last)
   iterator insert(iterator it, wxStringCharType ch)
     { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
   void insert(iterator it, const_iterator first, const_iterator last)
-    { insert(it - begin(), first, last - first); }
+    { insert(it - begin(), first.GetPtr(), last - first); }
   void insert(iterator it, size_type n, wxStringCharType ch)
     { insert(it - begin(), n, ch); }
 
   void insert(iterator it, size_type n, wxStringCharType ch)
     { insert(it - begin(), n, ch); }
 
@@ -477,18 +483,23 @@ public:
   const wxStringCharType* data() const { return m_pchData; }
 
     // replaces the substring of length nLen starting at nStart
   const wxStringCharType* data() const { return m_pchData; }
 
     // replaces the substring of length nLen starting at nStart
-  wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringCharType* sz);
+  wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringCharType* sz)
+    { return replace(nStart, nLen, sz, npos); }
     // replaces the substring of length nLen starting at nStart
   wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringImpl& str)
     // replaces the substring of length nLen starting at nStart
   wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringImpl& str)
-    { return replace(nStart, nLen, str.c_str()); }
+    { return replace(nStart, nLen, str.c_str(), str.length()); }
     // replaces the substring with nCount copies of ch
     // replaces the substring with nCount copies of ch
-  wxStringImpl& replace(size_t nStart, size_t nLen, size_t nCount, wxStringCharType ch);
+  wxStringImpl& replace(size_t nStart, size_t nLen,
+                        size_t nCount, wxStringCharType ch)
+    { return replace(nStart, nLen, wxStringImpl(nCount, ch)); }
     // replaces a substring with another substring
   wxStringImpl& replace(size_t nStart, size_t nLen,
     // replaces a substring with another substring
   wxStringImpl& replace(size_t nStart, size_t nLen,
-                        const wxStringImpl& str, size_t nStart2, size_t nLen2);
+                        const wxStringImpl& str, size_t nStart2, size_t nLen2)
+    { return replace(nStart, nLen, str.substr(nStart2, nLen2)); }
     // replaces the substring with first nCount chars of sz
   wxStringImpl& replace(size_t nStart, size_t nLen,
                         const wxStringCharType* sz, size_t nCount);
     // replaces the substring with first nCount chars of sz
   wxStringImpl& replace(size_t nStart, size_t nLen,
                         const wxStringCharType* sz, size_t nCount);
+
   wxStringImpl& replace(iterator first, iterator last, const_pointer s)
     { return replace(first - begin(), last - first, s); }
   wxStringImpl& replace(iterator first, iterator last, const_pointer s,
   wxStringImpl& replace(iterator first, iterator last, const_pointer s)
     { return replace(first - begin(), last - first, s); }
   wxStringImpl& replace(iterator first, iterator last, const_pointer s,
@@ -500,7 +511,7 @@ public:
     { return replace(first - begin(), last - first, n, c); }
   wxStringImpl& replace(iterator first, iterator last,
                         const_iterator first1, const_iterator last1)
     { return replace(first - begin(), last - first, n, c); }
   wxStringImpl& replace(iterator first, iterator last,
                         const_iterator first1, const_iterator last1)
-    { return replace(first - begin(), last - first, first1, last1 - first1); }
+    { return replace(first - begin(), last - first, first1.GetPtr(), last1 - first1); }
 
     // swap two strings
   void swap(wxStringImpl& str);
 
     // swap two strings
   void swap(wxStringImpl& str);
@@ -546,7 +557,7 @@ public:
   void DoUngetWriteBuf();
   void DoUngetWriteBuf(size_t nLen);
 
   void DoUngetWriteBuf();
   void DoUngetWriteBuf(size_t nLen);
 
-  friend class WXDLLIMPEXP_BASE wxString;
+  friend class WXDLLIMPEXP_FWD_BASE wxString;
 };
 
 #endif // !wxUSE_STL_BASED_WXSTRING
 };
 
 #endif // !wxUSE_STL_BASED_WXSTRING