Skip to main content
Use the locale-aware character-type overloads; thanks @G.Sliepen
Source Link
Toby Speight
  • 81.9k
  • 14
  • 101
  • 309

Since is_palindrome() accepts ranges of any equality-comparable type, palindrome_view doesn't need to be a range of char, and there's need to cast within the transform():

auto constexpr palindrome_view =
    std::views::filter([](unsigned char c){ return std::isalnum(c); }) |
    std::views::transform([](unsigned char c){ return std::toupper(c); });

In case it's not clear, thisThis view iswasn't part of the testsmain code because it's not generic. It would But it could be easymade to create a wide-char version, using std::iswalnum() andwork for other character types by including std::towupper()<locale> frominstead of <cwctype><cctype>, but a fully generic version isn't possible using only the standard library.:

auto palindrome_locale = std::locale::classic();
auto constexpr palindrome_view =
    std::views::filter([](auto c){ return std::isalnum(c, palindrome_locale); }) |
    std::views::transform([](auto c){ return std::toupper(c, palindrome_locale); });

Since is_palindrome() accepts ranges of any equality-comparable type, palindrome_view doesn't need to be a range of char, and there's need to cast within the transform():

auto constexpr palindrome_view =
    std::views::filter([](unsigned char c){ return std::isalnum(c); }) |
    std::views::transform([](unsigned char c){ return std::toupper(c); });

In case it's not clear, this view is part of the tests because it's not generic. It would be easy to create a wide-char version, using std::iswalnum() and std::towupper() from <cwctype>, but a fully generic version isn't possible using only the standard library.

Since is_palindrome() accepts ranges of any equality-comparable type, palindrome_view doesn't need to be a range of char, and there's need to cast within the transform():

auto constexpr palindrome_view =
    std::views::filter([](unsigned char c){ return std::isalnum(c); }) |
    std::views::transform([](unsigned char c){ return std::toupper(c); });

This view wasn't part of the main code because it's not generic. But it could be made to work for other character types by including <locale> instead of <cctype>:

auto palindrome_locale = std::locale::classic();
auto constexpr palindrome_view =
    std::views::filter([](auto c){ return std::isalnum(c, palindrome_locale); }) |
    std::views::transform([](auto c){ return std::toupper(c, palindrome_locale); });
Source Link
Toby Speight
  • 81.9k
  • 14
  • 101
  • 309

Since is_palindrome() accepts ranges of any equality-comparable type, palindrome_view doesn't need to be a range of char, and there's need to cast within the transform():

auto constexpr palindrome_view =
    std::views::filter([](unsigned char c){ return std::isalnum(c); }) |
    std::views::transform([](unsigned char c){ return std::toupper(c); });

In case it's not clear, this view is part of the tests because it's not generic. It would be easy to create a wide-char version, using std::iswalnum() and std::towupper() from <cwctype>, but a fully generic version isn't possible using only the standard library.