275 lines
11 KiB
Markdown
275 lines
11 KiB
Markdown
[re.traits]
|
||
|
||
# 28 Text processing library [[text]](./#text)
|
||
|
||
## 28.6 Regular expressions library [[re]](re#traits)
|
||
|
||
### 28.6.6 Class template regex_traits [re.traits]
|
||
|
||
[ð](#lib:regex_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; };}
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10029)
|
||
|
||
The specializations regex_traits<char> andregex_traits<wchar_t> meet the
|
||
requirements for a regular expression traits class ([[re.req]](re.req "28.6.2 Requirements"))[.](#1.sentence-1)
|
||
|
||
[ð](#lib:regex_traits,char_class_type)
|
||
|
||
`using char_class_type = bitmask_type;
|
||
`
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10042)
|
||
|
||
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[.](#2.sentence-1)
|
||
|
||
[ð](#lib:length,regex_traits)
|
||
|
||
`static size_t length(const char_type* p);
|
||
`
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10054)
|
||
|
||
*Returns*: char_traits<charT>::length(p)[.](#3.sentence-1)
|
||
|
||
[ð](#lib:regex_traits,translate)
|
||
|
||
`charT translate(charT c) const;
|
||
`
|
||
|
||
[4](#4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10065)
|
||
|
||
*Returns*: c[.](#4.sentence-1)
|
||
|
||
[ð](#lib:regex_traits,translate_nocase)
|
||
|
||
`charT translate_nocase(charT c) const;
|
||
`
|
||
|
||
[5](#5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10076)
|
||
|
||
*Returns*: use_facet<ctype<charT>>(getloc()).tolower(c)[.](#5.sentence-1)
|
||
|
||
[ð](#lib:regex_traits,transform)
|
||
|
||
`template<class ForwardIterator>
|
||
string_type transform(ForwardIterator first, ForwardIterator last) const;
|
||
`
|
||
|
||
[6](#6)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10088)
|
||
|
||
*Effects*: As if by:string_type str(first, last);return use_facet<collate<charT>>( getloc()).transform(str.data(), str.data() + str.length());
|
||
|
||
[ð](#lib:regex_traits,transform_primary)
|
||
|
||
`template<class ForwardIterator>
|
||
string_type transform_primary(ForwardIterator first, ForwardIterator last) const;
|
||
`
|
||
|
||
[7](#7)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10105)
|
||
|
||
*Effects*: Iftypeid(use_facet<collate<charT>>(getloc())) == 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[.](#7.sentence-1)
|
||
|
||
[ð](#lib:regex_traits,lookup_collatename)
|
||
|
||
`template<class ForwardIterator>
|
||
string_type lookup_collatename(ForwardIterator first, ForwardIterator last) const;
|
||
`
|
||
|
||
[8](#8)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10124)
|
||
|
||
*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)[.](#8.sentence-1)
|
||
|
||
Returns an empty string if the character sequence is not a
|
||
valid collating element[.](#8.sentence-2)
|
||
|
||
[ð](#lib:regex_traits,lookup_classname)
|
||
|
||
`template<class ForwardIterator>
|
||
char_class_type lookup_classname(
|
||
ForwardIterator first, ForwardIterator last, bool icase = false) const;
|
||
`
|
||
|
||
[9](#9)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10141)
|
||
|
||
*Returns*: An unspecified value that represents
|
||
the character classification named by the character sequence
|
||
designated by the iterator range [first, last)[.](#9.sentence-1)
|
||
|
||
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[.](#9.sentence-2)[238](#footnote-238 "For example, if the parameter icase is true then [[:lower:]] is the same as [[:alpha:]].")
|
||
|
||
The value
|
||
returned shall be independent of the case of the characters in
|
||
the character sequence[.](#9.sentence-3)
|
||
|
||
If the name
|
||
is not recognized then returns char_class_type()[.](#9.sentence-4)
|
||
|
||
[10](#10)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10159)
|
||
|
||
*Remarks*: For regex_traits<char>, at least the narrow character names
|
||
in Table [121](#tab:re.traits.classnames "Table 121: Character class names and corresponding ctype masks") shall be recognized[.](#10.sentence-1)
|
||
|
||
For regex_traits<wchar_t>, at least the wide character names
|
||
in Table [121](#tab:re.traits.classnames "Table 121: Character class names and corresponding ctype masks") shall be recognized[.](#10.sentence-2)
|
||
|
||
[ð](#lib:regex_traits,isctype)
|
||
|
||
`bool isctype(charT c, char_class_type f) const;
|
||
`
|
||
|
||
[11](#11)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10173)
|
||
|
||
*Effects*: Determines if the character c is a member of the character
|
||
classification represented by f[.](#11.sentence-1)
|
||
|
||
[12](#12)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10178)
|
||
|
||
*Returns*: Given the following function declaration:// for exposition onlytemplate<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 [121](#tab:re.traits.classnames "Table 121: Character class names and corresponding ctype masks") 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 [1](#example-1)*: 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 [2](#example-2)*: 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*]
|
||
|
||
[ð](#lib:value,regex_traits)
|
||
|
||
`int value(charT ch, int radix) const;
|
||
`
|
||
|
||
[13](#13)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10232)
|
||
|
||
*Preconditions*: The value of radix is 8, 10, or 16[.](#13.sentence-1)
|
||
|
||
[14](#14)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10236)
|
||
|
||
*Returns*: The value represented by the digit ch in baseradix if the character ch is a valid digit in baseradix; otherwise returns -1[.](#14.sentence-1)
|
||
|
||
[ð](#lib:locale)
|
||
|
||
`locale_type imbue(locale_type loc);
|
||
`
|
||
|
||
[15](#15)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10250)
|
||
|
||
*Effects*: Imbues *this with a copy of the
|
||
locale loc[.](#15.sentence-1)
|
||
|
||
[*Note [1](#note-1)*:
|
||
|
||
Calling imbue with a
|
||
different locale than the one currently in use invalidates all cached
|
||
data held by *this[.](#15.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
[16](#16)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10260)
|
||
|
||
*Postconditions*: getloc() == loc[.](#16.sentence-1)
|
||
|
||
[17](#17)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10264)
|
||
|
||
*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[.](#17.sentence-1)
|
||
|
||
[ð](#lib:locale_)
|
||
|
||
`locale_type getloc() const;
|
||
`
|
||
|
||
[18](#18)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L10278)
|
||
|
||
*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[.](#18.sentence-1)
|
||
|
||
Table [121](#tab:re.traits.classnames) — Character class names and corresponding ctype masks [[tab:re.traits.classnames]](./tab:re.traits.classnames)
|
||
|
||
| [ð](#tab:re.traits.classnames-row-1)<br>**Narrow character name** | **Wide character name** | **Corresponding ctype_base::mask value** |
|
||
| --- | --- | --- |
|
||
| [ð](#tab:re.traits.classnames-row-2)<br>"alnum" | L"alnum" | ctype_base::alnum |
|
||
| [ð](#tab:re.traits.classnames-row-3)<br>"alpha" | L"alpha" | ctype_base::alpha |
|
||
| [ð](#tab:re.traits.classnames-row-4)<br>"blank" | L"blank" | ctype_base::blank |
|
||
| [ð](#tab:re.traits.classnames-row-5)<br>"cntrl" | L"cntrl" | ctype_base::cntrl |
|
||
| [ð](#tab:re.traits.classnames-row-6)<br>"digit" | L"digit" | ctype_base::digit |
|
||
| [ð](#tab:re.traits.classnames-row-7)<br>"d" | L"d" | ctype_base::digit |
|
||
| [ð](#tab:re.traits.classnames-row-8)<br>"graph" | L"graph" | ctype_base::graph |
|
||
| [ð](#tab:re.traits.classnames-row-9)<br>"lower" | L"lower" | ctype_base::lower |
|
||
| [ð](#tab:re.traits.classnames-row-10)<br>"print" | L"print" | ctype_base::print |
|
||
| [ð](#tab:re.traits.classnames-row-11)<br>"punct" | L"punct" | ctype_base::punct |
|
||
| [ð](#tab:re.traits.classnames-row-12)<br>"space" | L"space" | ctype_base::space |
|
||
| [ð](#tab:re.traits.classnames-row-13)<br>"s" | L"s" | ctype_base::space |
|
||
| [ð](#tab:re.traits.classnames-row-14)<br>"upper" | L"upper" | ctype_base::upper |
|
||
| [ð](#tab:re.traits.classnames-row-15)<br>"w" | L"w" | ctype_base::alnum |
|
||
| [ð](#tab:re.traits.classnames-row-16)<br>"xdigit" | L"xdigit" | ctype_base::xdigit |
|
||
|
||
[238)](#footnote-238)[238)](#footnoteref-238)
|
||
|
||
For example, if the parameter icase is true then[[:lower:]] is the same as [[:alpha:]][.](#footnote-238.sentence-1)
|