C++ Logo

sg16

Advanced search

utfN_view

From: Zach Laine <whatwasthataddress_at_[hidden]>
Date: Sun, 16 Apr 2023 13:54:42 -0500
We again talked about utfN_view at the last meeting. I was trying to
justify their existence, and again I could not remember the salient
point during the discussion. Now I have. Here is one of them:

template<utf8_iter I, sentinel_for<I> S = I>
  struct utf8_view : view_interface<utf8_view<I, S>> {
    using iterator = I;
    using sentinel = S;

    constexpr utf8_view() {}
    constexpr utf8_view(iterator first, sentinel last);

    constexpr iterator begin() const;
    constexpr sentinel end() const;

    friend constexpr bool operator==(utf8_view lhs, utf8_view rhs)
      { return lhs.begin() == rhs.begin() && lhs.end() == rhs.end(); }

    template<class CharT, class Traits>
      friend basic_ostream<CharT, Traits>&
        operator<<(basic_ostream<CharT, Traits>& os, utf8_view v);

  private:
    using iterator_t = unspecified; // exposition only
    using sentinel_t = unspecified; // exposition only

    iterator_t first_; // exposition only
    [[no_unique_address]] sentinel_t last_; // exposition only
  };

Note the operator<<. I don't know how to provide a general-purpose
way to stream out a subrange<I, S>, when we know that it happens to
contain UTF-8, so I created utf8_view, and added an operator<<. I
have a similar concern about adding support for
std::format-/std::print-ing ranges of UTF.

Streaming or printing a utfN_view "just works", and this convenience
is used throughout Boost.Text and the examples in the papers I'm
proposing. I think the value of this convenience is evident in the
examples. If someone has a reasonable alternative, I'm happy to
replace utfN_view with something that works more like a typical
std::ranges view. Without such an alternative, I want to keep the
current design.

Zach

Received on 2023-04-16 18:54:55