AK: Add DeprecatedStringCodePointIterator

This is a safe iterator over the underlying code points. It will be used
in Jakt to assist in the migration away from DeprecatedString.
This commit is contained in:
Andreas Kling 2023-01-27 16:26:57 +01:00
parent 0f4bbfdfb7
commit 2dc657c77e
Notes: sideshowbarker 2024-07-17 01:03:11 +09:00
4 changed files with 33 additions and 0 deletions

View file

@ -11,6 +11,7 @@
#include <AK/Function.h>
#include <AK/StdLibExtras.h>
#include <AK/StringView.h>
#include <AK/Utf8View.h>
#include <AK/Vector.h>
namespace AK {
@ -449,4 +450,9 @@ Vector<size_t> DeprecatedString::find_all(StringView needle) const
return StringUtils::find_all(*this, needle);
}
DeprecatedStringCodePointIterator DeprecatedString::code_points() const
{
return DeprecatedStringCodePointIterator(*this);
}
}

View file

@ -129,6 +129,8 @@ public:
[[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
[[nodiscard]] DeprecatedStringCodePointIterator code_points() const;
[[nodiscard]] DeprecatedString trim(StringView characters, TrimMode mode = TrimMode::Both) const
{
auto trimmed_view = StringUtils::trim(view(), characters, mode);

View file

@ -30,6 +30,7 @@ class JsonValue;
class StackInfo;
class DeprecatedFlyString;
class DeprecatedString;
class DeprecatedStringCodePointIterator;
class StringBuilder;
class StringImpl;
class StringView;
@ -156,6 +157,7 @@ using AK::CircularBuffer;
using AK::CircularQueue;
using AK::DeprecatedFlyString;
using AK::DeprecatedString;
using AK::DeprecatedStringCodePointIterator;
using AK::DoublyLinkedList;
using AK::Error;
using AK::ErrorOr;

View file

@ -130,9 +130,32 @@ private:
mutable bool m_have_length { false };
};
class DeprecatedStringCodePointIterator {
public:
Optional<u32> next()
{
if (m_it.done())
return {};
auto value = *m_it;
++m_it;
return value;
}
DeprecatedStringCodePointIterator(DeprecatedString string)
: m_string(move(string))
, m_it(Utf8View(m_string).begin())
{
}
private:
DeprecatedString m_string;
Utf8CodePointIterator m_it;
};
}
#if USING_AK_GLOBALLY
using AK::DeprecatedStringCodePointIterator;
using AK::Utf8CodePointIterator;
using AK::Utf8View;
#endif