From 3b6e27e800975fd98638495608387be9c4b29d90 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Wed, 4 Nov 2020 10:00:29 +0800 Subject: [PATCH] Find offset by index --- nel/include/nel/misc/utf_string_view.h | 3 ++- nel/src/misc/utf_string_view.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/nel/include/nel/misc/utf_string_view.h b/nel/include/nel/misc/utf_string_view.h index 8a224cef5..6264f425e 100644 --- a/nel/include/nel/misc/utf_string_view.h +++ b/nel/include/nel/misc/utf_string_view.h @@ -131,7 +131,8 @@ public: inline bool empty() const { return !m_Size; } 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 { diff --git a/nel/src/misc/utf_string_view.cpp b/nel/src/misc/utf_string_view.cpp index 39bf3b92f..283ef9555 100644 --- a/nel/src/misc/utf_string_view.cpp +++ b/nel/src/misc/utf_string_view.cpp @@ -230,6 +230,18 @@ size_t CUtfStringView::count() const 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) { // Decode UTF-8