From 52dc2108ae52f9cc997fc80e2b2b0b399b9581ef Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 25 Mar 2012 16:45:50 +0000 Subject: [PATCH] Add helper wxString::IterOffsetInMBStr() method. This method can be used to recover the pointer in char* buffer containing multi-byte string representation corresponding to the given iterator which is useful for implementing backwards-compatible functions that must be able to return such pointers. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70995 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/string.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/wx/string.h b/include/wx/string.h index ee63cf915c..2e0e2423e2 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -1150,6 +1150,25 @@ public: #undef WX_STR_ITERATOR_TAG #undef WX_STR_ITERATOR_IMPL + // This method is mostly used by wxWidgets itself and return the offset of + // the given iterator in bytes relative to the start of the buffer + // representing the current string contents in the current locale encoding. + // + // It is inefficient as it involves converting part of the string to this + // encoding (and also unsafe as it simply returns 0 if the conversion fails) + // and so should be avoided if possible, wx itself only uses it to implement + // backwards-compatible API. + ptrdiff_t IterOffsetInMBStr(const const_iterator& i) const + { + const wxString str(begin(), i); + + // This is logically equivalent to strlen(str.mb_str()) but avoids + // actually converting the string to multibyte and just computes the + // length that it would have after conversion. + size_t ofs = wxConvLibc.FromWChar(NULL, 0, str.wc_str(), str.length()); + return ofs == wxCONV_FAILED ? 0 : static_cast(ofs); + } + friend class iterator; friend class const_iterator;