std::isdigit(std::locale)(3)

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

πŸ’… std::isdigit(std::locale) πŸ’…

okay bestie, this is the C++ Standard Libary manual page std::isdigit(std::locale)(3), and it's a cute little template that asks a locale whether a character counts as a digit. no cap. πŸŽ€


✨ NAME ✨

std::isdigit(std::locale) - std::isdigit(std::locale)

Yes bestie, the NAME section is literally the function twice. iconic. πŸ’€

πŸ¦‹ Synopsis πŸ¦‹

Defined in header <locale>:

template< class CharT >
bool isdigit( CharT ch, const locale& loc );

It checks if the given character is classified as a digit by the given locale's std::ctype facet. that's the whole vibe. ✨

🌸 Parameters 🌸

  • ch β€” character. the character we're vibe-checking πŸ‘€
  • loc β€” locale. the locale doing the classifying 🫢

πŸ’– Return value πŸ’–

Returns true if the character is classified as a digit, false otherwise. periodt. βœ…

🧚 Possible implementation 🧚

          template<class CharT>
          bool isdigit(CharT ch, const std::locale& loc)
          {
              return
       std::use_facet<std::ctype<CharT>>(loc).is(std::ctype_base::digit, ch);
          }

πŸ”₯ Example πŸ”₯

okay here's the full code, bestie β€” run it and watch the locale do its thing:

       // Run this code

        #include <iostream>
        #include <locale>
        #include <string>
        #include <unordered_set>

        struct jdigit_ctype : std::ctype<wchar_t>
        {
            std::unordered_set<wchar_t> jdigits{
                L'δΈ€', L'二', L'δΈ‰', L'ε››', L'δΊ”', L'ε…­', L'δΈƒ', L'ε…«', L'九',
       L'十'
            };

            bool do_is(mask m, char_type c) const override
            {
                return (m & digit) && jdigits.contains(c)
                    ? true // Japanese digits will be classified as digits
                    : ctype::do_is(m, c); // leave the rest to the parent class
            }
        };

        int main()
        {
            std::wstring text = L"123δΈ€δΊŒδΈ‰οΌ‘οΌ’οΌ“";
            std::locale loc(std::locale(""), new jdigit_ctype);

            std::locale::global(std::locale("en_US.utf8"));
            std::wcout.imbue(std::locale());

            for (const wchar_t c : text)
                if (std::isdigit(c, loc))
                    std::wcout << c << " is a digit\n";
                else
                    std::wcout << c << " is NOT a digit\n";
        }

πŸ“ Possible output πŸ“

        1 is a digit
        2 is a digit
        3 is a digit
        δΈ€ is a digit
        二 is a digit
        δΈ‰ is a digit
        οΌ‘ is NOT a digit
        οΌ’ is NOT a digit
        οΌ“ is NOT a digit

πŸ‘‘ See also πŸ‘‘

  • isdigit β€” checks if a character is a digit (function)
  • iswdigit β€” checks if a wide character is a digit (function)

k bye bestie πŸ’– β€” source: http://cppreference.com, version/date: 2024.06.10, manual name: std::isdigit(std::locale)(3). slay and read the docs. ✨

πŸ“¦ where this page comes from