Namespaces
Variants
Views
Actions

std::ctype::is, std::ctype::do_is

From cppreference.com
< cpp‎ | locale‎ | ctype
Revision as of 06:43, 2 November 2012 by Cubbi (Talk | contribs)

 
 
 
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <locale>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
public:
bool is(mask m, CharT c) const;
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
public:
const CharT* is(const CharT* low, const CharT* high, mask* vec) const;
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
protected:
virtual bool do_is(mask m, CharT c) const;
</td>

<td > (3) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
protected:
virtual const CharT* do_is(const CharT* low, const CharT* high, mask* vec) const;
</td>

<td > (4) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

1,2) public member function, calls the protected virtual member function do_is of the most derived class.
3) Checks if the character c is classified by the mask m
4) For every character in the character array [low, high), identifies the complete classification mask (e.g. digit|xdigit|alnum|print|graph for the digit '0' in the default locale), and stores the masks in the corresponding elements of the array pointed to by vec

Contents

Parameters

c - character to classify
m - mask to use for classifying a single character
low - pointer to the first character in an array of characters to classify
high - one past the end pointer for the array of characters to classify
vec - pointer to the first element of the array of masks to fill

Return value

1,3) true if c is classified by m
2,4) high

Example

#include <locale>
#include <utility>
#include <vector>
#include <iostream>
 
// utility wrapper to make locale-bound facets destructible
template<class Facet>
struct deletable_facet : Facet
{
    template<class ...Args>
    deletable_facet(Args&& ...args) : Facet(std::forward<Args>(args)...) {}
    ~deletable_facet() {}
};
 
int main()
{
    // classify a single character using the default locale
    auto& f = std::use_facet<std::ctype<char>>(std::locale());
    char c = '0';
    if(f.is(std::ctype_base::digit, c)) // or isdigit(c, locale());
    {
        std::cout << "'" << c << "' is a digit\n";
    }
 
    // classify every character in a string using a named locale
    deletable_facet<std::ctype_byname<wchar_t>>f2("en_US.utf8");
    std::wstring str = L"z\u00df\u6c34\U0001d10b";
    std::vector<std::ctype_base::mask> vec(str.size());
    f2.is(&str[0], &str[0] + str.size(), &vec[0]);
 
    for(size_t n = 0; n < str.size(); ++n) {
       std::cout << std::hex << "U+" << str[n] << " is: ";
       if(vec[n] & std::ctype_base::alnum) 
          std::cout << " alnum ";
       if(vec[n] & std::ctype_base::punct) 
          std::cout << " punct ";
       std::cout << '\n';
    }
}

Output:

'0' is a digit
U+7a is:  alnum 
U+df is:  alnum 
U+6c34 is:  alnum 
U+1d10b is:  punct

See also

Template:cpp/string/wide/dcl list iswctype