1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 09:27:35 +00:00

AK: Add a Utf8View type for iterating over UTF-8 codepoints

Utf8View wraps a StringView and implements begin() and end() that
return a Utf8CodepointIterator, which parses UTF-8-encoded Unicode
codepoints and returns them as 32-bit integers.

This is the first step towards supporting emojis in Serenity ^)
https://github.com/SerenityOS/serenity/issues/490
This commit is contained in:
Sergey Bugaev 2019-08-28 00:57:15 +03:00 committed by Andreas Kling
parent 970e0147f7
commit 5d3696174b
4 changed files with 241 additions and 1 deletions

48
AK/Utf8View.h Normal file
View file

@ -0,0 +1,48 @@
#pragma once
#include <AK/StringView.h>
#include <AK/Types.h>
namespace AK {
class Utf8View;
class Utf8CodepointIterator {
friend class Utf8View;
public:
~Utf8CodepointIterator() {}
bool operator==(const Utf8CodepointIterator&) const;
bool operator!=(const Utf8CodepointIterator&) const;
Utf8CodepointIterator& operator++();
u32 operator*() const;
private:
Utf8CodepointIterator(const unsigned char*, int);
const unsigned char* m_ptr { nullptr };
int m_length { -1 };
};
class Utf8View {
public:
explicit Utf8View(const StringView&);
~Utf8View() {}
const StringView& as_string() const { return m_string; }
Utf8CodepointIterator begin() const;
Utf8CodepointIterator end() const;
bool validate() const;
private:
const unsigned char* begin_ptr() const;
const unsigned char* end_ptr() const;
StringView m_string;
};
}
using AK::Utf8View;