summaryrefslogtreecommitdiff
path: root/Tests/AK
diff options
context:
space:
mode:
authorMax Wipfli <mail@maxwipfli.ch>2021-05-24 00:29:16 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-01 09:28:05 +0200
commit14506e8f5ebe5677f389435a7f22c7e36f903169 (patch)
tree3a78f5e8f241d3790e10231af5b803894d907830 /Tests/AK
parent31f6ba0952428d5bf3618202098831c61cb2a47f (diff)
downloadserenity-14506e8f5ebe5677f389435a7f22c7e36f903169.zip
AK: Implement Utf8CodepointIterator::peek(size_t)
This adds a peek method for Utf8CodepointIterator, which enables it to be used in some parsing cases where peeking is necessary. peek(0) is equivalent to operator*, expect that peek() does not contain any assertions and will just return an empty Optional<u32>. This also implements a test case for iterating UTF-8.
Diffstat (limited to 'Tests/AK')
-rw-r--r--Tests/AK/TestUtf8.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/Tests/AK/TestUtf8.cpp b/Tests/AK/TestUtf8.cpp
index 4a3f4e8029..522de3dcc9 100644
--- a/Tests/AK/TestUtf8.cpp
+++ b/Tests/AK/TestUtf8.cpp
@@ -67,3 +67,39 @@ TEST_CASE(validate_invalid_ut8)
EXPECT(!utf8_4.validate(valid_bytes));
EXPECT(valid_bytes == 0);
}
+
+TEST_CASE(iterate_utf8)
+{
+ Utf8View view("Some weird characters \u00A9\u266A\uA755");
+ Utf8CodepointIterator iterator = view.begin();
+
+ EXPECT(*iterator == 'S');
+ EXPECT(iterator.peek().has_value() && iterator.peek().value() == 'S');
+ EXPECT(iterator.peek(0).has_value() && iterator.peek(0).value() == 'S');
+ EXPECT(iterator.peek(1).has_value() && iterator.peek(1).value() == 'o');
+ EXPECT(iterator.peek(22).has_value() && iterator.peek(22).value() == 0x00A9);
+ EXPECT(iterator.peek(24).has_value() && iterator.peek(24).value() == 0xA755);
+ EXPECT(!iterator.peek(25).has_value());
+
+ ++iterator;
+
+ EXPECT(*iterator == 'o');
+ EXPECT(iterator.peek(23).has_value() && iterator.peek(23).value() == 0xA755);
+
+ for (size_t i = 0; i < 23; ++i)
+ ++iterator;
+
+ EXPECT(!iterator.done());
+ EXPECT(*iterator == 0xA755);
+ EXPECT(iterator.peek().has_value() && iterator.peek().value() == 0xA755);
+ EXPECT(!iterator.peek(1).has_value());
+
+ ++iterator;
+
+ EXPECT(iterator.done());
+ EXPECT(!iterator.peek(0).has_value());
+ EXPECT_CRASH("Dereferencing Utf8CodepointIterator which is already done.", [&iterator] {
+ *iterator;
+ return Test::Crash::Failure::DidNotCrash;
+ });
+}