std::regex_traits::isctype(3)
๐ธ NAME ๐ธ
okay so this is the manual page for std::regex_traits::isctype โ literally the std::regex_traits::isctype member, from the C++ Standard Libary manual (yes, that's the header's spelling, not me ๐ญ), section 3.
๐ Synopsis ๐
bool isctype( CharT c, char_class_type f ) const;
So isctype determines whether the character c belongs to the character class identified by f, which, in turn, is a value returned by lookup_classname() or a bitwise OR of several such values. periodt.
The version of this function provided in the standard library specializations of std::regex_traits does the following, bestie:
- First converts f to a value m of type
std::ctype_base::mask. For eachstd::ctypecategory listed in the table in the pagelookup_classname(), if the bits in f corresponding to the category are set, the corresponding bits in m will also be set. - Then attempts to classify the character in the imbued locale by calling
std::use_facet<std::ctype<CharT>>(getloc()).is(m, c).- If that returns
true,isctype()will also returntrue. - Otherwise, if c equals
'_', and f includes the result of callinglookup_classname()for the character class[:w:],trueis returned, otherwisefalseis returned.
- If that returns
๐ซถ Parameters ๐ซถ
- c โ the character to classify
- f โ the bitmask obtained from one or several calls to
lookup_classname()
โจ Return value โจ
true if c is classified by f, false otherwise.
๐ Example ๐
okay, run this code:
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::regex_traits<char> t;
std::string str_alnum = "alnum";
auto a = t.lookup_classname(str_alnum.begin(), str_alnum.end());
std::string str_w = "w"; // [:w:] is [:alnum:] plus '_'
auto w = t.lookup_classname(str_w.begin(), str_w.end());
std::cout << std::boolalpha
<< t.isctype('A', w) << ' ' << t.isctype('A', a) << '\n'
<< t.isctype('_', w) << ' ' << t.isctype('_', a) << '\n'
<< t.isctype(' ', w) << ' ' << t.isctype(' ', a) << '\n';
}
Output:
true true
true false
false false
this next one demonstrates a custom regex traits implementation of lookup_classname() / isctype():
#include <cwctype>
#include <iostream>
#include <locale>
#include <regex>
// This custom regex traits uses wctype/iswctype to implement lookup_classname/isctype.
struct wctype_traits : std::regex_traits<wchar_t>
{
using char_class_type = std::wctype_t;
template<class It>
char_class_type lookup_classname(It first, It last, bool = false) const
{
return std::wctype(std::string(first, last).c_str());
}
bool isctype(wchar_t c, char_class_type f) const
{
return std::iswctype(c, f);
}
};
int main()
{
std::locale::global(std::locale("ja_JP.utf8"));
std::wcout.sync_with_stdio(false);
std::wcout.imbue(std::locale());
std::wsmatch m;
std::wstring in = L"้ขจใฎ่ฐทใฎใใฆใทใซ";
// matches all characters (they are classified as alnum)
std::regex_search(in, m, std::wregex(L"([[:alnum:]]+)"));
std::wcout << "alnums: " << m[1] << '\n'; // prints "้ขจใฎ่ฐทใฎใใฆใทใซ"
// matches only the katakana
std::regex_search(in, m,
std::basic_regex<wchar_t,
wctype_traits>(L"([[:jkata:]]+)"));
std::wcout << "katakana: " << m[1] << '\n'; // prints "ใใฆใทใซ"
}
Output:
alnums: ้ขจใฎ่ฐทใฎใใฆใทใซ
katakana: ใใฆใทใซ
๐ฆ Defect reports ๐ฆ
so these behavior-changing defect reports got applied retroactively to previously published C++ standards โ not new behavior, retroactive tea โ:
- DR
LWG 2018, applied to C++11 โ behavior as published: the value of m was unspecified; correct behavior: matcheslookup_classname()'s minimal support.
๐ See also ๐
lookup_classnameโ gets a character class by name (public member function)do_isโ classifies a character or a character sequence [virtual] (virtual protected member function ofstd::ctype<CharT>)iswctypeโ classifies a wide character according to the specifiedLC_CTYPEcategory (function)
mwah ๐ โ source: http://cppreference.com, version/date: 2024.06.10. bestie, you ate this page fr fr ๐ซถโจ
๐ฆ 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