added overloads taking pairs of const char/wchar_t pointers for wxString methods working with const_iterators for backwards compatibility with old wxString::const_iterator which used to be convertible to/from const wxChar *

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45265 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-04-05 21:55:29 +00:00
parent a9dce709f5
commit f2a1b1bd23
4 changed files with 142 additions and 18 deletions

View File

@@ -1406,13 +1406,19 @@ public:
// take nLen chars starting at nPos // take nLen chars starting at nPos
wxString(const wxString& str, size_t nPos, size_t nLen) wxString(const wxString& str, size_t nPos, size_t nLen)
: m_impl(str.m_impl, nPos, nLen) { } : m_impl(str.m_impl, nPos, nLen) { }
// take all characters from pStart to pEnd // take all characters from first to last
wxString(const void *pStart, const void *pEnd)
: m_impl((const wxChar*)pStart, (const wxChar*)pEnd) { }
wxString(const_iterator first, const_iterator last) wxString(const_iterator first, const_iterator last)
: m_impl(first, last) { } : m_impl(first, last) { }
wxString(iterator first, iterator last) wxString(const char *first, const char *last)
: m_impl(first, 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 // lib.string.modifiers
// append elements str[pos], ..., str[pos+n] // append elements str[pos], ..., str[pos+n]
@@ -1459,6 +1465,10 @@ public:
// append from first to last // append from first to last
wxString& append(const_iterator first, const_iterator last) wxString& append(const_iterator first, const_iterator last)
{ m_impl.append(first, last); return *this; } { 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' // same as `this_string = str'
wxString& assign(const wxString& str) wxString& assign(const wxString& str)
@@ -1517,6 +1527,10 @@ public:
// assign from first to last // assign from first to last
wxString& assign(const_iterator first, const_iterator last) wxString& assign(const_iterator first, const_iterator last)
{ m_impl.assign(first, last); return *this; } { 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 // string comparison
int compare(const wxString& str) const; int compare(const wxString& str) const;
@@ -1577,6 +1591,10 @@ public:
{ return iterator(m_impl.insert(it, EncodeChar(ch))); } { return iterator(m_impl.insert(it, EncodeChar(ch))); }
void insert(iterator it, const_iterator first, const_iterator last) void insert(iterator it, const_iterator first, const_iterator last)
{ m_impl.insert(it, first, 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) void insert(iterator it, size_type n, wxUniChar ch)
{ {
#if wxUSE_UNICODE_UTF8 #if wxUSE_UNICODE_UTF8
@@ -1595,6 +1613,7 @@ public:
m_impl.erase(from, len); m_impl.erase(from, len);
return *this; return *this;
} }
// delete characters from first up to last
iterator erase(iterator first, iterator last) iterator erase(iterator first, iterator last)
{ return iterator(m_impl.erase(first, last)); } { return iterator(m_impl.erase(first, last)); }
iterator erase(iterator first) iterator erase(iterator first)
@@ -1718,6 +1737,12 @@ public:
wxString& replace(iterator first, iterator last, wxString& replace(iterator first, iterator last,
const_iterator first1, const_iterator last1) const_iterator first1, const_iterator last1)
{ m_impl.replace(first, last, first1, last1); return *this; } { 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 // swap two strings
void swap(wxString& str) void swap(wxString& str)

View File

@@ -177,8 +177,87 @@ public:
typedef const value_type& const_reference; typedef const value_type& const_reference;
typedef value_type* pointer; typedef value_type* pointer;
typedef const value_type* const_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 // constructors and destructor
// ctor for an empty string // ctor for an empty string
@@ -215,8 +294,8 @@ public:
size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen; size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
InitWith(str.c_str(), nPos, nLen); InitWith(str.c_str(), nPos, nLen);
} }
// take all characters from pStart to pEnd // take everything between start and end
wxStringImpl(const void *pStart, const void *pEnd); wxStringImpl(const_iterator start, const_iterator end);
// dtor is not virtual, this class must not be inherited from! // dtor is not virtual, this class must not be inherited from!
~wxStringImpl() ~wxStringImpl()
@@ -397,7 +476,7 @@ public:
// find first n characters of sz // find first n characters of sz
size_t find(const wxStringCharType* sz, size_t nStart = 0, size_t n = npos) const; 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; size_t find(wxStringCharType ch, size_t nStart = 0) const;
// rfind() family is exactly like find() but works right to left // rfind() family is exactly like find() but works right to left

View 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_iterator first, const_iterator last)
wxStringImpl::wxStringImpl(const void *pStart, const void *pEnd)
{ {
if ( pEnd >= pStart ) if ( last >= first )
{ {
InitWith((const wxChar *)pStart, 0, InitWith(first, 0, last - first);
(const wxChar *)pEnd - (const wxChar *)pStart);
} }
else else
{ {
wxFAIL_MSG( _T("pStart is not before pEnd") ); wxFAIL_MSG( _T("first must be before last") );
Init(); Init();
} }
} }

View File

@@ -94,13 +94,19 @@ void StdStringTestCase::StdConstructors()
CPPUNIT_ASSERT( s6 == s1 ); CPPUNIT_ASSERT( s6 == s1 );
CPPUNIT_ASSERT( s7 == s1 ); CPPUNIT_ASSERT( s7 == s1 );
CPPUNIT_ASSERT( s8 == _T("efgh") ); 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() void StdStringTestCase::StdAppend()
{ {
wxString s1, s2, s3, s4, s5, s6, s7, s8; 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")); s1.append(_T("def"));
s2.append(_T("defgh"), 3); s2.append(_T("defgh"), 3);
s3.append(wxString(_T("abcdef")), 3, 6); s3.append(wxString(_T("abcdef")), 3, 6);
@@ -117,6 +123,14 @@ void StdStringTestCase::StdAppend()
CPPUNIT_ASSERT( s5 == _T("abcaaaxxy") ); CPPUNIT_ASSERT( s5 == _T("abcaaaxxy") );
CPPUNIT_ASSERT( s6 == _T("abcdef") ); 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 = s8 = wxString(_T("null\0time"), 9);
s7.append(_T("def")); s7.append(_T("def"));
@@ -144,6 +158,14 @@ void StdStringTestCase::StdAssign()
CPPUNIT_ASSERT( s4 == _T("def") ); CPPUNIT_ASSERT( s4 == _T("def") );
CPPUNIT_ASSERT( s5 == _T("aaa") ); CPPUNIT_ASSERT( s5 == _T("aaa") );
CPPUNIT_ASSERT( s6 == _T("ef") ); 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() void StdStringTestCase::StdCompare()