From Jason Turner

[conversions.string]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpqjbi_tck/{from.md → to.md} +0 -196
tmp/tmpqjbi_tck/{from.md → to.md} RENAMED
@@ -1,196 +0,0 @@
1
- ##### `string` conversions <a id="conversions.string">[[conversions.string]]</a>
2
-
3
- Class template `wstring_convert` performs conversions between a wide
4
- string and a byte string. It lets you specify a code conversion facet
5
- (like class template `codecvt`) to perform the conversions, without
6
- affecting any streams or locales. If you want to use the code conversion
7
- facet `codecvt_utf8` to output to `cout` a UTF-8 multibyte sequence
8
- corresponding to a wide string, but you don’t want to alter the locale
9
- for `cout`, you can write something like:
10
-
11
- ``` cpp
12
- wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
13
- std::string mbstring = myconv.to_bytes(L"Hello\n");
14
- std::cout << mbstring;
15
- ```
16
-
17
- ``` cpp
18
- namespace std {
19
- template<class Codecvt, class Elem = wchar_t,
20
- class Wide_alloc = std::allocator<Elem>,
21
- class Byte_alloc = std::allocator<char> > class wstring_convert {
22
- public:
23
- typedef std::basic_string<char, char_traits<char>, Byte_alloc> byte_string;
24
- typedef std::basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
25
- typedef typename Codecvt::state_type state_type;
26
- typedef typename wide_string::traits_type::int_type int_type;
27
-
28
- explicit wstring_convert(Codecvt* pcvt = new Codecvt);
29
- wstring_convert(Codecvt* pcvt, state_type state);
30
- explicit wstring_convert(const byte_string& byte_err,
31
- const wide_string& wide_err = wide_string());
32
- ~wstring_convert();
33
-
34
- wstring_convert(const wstring_convert&) = delete;
35
- wstring_convert& operator=(const wstring_convert&) = delete;
36
-
37
- wide_string from_bytes(char byte);
38
- wide_string from_bytes(const char* ptr);
39
- wide_string from_bytes(const byte_string& str);
40
- wide_string from_bytes(const char* first, const char* last);
41
-
42
- byte_string to_bytes(Elem wchar);
43
- byte_string to_bytes(const Elem* wptr);
44
- byte_string to_bytes(const wide_string& wstr);
45
- byte_string to_bytes(const Elem* first, const Elem* last);
46
-
47
- size_t converted() const noexcept;
48
- state_type state() const;
49
- private:
50
- byte_string byte_err_string; // exposition only
51
- wide_string wide_err_string; // exposition only
52
- Codecvt* cvtptr; // exposition only
53
- state_type cvtstate; // exposition only
54
- size_t cvtcount; // exposition only
55
- };
56
- }
57
- ```
58
-
59
- The class template describes an object that controls conversions between
60
- wide string objects of class `std::basic_string<Elem, char_traits<Elem>,
61
- Wide_alloc>` and byte string objects of class `std::\\basic_string<char,
62
- char_traits<char>, Byte_alloc>`. The class template defines the types
63
- `wide_string` and `byte_string` as synonyms for these two types.
64
- Conversion between a sequence of `Elem` values (stored in a
65
- `wide_string` object) and multibyte sequences (stored in a `byte_string`
66
- object) is performed by an object of class `Codecvt`, which meets the
67
- requirements of the standard code-conversion facet `std::codecvt<Elem,
68
- char, std::mbstate_t>`.
69
-
70
- An object of this class template stores:
71
-
72
- - `byte_err_string` — a byte string to display on errors
73
- - `wide_err_string` — a wide string to display on errors
74
- - `cvtptr` — a pointer to the allocated conversion object (which is
75
- freed when the `wstring_convert` object is destroyed)
76
- - `cvtstate` — a conversion state object
77
- - `cvtcount` — a conversion count
78
-
79
- ``` cpp
80
- typedef std::basic_string<char, char_traits<char>, Byte_alloc> byte_string;
81
- ```
82
-
83
- The type shall be a synonym for
84
- `std::basic_string<char, char_traits<char>, Byte_alloc>`
85
-
86
- ``` cpp
87
- size_t converted() const noexcept;
88
- ```
89
-
90
- *Returns:* `cvtcount`.
91
-
92
- ``` cpp
93
- wide_string from_bytes(char byte);
94
- wide_string from_bytes(const char* ptr);
95
- wide_string from_bytes(const byte_string& str);
96
- wide_string from_bytes(const char* first, const char* last);
97
- ```
98
-
99
- *Effects:* The first member function shall convert the single-element
100
- sequence `byte` to a wide string. The second member function shall
101
- convert the null-terminated sequence beginning at `ptr` to a wide
102
- string. The third member function shall convert the sequence stored in
103
- `str` to a wide string. The fourth member function shall convert the
104
- sequence defined by the range \[`first`, `last`) to a wide string.
105
-
106
- In all cases:
107
-
108
- - If the `cvtstate` object was not constructed with an explicit value,
109
- it shall be set to its default value (the initial conversion state)
110
- before the conversion begins. Otherwise it shall be left unchanged.
111
- - The number of input elements successfully converted shall be stored in
112
- `cvtcount`.
113
-
114
- *Returns:* If no conversion error occurs, the member function shall
115
- return the converted wide string. Otherwise, if the object was
116
- constructed with a wide-error string, the member function shall return
117
- the wide-error string. Otherwise, the member function throws an object
118
- of class `std::range_error`.
119
-
120
- ``` cpp
121
- typedef typename wide_string::traits_type::int_type int_type;
122
- ```
123
-
124
- The type shall be a synonym for `wide_string::traits_type::int_type`.
125
-
126
- ``` cpp
127
- state_type state() const;
128
- ```
129
-
130
- returns `cvtstate`.
131
-
132
- ``` cpp
133
- typedef typename Codecvt::state_type state_type;
134
- ```
135
-
136
- The type shall be a synonym for `Codecvt::state_type`.
137
-
138
- ``` cpp
139
- byte_string to_bytes(Elem wchar);
140
- byte_string to_bytes(const Elem* wptr);
141
- byte_string to_bytes(const wide_string& wstr);
142
- byte_string to_bytes(const Elem* first, const Elem* last);
143
- ```
144
-
145
- *Effects:* The first member function shall convert the single-element
146
- sequence `wchar` to a byte string. The second member function shall
147
- convert the null-terminated sequence beginning at `wptr` to a byte
148
- string. The third member function shall convert the sequence stored in
149
- `wstr` to a byte string. The fourth member function shall convert the
150
- sequence defined by the range \[`first`, `last`) to a byte string.
151
-
152
- In all cases:
153
-
154
- - If the `cvtstate` object was not constructed with an explicit value,
155
- it shall be set to its default value (the initial conversion state)
156
- before the conversion begins. Otherwise it shall be left unchanged.
157
- - The number of input elements successfully converted shall be stored in
158
- `cvtcount`.
159
-
160
- *Returns:* If no conversion error occurs, the member function shall
161
- return the converted byte string. Otherwise, if the object was
162
- constructed with a byte-error string, the member function shall return
163
- the byte-error string. Otherwise, the member function shall throw an
164
- object of class `std::range_error`.
165
-
166
- ``` cpp
167
- typedef std::basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
168
- ```
169
-
170
- The type shall be a synonym for
171
- `std::basic_string<Elem, char_traits<Elem>, Wide_alloc>`.
172
-
173
- ``` cpp
174
- explicit wstring_convert(Codecvt* pcvt = new Codecvt);
175
- wstring_convert(Codecvt* pcvt, state_type state);
176
- explicit wstring_convert(const byte_string& byte_err,
177
- const wide_string& wide_err = wide_string());
178
- ```
179
-
180
- *Requires:* For the first and second constructors, `pcvt != nullptr`.
181
-
182
- *Effects:* The first constructor shall store `pcvt` in `cvtptr` and
183
- default values in `cvtstate`, `byte_err_string`, and `wide_err_string`.
184
- The second constructor shall store `pcvt` in `cvtptr`, `state` in
185
- `cvtstate`, and default values in `byte_err_string` and
186
- `wide_err_string`; moreover the stored state shall be retained between
187
- calls to `from_bytes` and `to_bytes`. The third constructor shall store
188
- `new Codecvt` in `cvtptr`, `state_type()` in `cvtstate`, `byte_err` in
189
- `byte_err_string`, and `wide_err` in `wide_err_string`.
190
-
191
- ``` cpp
192
- ~wstring_convert();
193
- ```
194
-
195
- *Effects:* The destructor shall delete `cvtptr`.
196
-