Skip to content

Commit

Permalink
[21_5] make string_u16 iterable with range based for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
jingkaimori authored May 20, 2024
1 parent f75cd8c commit 2e89509
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lolly/data/string_u16.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class string_u16 {
rep->resize (size);
return rep->a;
}
char16_t* begin () { return rep->a; }
char16_t* end () { return rep->a + rep->n; }

inline operator string_u16_view () {
return string_u16_view (rep->a, rep->n);
Expand Down
10 changes: 10 additions & 0 deletions tests/lolly/data/string_u16_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,14 @@ TEST_CASE ("test copy") {

string_u16 str3= copy (str1 (0, 3));
CHECK (str3 == u"str");
}

TEST_CASE ("test iteration") {
string_u16 str1 = u"s\0tr1";
int expectedIndex= 0;
for (const auto element : str1) {
CHECK_EQ (element, str1[expectedIndex]);
++expectedIndex;
}
CHECK_EQ (expectedIndex, N (str1));
}

0 comments on commit 2e89509

Please sign in to comment.