Namespaces
Variants

std::regex_traits<CharT>:: isctype

From cppreference.net
Regular expressions library
Classes
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
bool isctype ( CharT c, char_class_type f ) const ;

文字 c が、 f によって識別される文字クラスに属するかどうかを判定します。ここで f は、 lookup_classname() によって返される値、またはそのような値のビット単位の論理和です。

標準ライブラリの std::regex_traits 特殊化で提供されるこの関数のバージョンは、以下の処理を実行します:

1) まず f を型 std::ctype_base::mask の値 m に変換する。
std::ctype カテゴリについて、 lookup_classname() ページの表に列挙されているものに対応するビットが f で設定されている場合、 m の対応するビットも設定される。
2) 次に、imbueされたロケール内の文字を分類するため、 std:: use_facet < std:: ctype < CharT >> ( getloc ( ) ) . is ( m, c ) を呼び出します。
  • これが true を返す場合、 isctype() true を返します。
  • それ以外の場合、 c '_' に等しく、かつ f が文字クラス [:w:] に対する lookup_classname() の呼び出し結果を含む場合、 true が返され、それ以外の場合は false が返されます。

目次

パラメータ

c - 分類対象の文字
f - lookup_classname() の1回または複数回の呼び出しから取得したビットマスク

戻り値

true c f によって分類される場合、 false がそれ以外の場合。

#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';
}

出力:

true true
true false
false false

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 "ナウシカ"
}

出力:

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

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 2018 C++11 m の値は未規定であった lookup_classname() の最小サポートに一致する

関連項目

名前による文字クラスの取得
(公開メンバ関数)
[virtual]
文字または文字シーケンスの分類
( std::ctype<CharT> の仮想保護メンバ関数)
指定された LC_CTYPE カテゴリに従ってワイド文字を分類
(関数)