2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2020-01-24 13:45:29 +00:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2021-05-17 19:04:37 +00:00
|
|
|
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/Assertions.h>
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <AK/Format.h>
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/Utf8View.h>
|
2019-08-27 21:57:15 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-09-04 20:40:36 +00:00
|
|
|
Utf8View::Utf8View(const String& string)
|
|
|
|
: m_string(string)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
Utf8View::Utf8View(const StringView& string)
|
|
|
|
: m_string(string)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:06:39 +00:00
|
|
|
Utf8View::Utf8View(const char* string)
|
|
|
|
: m_string(string)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
const unsigned char* Utf8View::begin_ptr() const
|
|
|
|
{
|
|
|
|
return (const unsigned char*)m_string.characters_without_null_termination();
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* Utf8View::end_ptr() const
|
|
|
|
{
|
2019-09-04 20:40:36 +00:00
|
|
|
return begin_ptr() + m_string.length();
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 07:45:52 +00:00
|
|
|
Utf8CodePointIterator Utf8View::begin() const
|
2019-08-27 21:57:15 +00:00
|
|
|
{
|
2021-01-01 23:28:24 +00:00
|
|
|
return { begin_ptr(), m_string.length() };
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 07:45:52 +00:00
|
|
|
Utf8CodePointIterator Utf8View::end() const
|
2019-08-27 21:57:15 +00:00
|
|
|
{
|
|
|
|
return { end_ptr(), 0 };
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:45:52 +00:00
|
|
|
Utf8CodePointIterator Utf8View::iterator_at_byte_offset(size_t byte_offset) const
|
2021-05-18 14:09:20 +00:00
|
|
|
{
|
|
|
|
size_t current_offset = 0;
|
|
|
|
for (auto iterator = begin(); !iterator.done(); ++iterator) {
|
|
|
|
if (current_offset >= byte_offset)
|
|
|
|
return iterator;
|
|
|
|
current_offset += iterator.code_point_length_in_bytes();
|
|
|
|
}
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:45:52 +00:00
|
|
|
size_t Utf8View::byte_offset_of(const Utf8CodePointIterator& it) const
|
2019-09-04 20:40:36 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(it.m_ptr >= begin_ptr());
|
|
|
|
VERIFY(it.m_ptr <= end_ptr());
|
2019-09-04 20:40:36 +00:00
|
|
|
|
|
|
|
return it.m_ptr - begin_ptr();
|
|
|
|
}
|
|
|
|
|
2021-05-17 18:59:15 +00:00
|
|
|
Utf8View Utf8View::substring_view(size_t byte_offset, size_t byte_length) const
|
2019-09-04 20:40:36 +00:00
|
|
|
{
|
|
|
|
StringView string = m_string.substring_view(byte_offset, byte_length);
|
|
|
|
return Utf8View { string };
|
|
|
|
}
|
|
|
|
|
2021-05-17 19:04:37 +00:00
|
|
|
Utf8View Utf8View::unicode_substring_view(size_t codepoint_offset, size_t codepoint_length) const
|
|
|
|
{
|
|
|
|
if (codepoint_length == 0)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
size_t codepoint_index = 0, offset_in_bytes = 0;
|
|
|
|
for (auto iterator = begin(); !iterator.done(); ++iterator) {
|
|
|
|
if (codepoint_index == codepoint_offset)
|
|
|
|
offset_in_bytes = byte_offset_of(iterator);
|
|
|
|
if (codepoint_index == codepoint_offset + codepoint_length - 1) {
|
|
|
|
size_t length_in_bytes = byte_offset_of(++iterator) - offset_in_bytes;
|
|
|
|
return substring_view(offset_in_bytes, length_in_bytes);
|
|
|
|
}
|
|
|
|
++codepoint_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
static inline bool decode_first_byte(
|
|
|
|
unsigned char byte,
|
2021-01-01 23:28:24 +00:00
|
|
|
size_t& out_code_point_length_in_bytes,
|
2019-08-27 21:57:15 +00:00
|
|
|
u32& out_value)
|
|
|
|
{
|
|
|
|
if ((byte & 128) == 0) {
|
|
|
|
out_value = byte;
|
2020-08-05 20:31:20 +00:00
|
|
|
out_code_point_length_in_bytes = 1;
|
2019-08-27 21:57:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((byte & 64) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((byte & 32) == 0) {
|
|
|
|
out_value = byte & 31;
|
2020-08-05 20:31:20 +00:00
|
|
|
out_code_point_length_in_bytes = 2;
|
2019-08-27 21:57:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((byte & 16) == 0) {
|
|
|
|
out_value = byte & 15;
|
2020-08-05 20:31:20 +00:00
|
|
|
out_code_point_length_in_bytes = 3;
|
2019-08-27 21:57:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((byte & 8) == 0) {
|
|
|
|
out_value = byte & 7;
|
2020-08-05 20:31:20 +00:00
|
|
|
out_code_point_length_in_bytes = 4;
|
2019-08-27 21:57:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-18 09:15:18 +00:00
|
|
|
bool Utf8View::validate(size_t& valid_bytes) const
|
2019-08-27 21:57:15 +00:00
|
|
|
{
|
2020-05-18 09:15:18 +00:00
|
|
|
valid_bytes = 0;
|
2019-08-27 21:57:15 +00:00
|
|
|
for (auto ptr = begin_ptr(); ptr < end_ptr(); ptr++) {
|
2021-01-01 23:28:24 +00:00
|
|
|
size_t code_point_length_in_bytes;
|
2019-08-27 21:57:15 +00:00
|
|
|
u32 value;
|
2020-08-05 20:31:20 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(*ptr, code_point_length_in_bytes, value);
|
2019-08-27 21:57:15 +00:00
|
|
|
if (!first_byte_makes_sense)
|
|
|
|
return false;
|
|
|
|
|
2021-01-01 23:28:24 +00:00
|
|
|
for (size_t i = 1; i < code_point_length_in_bytes; i++) {
|
2019-08-27 21:57:15 +00:00
|
|
|
ptr++;
|
|
|
|
if (ptr >= end_ptr())
|
|
|
|
return false;
|
|
|
|
if (*ptr >> 6 != 2)
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-18 09:15:18 +00:00
|
|
|
|
2020-08-05 20:31:20 +00:00
|
|
|
valid_bytes += code_point_length_in_bytes;
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:47:34 +00:00
|
|
|
size_t Utf8View::calculate_length() const
|
2020-05-17 11:02:27 +00:00
|
|
|
{
|
|
|
|
size_t length = 0;
|
2020-12-20 23:09:48 +00:00
|
|
|
for ([[maybe_unused]] auto code_point : *this) {
|
2020-05-17 11:02:27 +00:00
|
|
|
++length;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2021-03-21 20:31:15 +00:00
|
|
|
bool Utf8View::starts_with(const Utf8View& start) const
|
|
|
|
{
|
|
|
|
if (start.is_empty())
|
|
|
|
return true;
|
|
|
|
if (is_empty())
|
|
|
|
return false;
|
|
|
|
if (start.length() > length())
|
|
|
|
return false;
|
|
|
|
if (begin_ptr() == start.begin_ptr())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (auto k = begin(), l = start.begin(); l != start.end(); ++k, ++l) {
|
|
|
|
if (*k != *l)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:45:52 +00:00
|
|
|
Utf8CodePointIterator::Utf8CodePointIterator(const unsigned char* ptr, size_t length)
|
2019-08-27 21:57:15 +00:00
|
|
|
: m_ptr(ptr)
|
|
|
|
, m_length(length)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:45:52 +00:00
|
|
|
bool Utf8CodePointIterator::operator==(const Utf8CodePointIterator& other) const
|
2019-08-27 21:57:15 +00:00
|
|
|
{
|
|
|
|
return m_ptr == other.m_ptr && m_length == other.m_length;
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:45:52 +00:00
|
|
|
bool Utf8CodePointIterator::operator!=(const Utf8CodePointIterator& other) const
|
2019-08-27 21:57:15 +00:00
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:45:52 +00:00
|
|
|
Utf8CodePointIterator& Utf8CodePointIterator::operator++()
|
2019-08-27 21:57:15 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_length > 0);
|
AK: Fix buffer overrun in Utf8CodepointIterator::operator++
The old implementation tried to move forward as long as the current
byte looks like a UTF-8 character continuation byte (has its two
most significant bits set to 10). This is correct as long as we assume
the string is actually valid UTF-8, which we do (we also have a separate
method that can check whether it is the case).
We can't, however, assume that the data after the end of our string
is also valid UTF-8 (in fact, we're not even allowed to look at data
outside out string, but it happens to a valid memory region most of
the time). If the byte after the end of our string also has its most
significant bits set to 10, we would move one byte forward, and then
fail the m_length > 0 assertion.
One way to fix this would be to add a length check inside the loop
condition. The other one, implemented in this commit, is to reimplement
the whole function in terms of decode_first_byte(), which gives us
the length as encoded in the first byte. This also brings it more
in line with the other functions around it that do UTF-8 decoding.
2019-09-08 15:24:54 +00:00
|
|
|
|
2021-01-01 23:28:24 +00:00
|
|
|
size_t code_point_length_in_bytes = 0;
|
AK: Fix buffer overrun in Utf8CodepointIterator::operator++
The old implementation tried to move forward as long as the current
byte looks like a UTF-8 character continuation byte (has its two
most significant bits set to 10). This is correct as long as we assume
the string is actually valid UTF-8, which we do (we also have a separate
method that can check whether it is the case).
We can't, however, assume that the data after the end of our string
is also valid UTF-8 (in fact, we're not even allowed to look at data
outside out string, but it happens to a valid memory region most of
the time). If the byte after the end of our string also has its most
significant bits set to 10, we would move one byte forward, and then
fail the m_length > 0 assertion.
One way to fix this would be to add a length check inside the loop
condition. The other one, implemented in this commit, is to reimplement
the whole function in terms of decode_first_byte(), which gives us
the length as encoded in the first byte. This also brings it more
in line with the other functions around it that do UTF-8 decoding.
2019-09-08 15:24:54 +00:00
|
|
|
u32 value;
|
2020-08-05 20:31:20 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
|
AK: Fix buffer overrun in Utf8CodepointIterator::operator++
The old implementation tried to move forward as long as the current
byte looks like a UTF-8 character continuation byte (has its two
most significant bits set to 10). This is correct as long as we assume
the string is actually valid UTF-8, which we do (we also have a separate
method that can check whether it is the case).
We can't, however, assume that the data after the end of our string
is also valid UTF-8 (in fact, we're not even allowed to look at data
outside out string, but it happens to a valid memory region most of
the time). If the byte after the end of our string also has its most
significant bits set to 10, we would move one byte forward, and then
fail the m_length > 0 assertion.
One way to fix this would be to add a length check inside the loop
condition. The other one, implemented in this commit, is to reimplement
the whole function in terms of decode_first_byte(), which gives us
the length as encoded in the first byte. This also brings it more
in line with the other functions around it that do UTF-8 decoding.
2019-09-08 15:24:54 +00:00
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(first_byte_makes_sense);
|
AK: Fix buffer overrun in Utf8CodepointIterator::operator++
The old implementation tried to move forward as long as the current
byte looks like a UTF-8 character continuation byte (has its two
most significant bits set to 10). This is correct as long as we assume
the string is actually valid UTF-8, which we do (we also have a separate
method that can check whether it is the case).
We can't, however, assume that the data after the end of our string
is also valid UTF-8 (in fact, we're not even allowed to look at data
outside out string, but it happens to a valid memory region most of
the time). If the byte after the end of our string also has its most
significant bits set to 10, we would move one byte forward, and then
fail the m_length > 0 assertion.
One way to fix this would be to add a length check inside the loop
condition. The other one, implemented in this commit, is to reimplement
the whole function in terms of decode_first_byte(), which gives us
the length as encoded in the first byte. This also brings it more
in line with the other functions around it that do UTF-8 decoding.
2019-09-08 15:24:54 +00:00
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(code_point_length_in_bytes <= m_length);
|
2020-08-05 20:31:20 +00:00
|
|
|
m_ptr += code_point_length_in_bytes;
|
|
|
|
m_length -= code_point_length_in_bytes;
|
2019-08-27 21:57:15 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:45:52 +00:00
|
|
|
size_t Utf8CodePointIterator::code_point_length_in_bytes() const
|
2019-10-18 20:49:23 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_length > 0);
|
2021-01-01 23:28:24 +00:00
|
|
|
size_t code_point_length_in_bytes = 0;
|
2019-10-18 20:49:23 +00:00
|
|
|
u32 value;
|
2020-08-05 20:31:20 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(first_byte_makes_sense);
|
2020-08-05 20:31:20 +00:00
|
|
|
return code_point_length_in_bytes;
|
2019-10-18 20:49:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 07:45:52 +00:00
|
|
|
u32 Utf8CodePointIterator::operator*() const
|
2019-08-27 21:57:15 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_length > 0);
|
2019-08-27 21:57:15 +00:00
|
|
|
|
2020-08-05 20:31:20 +00:00
|
|
|
u32 code_point_value_so_far = 0;
|
2021-01-01 23:28:24 +00:00
|
|
|
size_t code_point_length_in_bytes = 0;
|
2019-08-27 21:57:15 +00:00
|
|
|
|
2020-08-05 20:31:20 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(m_ptr[0], code_point_length_in_bytes, code_point_value_so_far);
|
2020-10-07 12:02:42 +00:00
|
|
|
if (!first_byte_makes_sense)
|
2021-01-01 23:28:24 +00:00
|
|
|
dbgln("First byte doesn't make sense, bytes: {}", StringView { (const char*)m_ptr, m_length });
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(first_byte_makes_sense);
|
2020-10-07 12:02:42 +00:00
|
|
|
if (code_point_length_in_bytes > m_length)
|
|
|
|
dbgln("Not enough bytes (need {}, have {}), first byte is: {:#02x}, '{}'", code_point_length_in_bytes, m_length, m_ptr[0], (const char*)m_ptr);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(code_point_length_in_bytes <= m_length);
|
2019-08-27 21:57:15 +00:00
|
|
|
|
2021-01-01 23:28:24 +00:00
|
|
|
for (size_t offset = 1; offset < code_point_length_in_bytes; offset++) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_ptr[offset] >> 6 == 2);
|
2020-08-05 20:31:20 +00:00
|
|
|
code_point_value_so_far <<= 6;
|
|
|
|
code_point_value_so_far |= m_ptr[offset] & 63;
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 20:31:20 +00:00
|
|
|
return code_point_value_so_far;
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 07:45:52 +00:00
|
|
|
Optional<u32> Utf8CodePointIterator::peek(size_t offset) const
|
2021-05-23 22:29:16 +00:00
|
|
|
{
|
|
|
|
if (offset == 0) {
|
|
|
|
if (this->done())
|
|
|
|
return {};
|
|
|
|
return this->operator*();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto new_iterator = *this;
|
|
|
|
for (size_t index = 0; index < offset; ++index) {
|
|
|
|
++new_iterator;
|
|
|
|
if (new_iterator.done())
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return *new_iterator;
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|