std::regex_traits::value(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 NAME 🌸

okay bestie, welcome to std::regex_traits::value(3) β€” the page from the C++ Standard Libary manual (yes, that's literally how it's spelled, iykyk 😭). In classic manpage fashion, the name is also std::regex_traits::value β€” std::regex_traits::value. It's giving "same name, main character energy" πŸ’…βœ¨

πŸ“œ Synopsis πŸ“œ

int value( CharT ch, int radix ) const;  (since C++11)

bestie, this one determines the value represented by the digit ch in the numeric base radix, given the currently imbued locale. No cap, this function is called by std::regex when processing Quantifiers such as {1} or {2,5}, Backreferences such as \1, and hexadecimal and Unicode character escapes. It's lowkey the digit detective of regex traits πŸ”₯

πŸ’– Parameters πŸ’–

  • ch β€” the character that may represent a digit
  • radix β€” either 8, 10, or 16

πŸ¦‹ Return value πŸ¦‹

The numeric value if ch indeed represents a digit in the currently imbued locale that is valid for the numeric base radix, or -1 on error. periodt. If it's not valid, it's a -1 situation and we move on 🫢

🧚 Example 🧚

okay so here's the whole code moment, bestie β€” run this code and watch the custom traits do their thing πŸ’œ

        // Run this code

        #include <iostream>
        #include <locale>
        #include <map>
        #include <regex>

        // This custom regex traits allows japanese numerals
        struct jnum_traits : std::regex_traits<wchar_t>
        {
            static std::map<wchar_t, int> data;
            int value(wchar_t ch, int radix) const
            {
                wchar_t up = std::toupper(ch, getloc());
                return data.count(up) ? data[up] : regex_traits::value(ch,
       radix);
            }
        };
        std::map<wchar_t, int> jnum_traits::data = {{L'〇',0}, {L'δΈ€',1},
       {L'二',2},
                                                    {L'δΈ‰',3}, {L'ε››',4},
       {L'δΊ”',5},
                                                    {L'ε…­',6}, {L'δΈƒ',7},
       {L'ε…«',8},
                                                    {L'九',9}, {L'1',10},
       {L'οΌ’',11},
                                                    {L'οΌ£',12}, {L'οΌ€',13},
       {L'οΌ₯',14},
                                                    {L'οΌ¦',15}};

        int main()
        {
            std::locale::global(std::locale("ja_JP.utf8"));
            std::wcout.sync_with_stdio(false);
            std::wcout.imbue(std::locale());

            std::wstring in = L"ι’¨";

            if (std::regex_match(in, std::wregex(L"\\u98a8")))
                std::wcout << "\\u98a8 matched " << in << '\n';

            if (std::regex_match(in, std::basic_regex<wchar_t,
       jnum_traits>(L"\\u九八a八")))
                std::wcout << L"\\u九八a八 with custom traits matched " << in
       << '\n';
        }

πŸ“ Output πŸ“

        \u98a8 matched ι’¨
        \u九八a八 with custom traits matched ι’¨

source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::regex_traits::value(3) β€” and that's the whole manual page, bestie, slay πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from