std::regex_traits::isctype - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::regex_traits::isctype

From cppreference.com
< cpp‎ | regex‎ | regex traits

bool isctype( CharT c, char_class_type f ) const; |  |

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.

The version of this function provided in the standard library specializations of std::regex_traits does the following:

1) First converts `f` to some temporary value `m` of type std::ctype_base::mask in implementation-defined manner
2) Then attempts to classify the character in the imbued locale by calling std::use_facet<std::ctype<CharT>>(getloc()).is(m, c). If that returned true, true is returned by `isctype()`.
3) Otherwise, checks whether `c` equals `'_'` and the bitmask `f` includes the result of calling lookup_classname() for the character class `[:w:]`, in which case true is returned.
4) Otherwise, `false` is returned.

### [edit] Parameters

c | - | the character to classify
f | - | the bitmask obtained from one or several calls to lookup_classname()

### [edit] Return value

true if `c` is classified by `f`, false otherwise.

### [edit] Notes

The description above summarizes C++14; the C++11 phrasing required this function to return true for `'_'` in all cases (LWG issue 2018)

### [edit] Example

Run this code

    #include <iostream>
    #include <string>
    #include <regex>

    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

demonstraits a custom regex_traits implementation of lookup_classname/isctype

Run this code

    #include <iostream>
    #include <locale>
    #include <regex>
    #include <cwctype>

    // 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 kanji
        std::regex_search(in, m,
                          std::basic_regex<wchar_t, wctype_traits>(L"([[:jkata:]]+)"));
        std::wcout << "katakana: " << m[1] << '\n'; // prints "ナウシカ"
    }

Output:

    alnums: 風の谷のナウシカ
    katakana: ナウシカ

### [edit] See also

lookup_classname | gets a character class by name (public member function) [edit]
do_is [virtual] | classifies a character or a character sequence (virtual protected member function of `std::ctype`) [edit]
iswctype | classifies a wide character according to the specified LC_CTYPE category (function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/regex/regex_traits/isctype&oldid=67008"