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>
|
2022-04-12 16:25:41 +00:00
|
|
|
#include <AK/Debug.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
|
|
|
|
2024-07-16 16:54:56 +00:00
|
|
|
#include <simdutf.h>
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
namespace AK {
|
|
|
|
|
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;
|
2021-05-30 16:52:24 +00:00
|
|
|
current_offset += iterator.underlying_code_point_length_in_bytes();
|
2021-05-18 14:09:20 +00:00
|
|
|
}
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:57:20 +00:00
|
|
|
Utf8CodePointIterator Utf8View::iterator_at_byte_offset_without_validation(size_t byte_offset) const
|
|
|
|
{
|
|
|
|
return Utf8CodePointIterator { reinterpret_cast<u8 const*>(m_string.characters_without_null_termination()) + byte_offset, m_string.length() - byte_offset };
|
|
|
|
}
|
|
|
|
|
2021-08-16 14:27:26 +00:00
|
|
|
size_t Utf8View::byte_offset_of(size_t code_point_offset) const
|
|
|
|
{
|
|
|
|
size_t byte_offset = 0;
|
|
|
|
|
|
|
|
for (auto it = begin(); !it.done(); ++it) {
|
|
|
|
if (code_point_offset == 0)
|
|
|
|
return byte_offset;
|
|
|
|
|
|
|
|
byte_offset += it.underlying_code_point_length_in_bytes();
|
|
|
|
--code_point_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return byte_offset;
|
|
|
|
}
|
|
|
|
|
2021-06-01 08:01:11 +00:00
|
|
|
Utf8View Utf8View::unicode_substring_view(size_t code_point_offset, size_t code_point_length) const
|
2021-05-17 19:04:37 +00:00
|
|
|
{
|
2021-06-01 08:01:11 +00:00
|
|
|
if (code_point_length == 0)
|
2021-05-17 19:04:37 +00:00
|
|
|
return {};
|
|
|
|
|
2021-06-01 08:01:11 +00:00
|
|
|
size_t code_point_index = 0, offset_in_bytes = 0;
|
2021-05-17 19:04:37 +00:00
|
|
|
for (auto iterator = begin(); !iterator.done(); ++iterator) {
|
2021-06-01 08:01:11 +00:00
|
|
|
if (code_point_index == code_point_offset)
|
2021-05-17 19:04:37 +00:00
|
|
|
offset_in_bytes = byte_offset_of(iterator);
|
2021-06-01 08:01:11 +00:00
|
|
|
if (code_point_index == code_point_offset + code_point_length - 1) {
|
2021-05-17 19:04:37 +00:00
|
|
|
size_t length_in_bytes = byte_offset_of(++iterator) - offset_in_bytes;
|
|
|
|
return substring_view(offset_in_bytes, length_in_bytes);
|
|
|
|
}
|
2021-06-01 08:01:11 +00:00
|
|
|
++code_point_index;
|
2021-05-17 19:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:47:34 +00:00
|
|
|
size_t Utf8View::calculate_length() const
|
2020-05-17 11:02:27 +00:00
|
|
|
{
|
2024-07-16 16:54:56 +00:00
|
|
|
// FIXME: simdutf's code point length method assumes valid UTF-8, whereas Utf8View uses U+FFFD as a replacement
|
|
|
|
// for invalid code points. If we change Utf8View to only accept valid encodings as an invariant, we can
|
|
|
|
// remove this branch.
|
|
|
|
if (validate()) [[likely]]
|
|
|
|
return simdutf::count_utf8(m_string.characters_without_null_termination(), m_string.length());
|
|
|
|
|
2020-05-17 11:02:27 +00:00
|
|
|
size_t length = 0;
|
2023-03-13 13:39:45 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < m_string.length(); ++length) {
|
|
|
|
auto [byte_length, code_point, is_valid] = decode_leading_byte(static_cast<u8>(m_string[i]));
|
|
|
|
|
|
|
|
// Similar to Utf8CodePointIterator::operator++, if the byte is invalid, try the next byte.
|
|
|
|
i += is_valid ? byte_length : 1;
|
2020-05-17 11:02:27 +00:00
|
|
|
}
|
2023-03-13 13:39:45 +00:00
|
|
|
|
2020-05-17 11:02:27 +00:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool Utf8View::starts_with(Utf8View const& start) const
|
2021-03-21 20:31:15 +00:00
|
|
|
{
|
|
|
|
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-16 11:17:03 +00:00
|
|
|
bool Utf8View::contains(u32 needle) const
|
|
|
|
{
|
2024-10-27 13:03:16 +00:00
|
|
|
if (needle <= 0x7f) {
|
|
|
|
// OPTIMIZATION: Fast path for ASCII
|
|
|
|
for (u8 code_point : as_string()) {
|
|
|
|
if (code_point == needle)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (u32 code_point : *this) {
|
|
|
|
if (code_point == needle)
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-16 11:17:03 +00:00
|
|
|
}
|
2024-10-27 13:03:16 +00:00
|
|
|
|
2021-06-16 11:17:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
Utf8View Utf8View::trim(Utf8View const& characters, TrimMode mode) const
|
2021-06-16 11:17:03 +00:00
|
|
|
{
|
|
|
|
size_t substring_start = 0;
|
2021-07-16 16:40:46 +00:00
|
|
|
size_t substring_length = byte_length();
|
2021-06-16 11:17:03 +00:00
|
|
|
|
|
|
|
if (mode == TrimMode::Left || mode == TrimMode::Both) {
|
2021-07-16 16:40:46 +00:00
|
|
|
for (auto code_point = begin(); code_point != end(); ++code_point) {
|
2021-06-16 11:17:03 +00:00
|
|
|
if (substring_length == 0)
|
|
|
|
return {};
|
2021-07-16 16:40:46 +00:00
|
|
|
if (!characters.contains(*code_point))
|
2021-06-16 11:17:03 +00:00
|
|
|
break;
|
2021-07-16 16:40:46 +00:00
|
|
|
substring_start += code_point.underlying_code_point_length_in_bytes();
|
|
|
|
substring_length -= code_point.underlying_code_point_length_in_bytes();
|
2021-06-16 11:17:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == TrimMode::Right || mode == TrimMode::Both) {
|
|
|
|
size_t seen_whitespace_length = 0;
|
2021-07-16 16:40:46 +00:00
|
|
|
for (auto code_point = begin(); code_point != end(); ++code_point) {
|
|
|
|
if (characters.contains(*code_point))
|
|
|
|
seen_whitespace_length += code_point.underlying_code_point_length_in_bytes();
|
2021-06-16 11:17:03 +00:00
|
|
|
else
|
|
|
|
seen_whitespace_length = 0;
|
|
|
|
}
|
|
|
|
if (seen_whitespace_length >= substring_length)
|
|
|
|
return {};
|
|
|
|
substring_length -= seen_whitespace_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return substring_view(substring_start, substring_length);
|
|
|
|
}
|
|
|
|
|
2024-07-16 16:54:56 +00:00
|
|
|
bool Utf8View::validate(size_t& valid_bytes, AllowSurrogates allow_surrogates) const
|
|
|
|
{
|
|
|
|
auto result = simdutf::validate_utf8_with_errors(m_string.characters_without_null_termination(), m_string.length());
|
|
|
|
valid_bytes = result.count;
|
|
|
|
|
|
|
|
if (result.error == simdutf::SURROGATE && allow_surrogates == AllowSurrogates::Yes) {
|
|
|
|
valid_bytes += 3; // All surrogates have a UTF-8 byte length of 3.
|
|
|
|
|
|
|
|
size_t substring_valid_bytes = 0;
|
|
|
|
auto is_valid = substring_view(valid_bytes).validate(substring_valid_bytes, allow_surrogates);
|
|
|
|
|
|
|
|
valid_bytes += substring_valid_bytes;
|
|
|
|
return is_valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.error == simdutf::SUCCESS;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-02-20 13:08:40 +00:00
|
|
|
ErrorOr<void> Formatter<Utf8View>::format(FormatBuilder& builder, Utf8View const& string)
|
|
|
|
{
|
|
|
|
return Formatter<StringView>::format(builder, string.as_string());
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|