2020-01-18 11:38:21 +03:00
/*
2020-01-24 16:45:29 +03:00
* Copyright ( c ) 2019 - 2020 , Sergey Bugaev < bugaevc @ serenityos . org >
2021-05-17 22:04:37 +03:00
* Copyright ( c ) 2021 , Max Wipfli < mail @ maxwipfli . ch >
2020-01-18 11:38:21 +03:00
*
2021-04-22 11:24:48 +03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 11:38:21 +03:00
*/
2020-02-14 23:41:10 +03:00
# include <AK/Assertions.h>
2022-04-12 19:25:41 +03:00
# include <AK/Debug.h>
2021-03-12 19:29:37 +03:00
# include <AK/Format.h>
2020-02-14 23:41:10 +03:00
# include <AK/Utf8View.h>
2019-08-28 00:57:15 +03:00
namespace AK {
2021-06-01 10:45:52 +03:00
Utf8CodePointIterator Utf8View : : iterator_at_byte_offset ( size_t byte_offset ) const
2021-05-18 17:09:20 +03:00
{
size_t current_offset = 0 ;
for ( auto iterator = begin ( ) ; ! iterator . done ( ) ; + + iterator ) {
if ( current_offset > = byte_offset )
return iterator ;
2021-05-30 19:52:24 +03:00
current_offset + = iterator . underlying_code_point_length_in_bytes ( ) ;
2021-05-18 17:09:20 +03:00
}
return end ( ) ;
}
2022-11-24 16:57:20 +03: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 } ;
}
2022-04-01 20:58:27 +03:00
size_t Utf8View : : byte_offset_of ( Utf8CodePointIterator const & it ) const
2019-09-04 23:40:36 +03:00
{
2021-02-23 22:42:32 +03:00
VERIFY ( it . m_ptr > = begin_ptr ( ) ) ;
VERIFY ( it . m_ptr < = end_ptr ( ) ) ;
2019-09-04 23:40:36 +03:00
return it . m_ptr - begin_ptr ( ) ;
}
2021-08-16 17:27:26 +03: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 11:01:11 +03:00
Utf8View Utf8View : : unicode_substring_view ( size_t code_point_offset , size_t code_point_length ) const
2021-05-17 22:04:37 +03:00
{
2021-06-01 11:01:11 +03:00
if ( code_point_length = = 0 )
2021-05-17 22:04:37 +03:00
return { } ;
2021-06-01 11:01:11 +03:00
size_t code_point_index = 0 , offset_in_bytes = 0 ;
2021-05-17 22:04:37 +03:00
for ( auto iterator = begin ( ) ; ! iterator . done ( ) ; + + iterator ) {
2021-06-01 11:01:11 +03:00
if ( code_point_index = = code_point_offset )
2021-05-17 22:04:37 +03:00
offset_in_bytes = byte_offset_of ( iterator ) ;
2021-06-01 11:01:11 +03:00
if ( code_point_index = = code_point_offset + code_point_length - 1 ) {
2021-05-17 22:04:37 +03: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 11:01:11 +03:00
+ + code_point_index ;
2021-05-17 22:04:37 +03:00
}
VERIFY_NOT_REACHED ( ) ;
}
2020-10-20 18:47:34 +03:00
size_t Utf8View : : calculate_length ( ) const
2020-05-17 14:02:27 +03:00
{
size_t length = 0 ;
2023-03-13 16:39:45 +03: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 14:02:27 +03:00
}
2023-03-13 16:39:45 +03:00
2020-05-17 14:02:27 +03:00
return length ;
}
2022-04-01 20:58:27 +03:00
bool Utf8View : : starts_with ( Utf8View const & start ) const
2021-03-21 23:31:15 +03: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 14:17:03 +03:00
bool Utf8View : : contains ( u32 needle ) const
{
for ( u32 code_point : * this ) {
if ( code_point = = needle )
return true ;
}
return false ;
}
2022-04-01 20:58:27 +03:00
Utf8View Utf8View : : trim ( Utf8View const & characters , TrimMode mode ) const
2021-06-16 14:17:03 +03:00
{
size_t substring_start = 0 ;
2021-07-16 19:40:46 +03:00
size_t substring_length = byte_length ( ) ;
2021-06-16 14:17:03 +03:00
if ( mode = = TrimMode : : Left | | mode = = TrimMode : : Both ) {
2021-07-16 19:40:46 +03:00
for ( auto code_point = begin ( ) ; code_point ! = end ( ) ; + + code_point ) {
2021-06-16 14:17:03 +03:00
if ( substring_length = = 0 )
return { } ;
2021-07-16 19:40:46 +03:00
if ( ! characters . contains ( * code_point ) )
2021-06-16 14:17:03 +03:00
break ;
2021-07-16 19:40:46 +03:00
substring_start + = code_point . underlying_code_point_length_in_bytes ( ) ;
substring_length - = code_point . underlying_code_point_length_in_bytes ( ) ;
2021-06-16 14:17:03 +03:00
}
}
if ( mode = = TrimMode : : Right | | mode = = TrimMode : : Both ) {
size_t seen_whitespace_length = 0 ;
2021-07-16 19:40:46 +03: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 14:17:03 +03: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 ) ;
}
2021-06-01 10:45:52 +03:00
Utf8CodePointIterator & Utf8CodePointIterator : : operator + + ( )
2019-08-28 00:57:15 +03:00
{
2021-02-23 22:42:32 +03: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 18:24:54 +03:00
2021-05-30 19:52:24 +03:00
size_t code_point_length_in_bytes = underlying_code_point_length_in_bytes ( ) ;
if ( code_point_length_in_bytes > m_length ) {
// We don't have enough data for the next code point. Skip one character and try again.
// The rest of the code will output replacement characters as needed for any eventual extension bytes we might encounter afterwards.
2022-04-12 19:25:41 +03:00
dbgln_if ( UTF8_DEBUG , " Expected code point size {} is too big for the remaining length {}. Moving forward one byte. " , code_point_length_in_bytes , m_length ) ;
2021-05-30 19:52:24 +03:00
m_ptr + = 1 ;
m_length - = 1 ;
return * this ;
}
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 18:24:54 +03:00
2020-08-05 23:31:20 +03:00
m_ptr + = code_point_length_in_bytes ;
m_length - = code_point_length_in_bytes ;
2019-08-28 00:57:15 +03:00
return * this ;
}
2021-05-30 19:52:24 +03:00
size_t Utf8CodePointIterator : : underlying_code_point_length_in_bytes ( ) const
2019-10-18 23:49:23 +03:00
{
2021-02-23 22:42:32 +03:00
VERIFY ( m_length > 0 ) ;
2023-03-03 15:56:24 +03:00
auto [ code_point_length_in_bytes , value , first_byte_makes_sense ] = Utf8View : : decode_leading_byte ( * m_ptr ) ;
2021-05-30 19:52:24 +03:00
// If any of these tests fail, we will output a replacement character for this byte and treat it as a code point of size 1.
if ( ! first_byte_makes_sense )
return 1 ;
if ( code_point_length_in_bytes > m_length )
return 1 ;
for ( size_t offset = 1 ; offset < code_point_length_in_bytes ; offset + + ) {
if ( m_ptr [ offset ] > > 6 ! = 2 )
return 1 ;
}
2020-08-05 23:31:20 +03:00
return code_point_length_in_bytes ;
2019-10-18 23:49:23 +03:00
}
2021-05-30 19:52:24 +03:00
ReadonlyBytes Utf8CodePointIterator : : underlying_code_point_bytes ( ) const
{
return { m_ptr , underlying_code_point_length_in_bytes ( ) } ;
}
2021-06-01 10:45:52 +03:00
u32 Utf8CodePointIterator : : operator * ( ) const
2019-08-28 00:57:15 +03:00
{
2021-02-23 22:42:32 +03:00
VERIFY ( m_length > 0 ) ;
2023-03-03 15:56:24 +03:00
auto [ code_point_length_in_bytes , code_point_value_so_far , first_byte_makes_sense ] = Utf8View : : decode_leading_byte ( * m_ptr ) ;
2021-05-30 19:52:24 +03:00
if ( ! first_byte_makes_sense ) {
// The first byte of the code point doesn't make sense: output a replacement character
2022-04-12 19:25:41 +03:00
dbgln_if ( UTF8_DEBUG , " First byte doesn't make sense: {:#02x}. " , m_ptr [ 0 ] ) ;
2021-05-30 19:52:24 +03:00
return 0xFFFD ;
}
if ( code_point_length_in_bytes > m_length ) {
// There is not enough data left for the full code point: output a replacement character
2022-04-12 19:25:41 +03:00
dbgln_if ( UTF8_DEBUG , " Not enough bytes (need {}, have {}), first byte is: {:#02x}. " , code_point_length_in_bytes , m_length , m_ptr [ 0 ] ) ;
2021-05-30 19:52:24 +03:00
return 0xFFFD ;
}
2019-08-28 00:57:15 +03:00
2021-01-02 02:28:24 +03:00
for ( size_t offset = 1 ; offset < code_point_length_in_bytes ; offset + + ) {
2021-05-30 19:52:24 +03:00
if ( m_ptr [ offset ] > > 6 ! = 2 ) {
// One of the extension bytes of the code point doesn't make sense: output a replacement character
2022-04-12 19:25:41 +03:00
dbgln_if ( UTF8_DEBUG , " Extension byte {:#02x} in {} position after first byte {:#02x} doesn't make sense. " , m_ptr [ offset ] , offset , m_ptr [ 0 ] ) ;
2021-05-30 19:52:24 +03:00
return 0xFFFD ;
}
2020-08-05 23:31:20 +03:00
code_point_value_so_far < < = 6 ;
code_point_value_so_far | = m_ptr [ offset ] & 63 ;
2019-08-28 00:57:15 +03:00
}
2022-09-12 19:25:20 +03:00
if ( code_point_value_so_far > 0x10FFFF ) {
dbgln_if ( UTF8_DEBUG , " Multi-byte sequence is otherwise valid, but code point {:#x} is not permissible. " , code_point_value_so_far ) ;
return 0xFFFD ;
}
2020-08-05 23:31:20 +03:00
return code_point_value_so_far ;
2019-08-28 00:57:15 +03:00
}
2021-06-01 10:45:52 +03:00
Optional < u32 > Utf8CodePointIterator : : peek ( size_t offset ) const
2021-05-24 01:29:16 +03: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 16:08:40 +03:00
ErrorOr < void > Formatter < Utf8View > : : format ( FormatBuilder & builder , Utf8View const & string )
{
return Formatter < StringView > : : format ( builder , string . as_string ( ) ) ;
}
2019-08-28 00:57:15 +03:00
}