名前空間
変種
操作

std::regex_traits<CharT>::isctype

From cppreference.com
< cpp‎ | regex‎ | regex traits
 
 
 
正規表現ライブラリ
クラス
(C++11)
アルゴリズム
イテレータ
例外
Traits
定数
(C++11)
正規表現文法
 
 
bool isctype( CharT c, char_class_type f ) const;

文字 c が、文字クラス f によって識別される文字クラスに属するかどうかを決定します。f は、lookup_classname() の戻り値、またはそれらの値のビットごとのORです。

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

1) まず、fstd::ctype_base::mask 型の値 m に変換します。
lookup_classname() のページにある表でリストされている各 std::ctype カテゴリについて、カテゴリに対応する f のビットが設定されている場合、m の対応するビットも設定されます。
2) 次に、埋め込まれたロケールで文字を分類しようとします。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: cf によって分類される場合。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() の最小サポートに一致します。

[編集] 関連項目

名前で文字クラスを取得します。
(public member function) [編集]
[virtual]
文字または文字シーケンスを分類します
(std::ctype<CharT> の仮想 protected メンバ関数) [edit]
指定されたLC_CTYPEカテゴリに従ってワイド文字を分類する
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)