名前空間
変種
操作

std::regex_traits<CharT>::isctype

提供: cppreference.com
< cpp‎ | regex‎ | regex traits
bool isctype( CharT c, char_class_type f ) const;

文字 cf の表す文字クラスに属するかどうか調べます。 文字クラスは、 lookup_classname() によって返された値か、それらの値のビット単位の論理和です。

std::regex_traits の標準ライブラリの特殊化によって提供されるこの関数のバージョンは以下を行います。

1) まず fstd::ctype_base::mask 型の何らかの一時的な値 m に処理系定義の方法で変換します。
2) それから std::use_facet<std::ctype<CharT>>(getloc()).is(m, c) を呼ぶことによって、設定されているロケールにおける文字の分類を試みます。 これが true を返した場合、 isctype()true を返します。
3) そうでなく、 c'_' と等しければ、ビットマスク f が文字クラス [:w:] に対して lookup_classname() を呼んだ結果を含むかどうか調べ、その場合 true を返します。
4) そうでなければ、 false が返されます。

目次

[編集] 引数

c - 分類する文字
f - lookup_classname() の1回以上の呼び出しで取得したビットマスク

[編集] 戻り値

cf に分類される場合は true、そうでなければ false

[編集] ノート

上記の説明は C++14 を要約しています。 C++11 の記述ではこの関数はいかなる場合でも '_' に対して true を返すことが要求されます (LWG issue 2018)。

[編集]

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

出力:

true true
true false
false false

lookup_classname/isctype のカスタム regex_traits 実装をデモンストレーションします

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

出力:

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


[編集] 関連項目

名前から文字クラスを取得します
(パブリックメンバ関数) [edit]
[仮想]
文字を分類します
(std::ctype<CharT>の仮想プロテクテッドメンバ関数) [edit]
指定された LC_CTYPE カテゴリに従ってワイド文字を分類します
(関数) [edit]