Find offset by index

develop
kaetemi 4 years ago
parent 297de1d19a
commit 3b6e27e800

@ -131,7 +131,8 @@ public:
inline bool empty() const { return !m_Size; } inline bool empty() const { return !m_Size; }
const void *ptr() const { return m_Str; } const void *ptr() const { return m_Str; }
size_t count() const; // Slow count of UTF-32 characters size_t count() const; //< Slow count of UTF-32 codepoints
ptrdiff_t offset(ptrdiff_t i); const //< Get byte offset by utf-32 codepoint index
inline CUtfStringView substr(const iterator &begin, const iterator &end) const inline CUtfStringView substr(const iterator &begin, const iterator &end) const
{ {

@ -230,6 +230,18 @@ size_t CUtfStringView::count() const
return res; return res;
} }
ptrdiff_t CUtfStringView::offset(ptrdiff_t i)
{
size_t res = 0;
for (iterator it(begin()), end(this->end()); it != end; ++it)
{
if (res == i)
return (ptrdiff_t)it.ptr() - (ptrdiff_t)ptr();
++res;
}
return res;
}
u32char CUtfStringView::utf8Iterator(const void **addr) u32char CUtfStringView::utf8Iterator(const void **addr)
{ {
// Decode UTF-8 // Decode UTF-8

Loading…
Cancel
Save