only provide ptrdiff_t versions of verious operator+/- working with iterators instead of overloads for both int and size_t: this seems to be enough and the existing overloads were not enough for 64 bit builds where expressions such as iter+(ptr2-ptr1) didn't compile without extra casts

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@51053 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-01-06 22:27:01 +00:00
parent 91a2809da0
commit b5343e065a
3 changed files with 38 additions and 98 deletions

View File

@@ -237,22 +237,12 @@ public:
wxCStrData operator+(size_t n) const wxCStrData operator+(size_t n) const
{ return wxCStrData(m_str, m_offset + n, m_owned); } { return wxCStrData(m_str, m_offset + n, m_owned); }
// and these for "str.c_str() + n - 2": // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
wxCStrData operator-(int n) const // expression but it must be ptrdiff_t and not e.g. int to work in this
// example):
wxCStrData operator-(ptrdiff_t n) const
{ {
wxASSERT_MSG( n <= (int)m_offset, wxASSERT_MSG( n <= (ptrdiff_t)m_offset,
_T("attempt to construct address before the beginning of the string") );
return wxCStrData(m_str, m_offset - n, m_owned);
}
wxCStrData operator-(long n) const
{
wxASSERT_MSG( n <= (int)m_offset,
_T("attempt to construct address before the beginning of the string") );
return wxCStrData(m_str, m_offset - n, m_owned);
}
wxCStrData operator-(size_t n) const
{
wxASSERT_MSG( n <= m_offset,
_T("attempt to construct address before the beginning of the string") ); _T("attempt to construct address before the beginning of the string") );
return wxCStrData(m_str, m_offset - n, m_owned); return wxCStrData(m_str, m_offset - n, m_owned);
} }
@@ -603,25 +593,15 @@ public:
return tmp; \ return tmp; \
} \ } \
\ \
iterator_name& operator+=(int n) \ iterator_name& operator+=(ptrdiff_t n) \
{ \ { \
m_cur = wxStringOperations::AddToIter(m_cur, n); \ m_cur = wxStringOperations::AddToIter(m_cur, n); \
return *this; \ return *this; \
} \ } \
iterator_name& operator+=(size_t n) \ iterator_name& operator-=(ptrdiff_t n) \
{ \
m_cur = wxStringOperations::AddToIter(m_cur, (int)n); \
return *this; \
} \
iterator_name& operator-=(int n) \
{ \ { \
m_cur = wxStringOperations::AddToIter(m_cur, -n); \ m_cur = wxStringOperations::AddToIter(m_cur, -n); \
return *this; \ return *this; \
} \
iterator_name& operator-=(size_t n) \
{ \
m_cur = wxStringOperations::AddToIter(m_cur, -(int)n); \
return *this; \
} \ } \
\ \
difference_type operator-(const iterator_name& i) const \ difference_type operator-(const iterator_name& i) const \
@@ -682,14 +662,10 @@ public:
reference operator*() reference operator*()
{ return wxUniCharRef::CreateForString(m_node, m_cur); } { return wxUniCharRef::CreateForString(m_node, m_cur); }
iterator operator+(int n) const iterator operator+(ptrdiff_t n) const
{ return iterator(str(), wxStringOperations::AddToIter(m_cur, n)); } { return iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
iterator operator+(size_t n) const iterator operator-(ptrdiff_t n) const
{ return iterator(str(), wxStringOperations::AddToIter(m_cur, (int)n)); }
iterator operator-(int n) const
{ return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); } { return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
iterator operator-(size_t n) const
{ return iterator(str(), wxStringOperations::AddToIter(m_cur, -(int)n)); }
private: private:
iterator(wxString *str, underlying_iterator ptr) iterator(wxString *str, underlying_iterator ptr)
@@ -723,14 +699,10 @@ public:
reference operator*() const reference operator*() const
{ return wxStringOperations::DecodeChar(m_cur); } { return wxStringOperations::DecodeChar(m_cur); }
const_iterator operator+(int n) const const_iterator operator+(ptrdiff_t n) const
{ return const_iterator(str(), wxStringOperations::AddToIter(m_cur, n)); } { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
const_iterator operator+(size_t n) const const_iterator operator-(ptrdiff_t n) const
{ return const_iterator(str(), wxStringOperations::AddToIter(m_cur, (int)n)); }
const_iterator operator-(int n) const
{ return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); } { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
const_iterator operator-(size_t n) const
{ return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -(int)n)); }
private: private:
// for internal wxString use only: // for internal wxString use only:
@@ -758,14 +730,10 @@ public:
reference operator*() reference operator*()
{ return wxUniCharRef::CreateForString(m_cur); } { return wxUniCharRef::CreateForString(m_cur); }
iterator operator+(int n) const iterator operator+(ptrdiff_t n) const
{ return iterator(wxStringOperations::AddToIter(m_cur, n)); } { return iterator(wxStringOperations::AddToIter(m_cur, n)); }
iterator operator+(size_t n) const iterator operator-(ptrdiff_t n) const
{ return iterator(wxStringOperations::AddToIter(m_cur, (int)n)); }
iterator operator-(int n) const
{ return iterator(wxStringOperations::AddToIter(m_cur, -n)); } { return iterator(wxStringOperations::AddToIter(m_cur, -n)); }
iterator operator-(size_t n) const
{ return iterator(wxStringOperations::AddToIter(m_cur, -(int)n)); }
private: private:
// for internal wxString use only: // for internal wxString use only:
@@ -789,14 +757,10 @@ public:
reference operator*() const reference operator*() const
{ return wxStringOperations::DecodeChar(m_cur); } { return wxStringOperations::DecodeChar(m_cur); }
const_iterator operator+(int n) const const_iterator operator+(ptrdiff_t n) const
{ return const_iterator(wxStringOperations::AddToIter(m_cur, n)); } { return const_iterator(wxStringOperations::AddToIter(m_cur, n)); }
const_iterator operator+(size_t n) const const_iterator operator-(ptrdiff_t n) const
{ return const_iterator(wxStringOperations::AddToIter(m_cur, (int)n)); }
const_iterator operator-(int n) const
{ return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); } { return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); }
const_iterator operator-(size_t n) const
{ return const_iterator(wxStringOperations::AddToIter(m_cur, -(int)n)); }
private: private:
// for internal wxString use only: // for internal wxString use only:
@@ -844,21 +808,13 @@ public:
{ reverse_iterator_impl tmp = *this; ++m_cur; return tmp; } { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; }
// NB: explicit <T> in the functions below is to keep BCC 5.5 happy // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
reverse_iterator_impl operator+(int n) const reverse_iterator_impl operator+(ptrdiff_t n) const
{ return reverse_iterator_impl<T>(m_cur - n); } { return reverse_iterator_impl<T>(m_cur - n); }
reverse_iterator_impl operator+(size_t n) const reverse_iterator_impl operator-(ptrdiff_t n) const
{ return reverse_iterator_impl<T>(m_cur - n); }
reverse_iterator_impl operator-(int n) const
{ return reverse_iterator_impl<T>(m_cur + n); } { return reverse_iterator_impl<T>(m_cur + n); }
reverse_iterator_impl operator-(size_t n) const reverse_iterator_impl operator+=(ptrdiff_t n)
{ return reverse_iterator_impl<T>(m_cur + n); }
reverse_iterator_impl operator+=(int n)
{ m_cur -= n; return *this; } { m_cur -= n; return *this; }
reverse_iterator_impl operator+=(size_t n) reverse_iterator_impl operator-=(ptrdiff_t n)
{ m_cur -= n; return *this; }
reverse_iterator_impl operator-=(int n)
{ m_cur += n; return *this; }
reverse_iterator_impl operator-=(size_t n)
{ m_cur += n; return *this; } { m_cur += n; return *this; }
unsigned operator-(const reverse_iterator_impl& i) const unsigned operator-(const reverse_iterator_impl& i) const
@@ -2637,21 +2593,13 @@ private:
// string iterator operators that satisfy STL Random Access Iterator // string iterator operators that satisfy STL Random Access Iterator
// requirements: // requirements:
inline wxString::iterator operator+(int n, wxString::iterator i) inline wxString::iterator operator+(ptrdiff_t n, wxString::iterator i)
{ return i + n; } { return i + n; }
inline wxString::iterator operator+(size_t n, wxString::iterator i) inline wxString::const_iterator operator+(ptrdiff_t n, wxString::const_iterator i)
{ return i + n; } { return i + n; }
inline wxString::const_iterator operator+(int n, wxString::const_iterator i) inline wxString::reverse_iterator operator+(ptrdiff_t n, wxString::reverse_iterator i)
{ return i + n; } { return i + n; }
inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i) inline wxString::const_reverse_iterator operator+(ptrdiff_t n, wxString::const_reverse_iterator i)
{ return i + n; }
inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i)
{ return i + n; }
inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i)
{ return i + n; }
inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i)
{ return i + n; }
inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i)
{ return i + n; } { return i + n; }
// notice that even though for many compilers the friend declarations above are // notice that even though for many compilers the friend declarations above are

View File

@@ -219,21 +219,13 @@ public:
return tmp; \ return tmp; \
} \ } \
\ \
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 \ iterator_name operator-(ptrdiff_t n) const \
{ return iterator_name(m_ptr + n); } \
iterator_name operator-(int n) const \
{ return iterator_name(m_ptr - n); } \ { return iterator_name(m_ptr - n); } \
iterator_name operator-(size_t n) const \ iterator_name& operator+=(ptrdiff_t n) \
{ return iterator_name(m_ptr - n); } \
iterator_name& operator+=(int n) \
{ m_ptr += n; return *this; } \ { m_ptr += n; return *this; } \
iterator_name& operator+=(size_t n) \ iterator_name& operator-=(ptrdiff_t n) \
{ m_ptr += n; return *this; } \
iterator_name& operator-=(int n) \
{ m_ptr -= n; return *this; } \
iterator_name& operator-=(size_t n) \
{ m_ptr -= n; return *this; } \ { m_ptr -= n; return *this; } \
\ \
difference_type operator-(const iterator_name& i) const \ difference_type operator-(const iterator_name& i) const \

View File

@@ -36,17 +36,17 @@ struct WXDLLIMPEXP_BASE wxStringOperationsWchar
static void DecIter(wxStringImpl::const_iterator& i) { --i; } static void DecIter(wxStringImpl::const_iterator& i) { --i; }
// moves the iterator by n Unicode characters // moves the iterator by n Unicode characters
static wxStringImpl::iterator AddToIter(const wxStringImpl::iterator& i, int n) static wxStringImpl::iterator AddToIter(const wxStringImpl::iterator& i, ptrdiff_t n)
{ return i + n; } { return i + n; }
static wxStringImpl::const_iterator AddToIter(const wxStringImpl::const_iterator& i, int n) static wxStringImpl::const_iterator AddToIter(const wxStringImpl::const_iterator& i, ptrdiff_t n)
{ return i + n; } { return i + n; }
// returns distance of the two iterators in Unicode characters // returns distance of the two iterators in Unicode characters
static int DiffIters(const wxStringImpl::iterator& i1, static ptrdiff_t DiffIters(const wxStringImpl::iterator& i1,
const wxStringImpl::iterator& i2) const wxStringImpl::iterator& i2)
{ return i1 - i2; } { return i1 - i2; }
static int DiffIters(const wxStringImpl::const_iterator& i1, static ptrdiff_t DiffIters(const wxStringImpl::const_iterator& i1,
const wxStringImpl::const_iterator& i2) const wxStringImpl::const_iterator& i2)
{ return i1 - i2; } { return i1 - i2; }
// encodes the character to a form used to represent it in internal // encodes the character to a form used to represent it in internal
@@ -97,18 +97,18 @@ struct WXDLLIMPEXP_BASE wxStringOperationsUtf8
} }
template<typename Iterator> template<typename Iterator>
static Iterator AddToIter(const Iterator& i, int n) static Iterator AddToIter(const Iterator& i, ptrdiff_t n)
{ {
Iterator out(i); Iterator out(i);
if ( n > 0 ) if ( n > 0 )
{ {
for ( int j = 0; j < n; ++j ) for ( ptrdiff_t j = 0; j < n; ++j )
IncIter(out); IncIter(out);
} }
else if ( n < 0 ) else if ( n < 0 )
{ {
for ( int j = 0; j > n; --j ) for ( ptrdiff_t j = 0; j > n; --j )
DecIter(out); DecIter(out);
} }
@@ -116,9 +116,9 @@ struct WXDLLIMPEXP_BASE wxStringOperationsUtf8
} }
template<typename Iterator> template<typename Iterator>
static int DiffIters(Iterator i1, Iterator i2) static ptrdiff_t DiffIters(Iterator i1, Iterator i2)
{ {
int dist = 0; ptrdiff_t dist = 0;
if ( i1 < i2 ) if ( i1 < i2 )
{ {