r/cpp • ++ • Apr 01 '26

Personal project: bringing Rust-style unicode invariants to C++

For those who dabbled in Rust, you might know that string types in Rust, be it literals, slices or owned are by default UTF8 encoded and all methods enforce this invariant. Funny thing is that this design also works well in C++, allowing us to validate strings once, keep their unicode validity enforced through dedicated types and work on these unicode strings without worries of runtime errors.

This is what I've been working on for the past few weeks and I wanted to share it with you, prior to making the full official release.

Hopefully the few examples of the features this library has that I've added here are self explanatory, and I'm also adding a link to the project: https://github.com/cristi1990an/unicode_ranges

#include "unicode_ranges.hpp"
#include <print>
#include <array>
#include <ranges>

using namespace unicode_ranges;
using namespace unicode_ranges::literals;

int main()
{
  // Compile time string validation
  static constexpr utf8_string_view greetings = "SalutΔƒri din RomΓ’nia πŸ‡·πŸ‡΄ πŸ‘‹"_utf8_sv;

  // Rust style unicode character/grapheme lazy views over the string
  std::println("{}: {}",
    greetings.char_count(),                          // 25
    greetings.chars() | std::views::drop(21));       // [πŸ‡·, πŸ‡΄,  , πŸ‘‹]

  std::println("{}: {::s}",
    greetings.grapheme_count(),                      // 24
    greetings.graphemes() | std::views::drop(21));   // [πŸ‡·πŸ‡΄,  , πŸ‘‹]

  // owned utf8 string
  utf8_string owned_string = greetings;

  // find and replace grapheme cluster with utf8 string slice
  owned_string = owned_string.replace_all("πŸ‡·πŸ‡΄"_grapheme_utf8, "[ro flag]"_utf8_sv);

  // replace any of matching characters with replacement (rvalue overload might not reallocate)
  owned_string = std::move(owned_string).replace_all(std::array{ "Δƒ"_u8c, "Γ’"_u8c }, "a"_u8c);

  // STD style index based modifying methods also available
  owned_string.erase(owned_string.find("πŸ‘‹"_u8c), "πŸ‘‹"_u8c.code_unit_count());

  std::println("{}, {}, {}",
    owned_string,                                  // Salutari din Romania [ro flag]
    owned_string.starts_with("Salutari"_utf8_sv),  // true
    owned_string.is_ascii());                      // true

  // pipe the character view into your favorite view adaptors, don't worry about overlaps
  owned_string = owned_string.chars()
    | std::views::filter(
      [](utf8_char ch)
      {
        return ch.is_ascii_lowercase();
      })
    | std::ranges::to<utf8_string>();
  std::println("{}", owned_string);                // alutaridinomaniaroflag
}
29 Upvotes

6 comments sorted by

View all comments

Show parent comment

9

u/cristi1990an ++ Apr 01 '26

No, they're referring explicitly to grapheme clusters and they're right. Supporting them does require shipping (relatively large) unicode tables alongside the library which do bloat the binaries and do need to be kept up to date. And this is indeed what I'm doing here, maybe I'll have them optional in the future...

4

u/bouncebackabilify Apr 01 '26

Cool, thanks for the explanation :)

3

u/schombert Apr 01 '26

In theory, these tables are something that the system can (and probably should) provide, rather than forcing each application to include them. Windows does this (by providing the icu4c library as a system dll, plus the older uniscribe support) and I imagine that Linux does too.