mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Use Utf8View in Utf8StringLength.
parent
83401ad5e2
commit
f19e75b606
|
@ -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)
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -444,7 +444,7 @@ void Textbuf::Assign(const std::string_view text)
|
|||
*/
|
||||
void Textbuf::UpdateSize()
|
||||
{
|
||||
this->chars = static_cast<uint16_t>(Utf8StringLength(this->buf.c_str()) + 1); // terminating zero
|
||||
this->chars = static_cast<uint16_t>(Utf8StringLength(this->buf) + 1); // terminating zero
|
||||
assert(this->buf.size() < this->max_bytes);
|
||||
assert(this->chars <= this->max_chars);
|
||||
|
||||
|
|
Loading…
Reference in New Issue