Skip to content

ui::CodePoints

Header: winui/core/Utf8.hpp.

The code points of a UTF-8 string, for a range-for: for (char32_t cp : code_points(text)).

A non-owning view: the string must outlive it. Malformed sequences yield U+FFFD, one per sequence. The iterator's offset() gives the byte position of each code point, which is what every text API in the framework takes.

Example:

const std::string text = "Hello, world";
std::size_t letters = 0;
for (char32_t cp : ui::code_points(text))
{
    letters += (cp >= U'a' && cp <= U'z') ? 1 : 0;
}

// offset() is the byte position the text APIs take: cut the string at the first comma.
std::string greeting = text;
const ui::CodePoints points = ui::code_points(text);
for (auto it = points.begin(); it != points.end(); ++it)
{
    if (*it == U',')
    {
        greeting = text.substr(0, it.offset()); // "Hello"
        break;
    }
}

CodePoints

CodePoints(std::string_view text)

A view over text, which must outlive it.

begin

iterator begin() const

The first code point.

end

iterator end() const

One past the last code point (byte offset text.size()).

class iterator

Forward iterator over the code points; compares by byte offset only.

value_type

using ui::CodePoints::iterator::value_type = char32_t

Dereferencing yields a decoded code point by value.

difference_type

using ui::CodePoints::iterator::difference_type = std::ptrdiff_t

Required by std::weakly_incrementable; distances are not meaningful here.

iterator

iterator() = default

A singular iterator; only assignable.

iterator

iterator(std::string_view text, std::size_t pos)

Positioned at byte pos of text, which must be a code-point boundary.

operator*

char32_t operator*() const

The code point at the current position (U+FFFD if malformed).

operator++

iterator& operator++()

Advances past the current code point.

operator++

iterator operator++(int)

Post-increment.

offset

std::size_t offset() const

The byte offset of the current code point.

operator==

bool operator==(const iterator& other) const

Same byte offset; only meaningful between iterators over the same text.