mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:17:35 +00:00
AK: Add some more utility methods to Utf8View
This commit is contained in:
parent
95fe775d81
commit
c379f43d2a
2 changed files with 27 additions and 1 deletions
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
Utf8View::Utf8View(const String& string)
|
||||||
|
: m_string(string)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
Utf8View::Utf8View(const StringView& string)
|
Utf8View::Utf8View(const StringView& string)
|
||||||
: m_string(string)
|
: m_string(string)
|
||||||
{
|
{
|
||||||
|
@ -14,7 +19,7 @@ const unsigned char* Utf8View::begin_ptr() const
|
||||||
|
|
||||||
const unsigned char* Utf8View::end_ptr() const
|
const unsigned char* Utf8View::end_ptr() const
|
||||||
{
|
{
|
||||||
return (const unsigned char*)m_string.characters_without_null_termination() + m_string.length();
|
return begin_ptr() + m_string.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
Utf8CodepointIterator Utf8View::begin() const
|
Utf8CodepointIterator Utf8View::begin() const
|
||||||
|
@ -27,6 +32,20 @@ Utf8CodepointIterator Utf8View::end() const
|
||||||
return { end_ptr(), 0 };
|
return { end_ptr(), 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Utf8View::byte_offset_of(const Utf8CodepointIterator& it) const
|
||||||
|
{
|
||||||
|
ASSERT(it.m_ptr >= begin_ptr());
|
||||||
|
ASSERT(it.m_ptr <= end_ptr());
|
||||||
|
|
||||||
|
return it.m_ptr - begin_ptr();
|
||||||
|
}
|
||||||
|
|
||||||
|
Utf8View Utf8View::substring_view(int byte_offset, int byte_length) const
|
||||||
|
{
|
||||||
|
StringView string = m_string.substring_view(byte_offset, byte_length);
|
||||||
|
return Utf8View { string };
|
||||||
|
}
|
||||||
|
|
||||||
static inline bool decode_first_byte(
|
static inline bool decode_first_byte(
|
||||||
unsigned char byte,
|
unsigned char byte,
|
||||||
int& out_codepoint_length_in_bytes,
|
int& out_codepoint_length_in_bytes,
|
||||||
|
|
|
@ -26,6 +26,7 @@ private:
|
||||||
|
|
||||||
class Utf8View {
|
class Utf8View {
|
||||||
public:
|
public:
|
||||||
|
explicit Utf8View(const String&);
|
||||||
explicit Utf8View(const StringView&);
|
explicit Utf8View(const StringView&);
|
||||||
~Utf8View() {}
|
~Utf8View() {}
|
||||||
|
|
||||||
|
@ -34,6 +35,12 @@ public:
|
||||||
Utf8CodepointIterator begin() const;
|
Utf8CodepointIterator begin() const;
|
||||||
Utf8CodepointIterator end() const;
|
Utf8CodepointIterator end() const;
|
||||||
|
|
||||||
|
const unsigned char* bytes() const { return begin_ptr(); }
|
||||||
|
int byte_length() const { return m_string.length(); }
|
||||||
|
int byte_offset_of(const Utf8CodepointIterator&) const;
|
||||||
|
Utf8View substring_view(int byte_offset, int byte_length) const;
|
||||||
|
bool is_empty() const { return m_string.is_empty(); }
|
||||||
|
|
||||||
bool validate() const;
|
bool validate() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue