From f19e75b60646781a818359414750a1b11583a198 Mon Sep 17 00:00:00 2001 From: frosch Date: Tue, 1 Apr 2025 18:21:03 +0200 Subject: [PATCH] Codechange: Use Utf8View in Utf8StringLength. --- src/string.cpp | 19 +++---------------- src/string_func.h | 3 +-- src/tests/utf8.cpp | 12 ++++++++++++ src/textbuf.cpp | 2 +- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/string.cpp b/src/string.cpp index 6eec0604f6..3224a96a7a 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -389,23 +389,10 @@ bool StrContainsIgnoreCase(const std::string_view str, const std::string_view va * @param s The string to get the length for. * @return The length of the string in characters. */ -size_t Utf8StringLength(const char *s) +size_t Utf8StringLength(std::string_view str) { - size_t len = 0; - const char *t = s; - while (Utf8Consume(&t) != 0) len++; - return len; -} - -/** - * Get the length of an UTF-8 encoded string in number of characters - * and thus not the number of bytes that the encoded string contains. - * @param s The string to get the length for. - * @return The length of the string in characters. - */ -size_t Utf8StringLength(const std::string &str) -{ - return Utf8StringLength(str.c_str()); + Utf8View view(str); + return std::distance(view.begin(), view.end()); } bool strtolower(std::string &str, std::string::size_type offs) diff --git a/src/string_func.h b/src/string_func.h index 68fd80b45f..7a37952a6e 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -174,8 +174,7 @@ inline std::string::iterator Utf8PrevChar(std::string::iterator &s) return cur; } -size_t Utf8StringLength(const char *s); -size_t Utf8StringLength(const std::string &str); +size_t Utf8StringLength(std::string_view str); /** * Is the given character a lead surrogate code point? diff --git a/src/tests/utf8.cpp b/src/tests/utf8.cpp index c8ff3a191a..1429fd6283 100644 --- a/src/tests/utf8.cpp +++ b/src/tests/utf8.cpp @@ -95,6 +95,8 @@ TEST_CASE("Utf8View - iterate") auto it = begin; CHECK(it == begin); CHECK(it.GetByteOffset() == 0); + CHECK(std::distance(begin, it) == 0); + CHECK(std::distance(it, end) == 5); CHECK(*it == 0x1234); CHECK(it == view.GetIterAtByte(0)); CHECK(it == view.GetIterAtByte(1)); @@ -103,20 +105,28 @@ TEST_CASE("Utf8View - iterate") CHECK(begin < it); CHECK(it < end); CHECK(it.GetByteOffset() == 3); + CHECK(std::distance(begin, it) == 1); + CHECK(std::distance(it, end) == 4); CHECK(*it == 'a'); CHECK(it == view.GetIterAtByte(3)); ++it; CHECK(it.GetByteOffset() == 4); + CHECK(std::distance(begin, it) == 2); + CHECK(std::distance(it, end) == 3); CHECK(*it == 0); CHECK(it == view.GetIterAtByte(4)); ++it; CHECK(it.GetByteOffset() == 5); + CHECK(std::distance(begin, it) == 3); + CHECK(std::distance(it, end) == 2); CHECK(*it == 'b'); CHECK(it == view.GetIterAtByte(5)); ++it; CHECK(begin < it); CHECK(it < end); CHECK(it.GetByteOffset() == 6); + CHECK(std::distance(begin, it) == 4); + CHECK(std::distance(it, end) == 1); CHECK(*it == 0x00012345); CHECK(it == view.GetIterAtByte(6)); CHECK(it == view.GetIterAtByte(7)); @@ -125,6 +135,8 @@ TEST_CASE("Utf8View - iterate") ++it; CHECK(begin < it); CHECK(it.GetByteOffset() == 10); + CHECK(std::distance(begin, it) == 5); + CHECK(std::distance(it, end) == 0); CHECK(it == end); CHECK(it == view.GetIterAtByte(10)); --it; diff --git a/src/textbuf.cpp b/src/textbuf.cpp index eae281cb89..f41023b966 100644 --- a/src/textbuf.cpp +++ b/src/textbuf.cpp @@ -444,7 +444,7 @@ void Textbuf::Assign(const std::string_view text) */ void Textbuf::UpdateSize() { - this->chars = static_cast(Utf8StringLength(this->buf.c_str()) + 1); // terminating zero + this->chars = static_cast(Utf8StringLength(this->buf) + 1); // terminating zero assert(this->buf.size() < this->max_bytes); assert(this->chars <= this->max_chars);