std::isdigit(std::locale)(3)
π 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms