2020-05-17 18:01:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-17 18:01:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Checked.h>
|
2023-02-20 13:08:40 +00:00
|
|
|
#include <AK/Format.h>
|
2020-05-17 18:01:45 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2020-10-20 15:47:34 +00:00
|
|
|
class Utf32View;
|
|
|
|
|
2021-06-01 07:46:19 +00:00
|
|
|
class Utf32CodePointIterator {
|
2020-10-20 15:47:34 +00:00
|
|
|
friend class Utf32View;
|
|
|
|
|
|
|
|
public:
|
2021-06-01 07:46:19 +00:00
|
|
|
Utf32CodePointIterator() = default;
|
|
|
|
~Utf32CodePointIterator() = default;
|
2020-10-20 15:47:34 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator==(Utf32CodePointIterator const& other) const
|
2020-10-20 15:47:34 +00:00
|
|
|
{
|
|
|
|
return m_ptr == other.m_ptr && m_length == other.m_length;
|
|
|
|
}
|
2021-06-01 07:46:19 +00:00
|
|
|
Utf32CodePointIterator& operator++()
|
2020-10-20 15:47:34 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_length > 0);
|
2020-10-20 15:47:34 +00:00
|
|
|
m_ptr++;
|
|
|
|
m_length--;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ssize_t operator-(Utf32CodePointIterator const& other) const
|
2020-10-20 15:47:34 +00:00
|
|
|
{
|
|
|
|
return m_ptr - other.m_ptr;
|
|
|
|
}
|
|
|
|
u32 operator*() const
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_length > 0);
|
2020-10-20 15:47:34 +00:00
|
|
|
return *m_ptr;
|
|
|
|
}
|
|
|
|
|
2023-02-20 19:04:42 +00:00
|
|
|
// NOTE: This returns {} if the peek is at or past EOF.
|
|
|
|
Optional<u32> peek(size_t offset = 0) const;
|
|
|
|
|
2020-10-20 15:47:34 +00:00
|
|
|
constexpr int code_point_length_in_bytes() const { return sizeof(u32); }
|
|
|
|
bool done() const { return !m_length; }
|
|
|
|
|
|
|
|
private:
|
2022-04-01 17:58:27 +00:00
|
|
|
Utf32CodePointIterator(u32 const* ptr, size_t length)
|
2020-10-20 15:47:34 +00:00
|
|
|
: m_ptr(ptr)
|
|
|
|
, m_length((ssize_t)length)
|
|
|
|
{
|
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
u32 const* m_ptr { nullptr };
|
2020-10-20 15:47:34 +00:00
|
|
|
ssize_t m_length { -1 };
|
|
|
|
};
|
|
|
|
|
2020-05-17 18:01:45 +00:00
|
|
|
class Utf32View {
|
|
|
|
public:
|
2021-06-01 07:46:19 +00:00
|
|
|
using Iterator = Utf32CodePointIterator;
|
2020-10-20 15:47:34 +00:00
|
|
|
|
2021-01-10 23:29:28 +00:00
|
|
|
Utf32View() = default;
|
2022-04-01 17:58:27 +00:00
|
|
|
Utf32View(u32 const* code_points, size_t length)
|
2020-08-05 20:31:20 +00:00
|
|
|
: m_code_points(code_points)
|
2020-05-17 18:01:45 +00:00
|
|
|
, m_length(length)
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(code_points || length == 0);
|
2020-05-17 18:01:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 07:46:19 +00:00
|
|
|
Utf32CodePointIterator begin() const
|
2020-10-20 15:47:34 +00:00
|
|
|
{
|
|
|
|
return { begin_ptr(), m_length };
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:46:19 +00:00
|
|
|
Utf32CodePointIterator end() const
|
2020-10-20 15:47:34 +00:00
|
|
|
{
|
|
|
|
return { end_ptr(), 0 };
|
|
|
|
}
|
|
|
|
|
2021-07-18 00:32:31 +00:00
|
|
|
u32 at(size_t index) const
|
|
|
|
{
|
|
|
|
VERIFY(index < m_length);
|
|
|
|
return m_code_points[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 operator[](size_t index) const { return at(index); }
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
u32 const* code_points() const { return m_code_points; }
|
2020-05-17 18:01:45 +00:00
|
|
|
bool is_empty() const { return m_length == 0; }
|
2021-07-18 00:31:18 +00:00
|
|
|
bool is_null() const { return !m_code_points; }
|
2020-05-17 18:01:45 +00:00
|
|
|
size_t length() const { return m_length; }
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
size_t iterator_offset(Utf32CodePointIterator const& it) const
|
2020-12-28 22:51:24 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(it.m_ptr >= m_code_points);
|
|
|
|
VERIFY(it.m_ptr < m_code_points + m_length);
|
2020-12-28 22:51:24 +00:00
|
|
|
return ((ptrdiff_t)it.m_ptr - (ptrdiff_t)m_code_points) / sizeof(u32);
|
|
|
|
}
|
|
|
|
|
2020-05-17 18:01:45 +00:00
|
|
|
Utf32View substring_view(size_t offset, size_t length) const
|
|
|
|
{
|
2021-07-17 19:49:16 +00:00
|
|
|
VERIFY(offset <= m_length);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!Checked<size_t>::addition_would_overflow(offset, length));
|
|
|
|
VERIFY((offset + length) <= m_length);
|
2020-08-05 20:31:20 +00:00
|
|
|
return Utf32View(m_code_points + offset, length);
|
2020-05-17 18:01:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 18:00:17 +00:00
|
|
|
Utf32View substring_view(size_t offset) const
|
|
|
|
{
|
|
|
|
return substring_view(offset, length() - offset);
|
|
|
|
}
|
|
|
|
|
2020-05-17 18:01:45 +00:00
|
|
|
private:
|
2022-04-01 17:58:27 +00:00
|
|
|
u32 const* begin_ptr() const
|
2020-10-20 15:47:34 +00:00
|
|
|
{
|
|
|
|
return m_code_points;
|
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
u32 const* end_ptr() const
|
2020-10-20 15:47:34 +00:00
|
|
|
{
|
|
|
|
return m_code_points + m_length;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
u32 const* m_code_points { nullptr };
|
2020-05-17 18:01:45 +00:00
|
|
|
size_t m_length { 0 };
|
|
|
|
};
|
|
|
|
|
2023-02-20 13:08:40 +00:00
|
|
|
template<>
|
|
|
|
struct Formatter<Utf32View> : Formatter<StringView> {
|
|
|
|
ErrorOr<void> format(FormatBuilder&, Utf32View const&);
|
|
|
|
};
|
|
|
|
|
2020-05-17 18:01:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-05-17 18:01:45 +00:00
|
|
|
using AK::Utf32View;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|