mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:37:35 +00:00
AK: Use size_t in methods of Utf8View.
This commit is contained in:
parent
3aaece8733
commit
2927656d85
2 changed files with 15 additions and 15 deletions
|
@ -57,7 +57,7 @@ const unsigned char* Utf8View::end_ptr() const
|
||||||
|
|
||||||
Utf8CodepointIterator Utf8View::begin() const
|
Utf8CodepointIterator Utf8View::begin() const
|
||||||
{
|
{
|
||||||
return { begin_ptr(), (int)m_string.length() };
|
return { begin_ptr(), m_string.length() };
|
||||||
}
|
}
|
||||||
|
|
||||||
Utf8CodepointIterator Utf8View::end() const
|
Utf8CodepointIterator Utf8View::end() const
|
||||||
|
@ -81,7 +81,7 @@ Utf8View Utf8View::substring_view(int byte_offset, int byte_length) const
|
||||||
|
|
||||||
static inline bool decode_first_byte(
|
static inline bool decode_first_byte(
|
||||||
unsigned char byte,
|
unsigned char byte,
|
||||||
int& out_code_point_length_in_bytes,
|
size_t& out_code_point_length_in_bytes,
|
||||||
u32& out_value)
|
u32& out_value)
|
||||||
{
|
{
|
||||||
if ((byte & 128) == 0) {
|
if ((byte & 128) == 0) {
|
||||||
|
@ -115,13 +115,13 @@ bool Utf8View::validate(size_t& valid_bytes) const
|
||||||
{
|
{
|
||||||
valid_bytes = 0;
|
valid_bytes = 0;
|
||||||
for (auto ptr = begin_ptr(); ptr < end_ptr(); ptr++) {
|
for (auto ptr = begin_ptr(); ptr < end_ptr(); ptr++) {
|
||||||
int code_point_length_in_bytes;
|
size_t code_point_length_in_bytes;
|
||||||
u32 value;
|
u32 value;
|
||||||
bool first_byte_makes_sense = decode_first_byte(*ptr, code_point_length_in_bytes, value);
|
bool first_byte_makes_sense = decode_first_byte(*ptr, code_point_length_in_bytes, value);
|
||||||
if (!first_byte_makes_sense)
|
if (!first_byte_makes_sense)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (int i = 1; i < code_point_length_in_bytes; i++) {
|
for (size_t i = 1; i < code_point_length_in_bytes; i++) {
|
||||||
ptr++;
|
ptr++;
|
||||||
if (ptr >= end_ptr())
|
if (ptr >= end_ptr())
|
||||||
return false;
|
return false;
|
||||||
|
@ -144,7 +144,7 @@ size_t Utf8View::calculate_length() const
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
Utf8CodepointIterator::Utf8CodepointIterator(const unsigned char* ptr, int length)
|
Utf8CodepointIterator::Utf8CodepointIterator(const unsigned char* ptr, size_t length)
|
||||||
: m_ptr(ptr)
|
: m_ptr(ptr)
|
||||||
, m_length(length)
|
, m_length(length)
|
||||||
{
|
{
|
||||||
|
@ -164,7 +164,7 @@ Utf8CodepointIterator& Utf8CodepointIterator::operator++()
|
||||||
{
|
{
|
||||||
ASSERT(m_length > 0);
|
ASSERT(m_length > 0);
|
||||||
|
|
||||||
int code_point_length_in_bytes = 0;
|
size_t code_point_length_in_bytes = 0;
|
||||||
u32 value;
|
u32 value;
|
||||||
bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
|
bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
|
||||||
|
|
||||||
|
@ -177,10 +177,10 @@ Utf8CodepointIterator& Utf8CodepointIterator::operator++()
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Utf8CodepointIterator::code_point_length_in_bytes() const
|
size_t Utf8CodepointIterator::code_point_length_in_bytes() const
|
||||||
{
|
{
|
||||||
ASSERT(m_length > 0);
|
ASSERT(m_length > 0);
|
||||||
int code_point_length_in_bytes = 0;
|
size_t code_point_length_in_bytes = 0;
|
||||||
u32 value;
|
u32 value;
|
||||||
bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
|
bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
|
||||||
ASSERT(first_byte_makes_sense);
|
ASSERT(first_byte_makes_sense);
|
||||||
|
@ -192,17 +192,17 @@ u32 Utf8CodepointIterator::operator*() const
|
||||||
ASSERT(m_length > 0);
|
ASSERT(m_length > 0);
|
||||||
|
|
||||||
u32 code_point_value_so_far = 0;
|
u32 code_point_value_so_far = 0;
|
||||||
int code_point_length_in_bytes = 0;
|
size_t code_point_length_in_bytes = 0;
|
||||||
|
|
||||||
bool first_byte_makes_sense = decode_first_byte(m_ptr[0], code_point_length_in_bytes, code_point_value_so_far);
|
bool first_byte_makes_sense = decode_first_byte(m_ptr[0], code_point_length_in_bytes, code_point_value_so_far);
|
||||||
if (!first_byte_makes_sense)
|
if (!first_byte_makes_sense)
|
||||||
dbgln("First byte doesn't make sense, bytes: {}", StringView { (const char*)m_ptr, (size_t)m_length });
|
dbgln("First byte doesn't make sense, bytes: {}", StringView { (const char*)m_ptr, m_length });
|
||||||
ASSERT(first_byte_makes_sense);
|
ASSERT(first_byte_makes_sense);
|
||||||
if (code_point_length_in_bytes > m_length)
|
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);
|
dbgln("Not enough bytes (need {}, have {}), first byte is: {:#02x}, '{}'", code_point_length_in_bytes, m_length, m_ptr[0], (const char*)m_ptr);
|
||||||
ASSERT(code_point_length_in_bytes <= m_length);
|
ASSERT(code_point_length_in_bytes <= m_length);
|
||||||
|
|
||||||
for (int offset = 1; offset < code_point_length_in_bytes; offset++) {
|
for (size_t offset = 1; offset < code_point_length_in_bytes; offset++) {
|
||||||
ASSERT(m_ptr[offset] >> 6 == 2);
|
ASSERT(m_ptr[offset] >> 6 == 2);
|
||||||
code_point_value_so_far <<= 6;
|
code_point_value_so_far <<= 6;
|
||||||
code_point_value_so_far |= m_ptr[offset] & 63;
|
code_point_value_so_far |= m_ptr[offset] & 63;
|
||||||
|
|
|
@ -50,13 +50,13 @@ public:
|
||||||
return m_ptr - other.m_ptr;
|
return m_ptr - other.m_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int code_point_length_in_bytes() const;
|
size_t code_point_length_in_bytes() const;
|
||||||
bool done() const { return !m_length; }
|
bool done() const { return m_length == 0; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Utf8CodepointIterator(const unsigned char*, int);
|
Utf8CodepointIterator(const unsigned char*, size_t);
|
||||||
const unsigned char* m_ptr { nullptr };
|
const unsigned char* m_ptr { nullptr };
|
||||||
int m_length { -1 };
|
size_t m_length;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Utf8View {
|
class Utf8View {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue