Skip to content

Commit

Permalink
unicode: add utf8::skip
Browse files Browse the repository at this point in the history
Moving random bits of code into awlib. I remember needing this in the
past, so I'll add it here.
  • Loading branch information
Hedede committed Mar 11, 2024
1 parent ba7a0e3 commit 170a143
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
37 changes: 25 additions & 12 deletions utility/include/aw/utility/unicode/utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,6 @@ struct codec {
return output;
}

/*!
* Advance to the next sequence of code units
*/
template<typename Iterator>
static Iterator next(Iterator input, Iterator end)
{
while ((input < end) && !is_head(*input) && !is_ascii(*input))
++input;

return input;
}

/*!
* Decode UTF-8 sequence, if valid
*/
Expand Down Expand Up @@ -207,6 +195,31 @@ struct codec {

return input;
}

/*!
* Advance to the next sequence of code units
*/
template<typename Iterator>
static Iterator next(Iterator input, Iterator end)
{
while ((input < end) && !is_head(*input) && !is_ascii(*input))
++input;

return input;
}

/*!
* Skip n code points
*/
template<typename Iterator>
static Iterator skip(Iterator input, Iterator end, size_t n)
{
while (input < end && n > 0) {
input = next(std::next(input), end);
--n;
}
return input;
}
};
} // namespace aw::unicode::utf8
#endif//aw_utility_utf8_h
10 changes: 10 additions & 0 deletions utility/tests/utf8toutf16.c++
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
TestFile("utf8toutf16");

namespace aw {
Test(utf8skip) {
std::string s1 = "αβγ";
std::string s2 = "こんにちは";

auto it1 = aw::unicode::utf8::codec::skip(begin(s1), end(s1), 2);
auto it2 = aw::unicode::utf8::codec::skip(begin(s2), end(s2), 4);
TestEqual(std::string(it1, end(s1)), "γ");
TestEqual(std::string(it2, end(s2)), "");
}

Test(utf8toutf16)
{
const std::pair<std::u8string, std::u16string> utf_strings[] = {
Expand Down

0 comments on commit 170a143

Please sign in to comment.