Skip to content

Commit a2bcb2a

Browse files
trflynn89awesomekling
authored andcommitted
AK: Replace UTF-8 validation and length computation with simdutf
1 parent 3b96ef1 commit a2bcb2a

File tree

2 files changed

+31
-61
lines changed

2 files changed

+31
-61
lines changed

AK/Utf8View.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
* SPDX-License-Identifier: BSD-2-Clause
66
*/
77

8+
#define AK_DONT_REPLACE_STD
9+
810
#include <AK/Assertions.h>
911
#include <AK/Debug.h>
1012
#include <AK/Format.h>
1113
#include <AK/Utf8View.h>
1214

15+
#include <simdutf.h>
16+
1317
namespace AK {
1418

1519
Utf8CodePointIterator Utf8View::iterator_at_byte_offset(size_t byte_offset) const
@@ -72,6 +76,12 @@ Utf8View Utf8View::unicode_substring_view(size_t code_point_offset, size_t code_
7276

7377
size_t Utf8View::calculate_length() const
7478
{
79+
// FIXME: simdutf's code point length method assumes valid UTF-8, whereas Utf8View uses U+FFFD as a replacement
80+
// for invalid code points. If we change Utf8View to only accept valid encodings as an invariant, we can
81+
// remove this branch.
82+
if (validate()) [[likely]]
83+
return simdutf::count_utf8(m_string.characters_without_null_termination(), m_string.length());
84+
7585
size_t length = 0;
7686

7787
for (size_t i = 0; i < m_string.length(); ++length) {
@@ -143,6 +153,24 @@ Utf8View Utf8View::trim(Utf8View const& characters, TrimMode mode) const
143153
return substring_view(substring_start, substring_length);
144154
}
145155

156+
bool Utf8View::validate(size_t& valid_bytes, AllowSurrogates allow_surrogates) const
157+
{
158+
auto result = simdutf::validate_utf8_with_errors(m_string.characters_without_null_termination(), m_string.length());
159+
valid_bytes = result.count;
160+
161+
if (result.error == simdutf::SURROGATE && allow_surrogates == AllowSurrogates::Yes) {
162+
valid_bytes += 3; // All surrogates have a UTF-8 byte length of 3.
163+
164+
size_t substring_valid_bytes = 0;
165+
auto is_valid = substring_view(valid_bytes).validate(substring_valid_bytes, allow_surrogates);
166+
167+
valid_bytes += substring_valid_bytes;
168+
return is_valid;
169+
}
170+
171+
return result.error == simdutf::SUCCESS;
172+
}
173+
146174
Utf8CodePointIterator& Utf8CodePointIterator::operator++()
147175
{
148176
VERIFY(m_length > 0);

AK/Utf8View.h

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -121,41 +121,13 @@ class Utf8View {
121121
return m_length;
122122
}
123123

124-
constexpr bool validate(AllowSurrogates surrogates = AllowSurrogates::Yes) const
124+
bool validate(AllowSurrogates allow_surrogates = AllowSurrogates::Yes) const
125125
{
126126
size_t valid_bytes = 0;
127-
return validate(valid_bytes, surrogates);
127+
return validate(valid_bytes, allow_surrogates);
128128
}
129129

130-
constexpr bool validate(size_t& valid_bytes, AllowSurrogates surrogates = AllowSurrogates::Yes) const
131-
{
132-
valid_bytes = 0;
133-
134-
for (auto it = m_string.begin(); it != m_string.end(); ++it) {
135-
auto [byte_length, code_point, is_valid] = decode_leading_byte(static_cast<u8>(*it));
136-
if (!is_valid)
137-
return false;
138-
139-
for (size_t i = 1; i < byte_length; ++i) {
140-
if (++it == m_string.end())
141-
return false;
142-
143-
auto [code_point_bits, is_valid] = decode_continuation_byte(static_cast<u8>(*it));
144-
if (!is_valid)
145-
return false;
146-
147-
code_point <<= 6;
148-
code_point |= code_point_bits;
149-
}
150-
151-
if (!is_valid_code_point(code_point, byte_length, surrogates))
152-
return false;
153-
154-
valid_bytes += byte_length;
155-
}
156-
157-
return true;
158-
}
130+
bool validate(size_t& valid_bytes, AllowSurrogates allow_surrogates = AllowSurrogates::Yes) const;
159131

160132
private:
161133
friend class Utf8CodePointIterator;
@@ -198,36 +170,6 @@ class Utf8View {
198170
return { .is_valid = false };
199171
}
200172

201-
struct ContinuationByte {
202-
u32 code_point_bits { 0 };
203-
bool is_valid { false };
204-
};
205-
206-
static constexpr ContinuationByte decode_continuation_byte(u8 byte)
207-
{
208-
constexpr u8 continuation_byte_encoding_bits = 0b1000'0000;
209-
constexpr u8 continuation_byte_encoding_mask = 0b1100'0000;
210-
211-
if ((byte & continuation_byte_encoding_mask) == continuation_byte_encoding_bits) {
212-
byte &= ~continuation_byte_encoding_mask;
213-
return { byte, true };
214-
}
215-
216-
return { .is_valid = false };
217-
}
218-
219-
static constexpr bool is_valid_code_point(u32 code_point, size_t byte_length, AllowSurrogates surrogates = AllowSurrogates::Yes)
220-
{
221-
if (surrogates == AllowSurrogates::No && byte_length == 3 && code_point >= 0xD800 && code_point <= 0xDFFF)
222-
return false;
223-
for (auto const& data : utf8_encoded_byte_data) {
224-
if (code_point >= data.first_code_point && code_point <= data.last_code_point)
225-
return byte_length == data.byte_length;
226-
}
227-
228-
return false;
229-
}
230-
231173
StringView m_string;
232174
mutable size_t m_length { 0 };
233175
mutable bool m_have_length { false };

0 commit comments

Comments
 (0)