30 Regular expressions library [re]

30.7 Class template regex_­traits [re.traits]

namespace std {
  template<class charT>
    struct regex_traits {
      using char_type       = charT;
      using string_type     = basic_string<char_type>;
      using locale_type     = locale;
      using char_class_type = bitmask_type;

      regex_traits();
      static size_t length(const char_type* p);
      charT translate(charT c) const;
      charT translate_nocase(charT c) const;
      template<class ForwardIterator>
        string_type transform(ForwardIterator first, ForwardIterator last) const;
      template<class ForwardIterator>
        string_type transform_primary(
          ForwardIterator first, ForwardIterator last) const;
      template<class ForwardIterator>
        string_type lookup_collatename(
          ForwardIterator first, ForwardIterator last) const;
      template<class ForwardIterator>
        char_class_type lookup_classname(
          ForwardIterator first, ForwardIterator last, bool icase = false) const;
      bool isctype(charT c, char_class_type f) const;
      int value(charT ch, int radix) const;
      locale_type imbue(locale_type l);
      locale_type getloc() const;
    };
}
The specializations regex_­traits<char> and regex_­traits<wchar_­t> meet the requirements for a regular expression traits class ([re.req]).
using char_class_type = bitmask_type;
The type char_­class_­type is used to represent a character classification and is capable of holding an implementation specific set returned by lookup_­classname.
static size_t length(const char_type* p);
Returns: char_­traits<charT>​::​length(p).
charT translate(charT c) const;
Returns: c.
charT translate_nocase(charT c) const;
Returns: use_­facet<ctype<charT>>(getloc()).tolower(c).
template<class ForwardIterator> string_type transform(ForwardIterator first, ForwardIterator last) const;
Effects: As if by:
string_type str(first, last);
return use_facet<collate<charT>>(
  getloc()).transform(str.data(), str.data() + str.length());
template<class ForwardIterator> string_type transform_primary(ForwardIterator first, ForwardIterator last) const;
Effects: If
typeid(use_facet<collate<charT>>) == typeid(collate_byname<charT>)
and the form of the sort key returned by collate_­byname<charT>​::​transform(first, last) is known and can be converted into a primary sort key then returns that key, otherwise returns an empty string.
template<class ForwardIterator> string_type lookup_collatename(ForwardIterator first, ForwardIterator last) const;
Returns: A sequence of one or more characters that represents the collating element consisting of the character sequence designated by the iterator range [first, last).
Returns an empty string if the character sequence is not a valid collating element.
template<class ForwardIterator> char_class_type lookup_classname( ForwardIterator first, ForwardIterator last, bool icase = false) const;
Returns: An unspecified value that represents the character classification named by the character sequence designated by the iterator range [first, last).
If the parameter icase is true then the returned mask identifies the character classification without regard to the case of the characters being matched, otherwise it does honor the case of the characters being matched.325
The value returned shall be independent of the case of the characters in the character sequence.
If the name is not recognized then returns char_­class_­type().
Remarks: For regex_­traits<char>, at least the narrow character names in Table 139 shall be recognized.
For regex_­traits<wchar_­t>, at least the wide character names in Table 139 shall be recognized.
bool isctype(charT c, char_class_type f) const;
Effects: Determines if the character c is a member of the character classification represented by f.
Returns: Given the following function declaration:
// for exposition only
template<class C>
  ctype_base::mask convert(typename regex_traits<C>::char_class_type f);
that returns a value in which each ctype_­base​::​mask value corresponding to a value in f named in Table 139 is set, then the result is determined as if by:
ctype_base::mask m = convert<charT>(f);
const ctype<charT>& ct = use_facet<ctype<charT>>(getloc());
if (ct.is(m, c)) {
  return true;
} else if (c == ct.widen('_')) {
  charT w[1] = { ct.widen('w') };
  char_class_type x = lookup_classname(w, w+1);
  return (f&x) == x;
} else {
  return false;
}
[Example
:
regex_traits<char> t;
string d("d");
string u("upper");
regex_traits<char>::char_class_type f;
f = t.lookup_classname(d.begin(), d.end());
f |= t.lookup_classname(u.begin(), u.end());
ctype_base::mask m = convert<char>(f);  // m == ctype_­base​::​digit|ctype_­base​::​upper
— end example
]
[Example
:
regex_traits<char> t;
string w("w");
regex_traits<char>::char_class_type f;
f = t.lookup_classname(w.begin(), w.end());
t.isctype('A', f);  // returns true
t.isctype('_', f);  // returns true
t.isctype(' ', f);  // returns false
— end example
]
int value(charT ch, int radix) const;
Preconditions: The value of radix is 8, 10, or 16.
Returns: The value represented by the digit ch in base radix if the character ch is a valid digit in base radix; otherwise returns -1.
locale_type imbue(locale_type loc);
Effects: Imbues this with a copy of the locale loc.
[Note
:
Calling imbue with a different locale than the one currently in use invalidates all cached data held by *this.
— end note
]
Returns: If no locale has been previously imbued then a copy of the global locale in effect at the time of construction of *this, otherwise a copy of the last argument passed to imbue.
Postconditions: getloc() == loc.
locale_type getloc() const;
Returns: If no locale has been imbued then a copy of the global locale in effect at the time of construction of *this, otherwise a copy of the last argument passed to imbue.
Table 139: Character class names and corresponding ctype masks   [tab:re.traits.classnames]
Narrow character name
Wide character name
Corresponding ctype_­base​::​mask value
"alnum"
L"alnum"
ctype_­base​::​alnum
"alpha"
L"alpha"
ctype_­base​::​alpha
"blank"
L"blank"
ctype_­base​::​blank
"cntrl"
L"cntrl"
ctype_­base​::​cntrl
"digit"
L"digit"
ctype_­base​::​digit
"d"
L"d"
ctype_­base​::​digit
"graph"
L"graph"
ctype_­base​::​graph
"lower"
L"lower"
ctype_­base​::​lower
"print"
L"print"
ctype_­base​::​print
"punct"
L"punct"
ctype_­base​::​punct
"space"
L"space"
ctype_­base​::​space
"s"
L"s"
ctype_­base​::​space
"upper"
L"upper"
ctype_­base​::​upper
"w"
L"w"
ctype_­base​::​alnum
"xdigit"
L"xdigit"
ctype_­base​::​xdigit
For example, if the parameter icase is true then [[:lower:]] is the same as [[:alpha:]].
⮥