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-03 13:03:14 +00:00
# include <AK/CharacterTypes.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
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 } ;
}
2022-04-01 17:58:27 +00:00
size_t Utf8View : : byte_offset_of ( Utf8CodePointIterator const & 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-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 ( ) ;
}
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 + + ) {
2022-04-03 13:03:14 +00:00
size_t code_point_length_in_bytes = 0 ;
u32 code_point = 0 ;
bool first_byte_makes_sense = decode_first_byte ( * ptr , code_point_length_in_bytes , code_point ) ;
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 ;
2022-04-03 13:03:14 +00:00
code_point < < = 6 ;
code_point | = * ptr & 63 ;
2019-08-27 21:57:15 +00:00
}
2020-05-18 09:15:18 +00:00
2022-04-03 13:03:14 +00:00
if ( ! is_unicode ( code_point ) )
return false ;
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 ;
}
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
{
for ( u32 code_point : * this ) {
if ( code_point = = needle )
return true ;
}
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 ) ;
}
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-05-30 16:52:24 +00: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 16:25:41 +00: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 16:52:24 +00: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 15:24:54 +00:00
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-05-30 16:52:24 +00:00
size_t Utf8CodePointIterator : : underlying_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-05-30 16:52:24 +00: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 20:31:20 +00:00
return code_point_length_in_bytes ;
2019-10-18 20:49:23 +00:00
}
2021-05-30 16:52:24 +00:00
ReadonlyBytes Utf8CodePointIterator : : underlying_code_point_bytes ( ) const
{
return { m_ptr , underlying_code_point_length_in_bytes ( ) } ;
}
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 ) ;
2021-05-30 16:52:24 +00:00
if ( ! first_byte_makes_sense ) {
// The first byte of the code point doesn't make sense: output a replacement character
2022-04-12 16:25:41 +00:00
dbgln_if ( UTF8_DEBUG , " First byte doesn't make sense: {:#02x}. " , m_ptr [ 0 ] ) ;
2021-05-30 16:52:24 +00: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 16:25:41 +00: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 16:52:24 +00:00
return 0xFFFD ;
}
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-05-30 16:52:24 +00: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 16:25:41 +00: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 16:52:24 +00:00
return 0xFFFD ;
}
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
}
2022-09-12 16:25:20 +00: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 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 ;
}
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
}