source: mainline/uspace/lib/cpp/src/string.cpp@ 8fd0675f

Last change on this file since 8fd0675f was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[82b6716]1/*
[b57ba05]2 * SPDX-FileCopyrightText: 2019 Jaroslav Jindrak
[82b6716]3 *
[b57ba05]4 * SPDX-License-Identifier: BSD-3-Clause
[82b6716]5 */
6
[7dcce0a]7#include <cassert>
[82b6716]8#include <string>
9
10namespace std
11{
12 int stoi(const string& str, size_t* idx, int base)
13 {
14 // TODO: implement using stol once we have numeric limits
[7dcce0a]15 __unimplemented();
[82b6716]16 return 0;
17 }
18
19 long stol(const string& str, size_t* idx, int base)
20 {
21 char* end;
[bc56f30]22 long result = ::strtol(str.c_str(), &end, base);
[82b6716]23
24 if (end != str.c_str())
25 {
26 if (idx)
27 *idx = static_cast<size_t>(end - str.c_str());
28 return result;
29 }
30 // TODO: no conversion -> invalid_argument
31 // TODO: ERANGE in errno -> out_of_range
32 return 0;
33 }
34
35 unsigned long stoul(const string& str, size_t* idx, int base)
36 {
37 char* end;
[bc56f30]38 unsigned long result = ::strtoul(str.c_str(), &end, base);
[82b6716]39
40 if (end != str.c_str())
41 {
42 if (idx)
43 *idx = static_cast<size_t>(end - str.c_str());
44 return result;
45 }
46 // TODO: no conversion -> invalid_argument
47 // TODO: ERANGE in errno -> out_of_range
48 return 0;
49 }
50
51 long long stoll(const string& str, size_t* idx, int base)
52 {
53 // TODO: implement using stol once we have numeric limits
[7dcce0a]54 __unimplemented();
[82b6716]55 return 0;
56 }
57
58 unsigned long long stoull(const string& str, size_t* idx, int base)
59 {
60 // TODO: implement using stoul once we have numeric limits
[7dcce0a]61 __unimplemented();
[82b6716]62 return 0;
63 }
64
65 float stof(const string& str, size_t* idx)
66 {
67 // TODO: implement
[7dcce0a]68 __unimplemented();
[82b6716]69 return 0.f;
70 }
71
72 double stod(const string& str, size_t* idx)
73 {
74 // TODO: implement
[7dcce0a]75 __unimplemented();
[82b6716]76 return 0.0;
77 }
78
79 long double stold(const string& str, size_t* idx)
80 {
81 // TODO: implement
[7dcce0a]82 __unimplemented();
[82b6716]83 return 0.0l;
84 }
85
86 string to_string(int val)
87 {
88 char* tmp;
[bc56f30]89 ::asprintf(&tmp, "%d", val);
[82b6716]90
91 std::string res{tmp};
92 free(tmp);
93
94 return res;
95 }
96
97 string to_string(unsigned val)
98 {
99 char* tmp;
[bc56f30]100 ::asprintf(&tmp, "%u", val);
[82b6716]101
102 std::string res{tmp};
103 free(tmp);
104
105 return res;
106 }
107
108 string to_string(long val)
109 {
110 char* tmp;
[bc56f30]111 ::asprintf(&tmp, "%ld", val);
[82b6716]112
113 std::string res{tmp};
114 free(tmp);
115
116 return res;
117 }
118
119 string to_string(unsigned long val)
120 {
121 char* tmp;
[bc56f30]122 ::asprintf(&tmp, "%lu", val);
[82b6716]123
124 std::string res{tmp};
125 free(tmp);
126
127 return res;
128 }
129
130 string to_string(long long val)
131 {
132 char* tmp;
[bc56f30]133 ::asprintf(&tmp, "%lld", val);
[82b6716]134
135 std::string res{tmp};
136 free(tmp);
137
138 return res;
139 }
140
141 string to_string(unsigned long long val)
142 {
143 char* tmp;
[bc56f30]144 ::asprintf(&tmp, "%llu", val);
[82b6716]145
146 std::string res{tmp};
147 free(tmp);
148
149 return res;
150 }
151
152 string to_string(float val)
153 {
154 char* tmp;
[bc56f30]155 ::asprintf(&tmp, "%f", val);
[82b6716]156
157 std::string res{tmp};
158 free(tmp);
159
160 return res;
161 }
162
163 string to_string(double val)
164 {
165 char* tmp;
[bc56f30]166 ::asprintf(&tmp, "%f", val);
[82b6716]167
168 std::string res{tmp};
169 free(tmp);
170
171 return res;
172 }
173
174 string to_string(long double val)
175 {
176 char* tmp;
[bc56f30]177 ::asprintf(&tmp, "%Lf", val);
[82b6716]178
179 std::string res{tmp};
180 free(tmp);
181
182 return res;
183 }
184
185 int stoi(const wstring& str, size_t* idx, int base)
186 {
187 // TODO: implement
[7dcce0a]188 __unimplemented();
[82b6716]189 return 0;
190 }
191
192 long stol(const wstring& str, size_t* idx, int base)
193 {
194 // TODO: implement
[7dcce0a]195 __unimplemented();
[82b6716]196 return 0;
197 }
198
199 unsigned long stoul(const wstring& str, size_t* idx, int base)
200 {
201 // TODO: implement
[7dcce0a]202 __unimplemented();
[82b6716]203 return 0;
204 }
205
206 long long stoll(const wstring& str, size_t* idx, int base)
207 {
208 // TODO: implement
[7dcce0a]209 __unimplemented();
[82b6716]210 return 0;
211 }
212
213 unsigned long long stoull(const wstring& str, size_t* idx, int base)
214 {
215 // TODO: implement
[7dcce0a]216 __unimplemented();
[82b6716]217 return 0;
218 }
219
220 float stof(const wstring& str, size_t* idx)
221 {
222 // TODO: implement
[7dcce0a]223 __unimplemented();
[82b6716]224 return 0.f;
225 }
226
227 double stod(const wstring& str, size_t* idx)
228 {
229 // TODO: implement
[7dcce0a]230 __unimplemented();
[82b6716]231 return 0.0;
232 }
233
234 long double stold(const wstring& str, size_t* idx)
235 {
236 // TODO: implement
[7dcce0a]237 __unimplemented();
[82b6716]238 return 0.0l;
239 }
240
241 wstring to_wstring(int val)
242 {
243 // TODO: implement
[7dcce0a]244 __unimplemented();
[82b6716]245 return wstring{};
246 }
247
248 wstring to_wstring(unsigned val)
249 {
250 // TODO: implement
[7dcce0a]251 __unimplemented();
[82b6716]252 return wstring{};
253 }
254
255 wstring to_wstring(long val)
256 {
257 // TODO: implement
[7dcce0a]258 __unimplemented();
[82b6716]259 return wstring{};
260 }
261
262 wstring to_wstring(unsigned long val)
263 {
264 // TODO: implement
[7dcce0a]265 __unimplemented();
[82b6716]266 return wstring{};
267 }
268
269 wstring to_wstring(long long val)
270 {
271 // TODO: implement
[7dcce0a]272 __unimplemented();
[82b6716]273 return wstring{};
274 }
275
276 wstring to_wstring(unsigned long long val)
277 {
278 // TODO: implement
[7dcce0a]279 __unimplemented();
[82b6716]280 return wstring{};
281 }
282
283 wstring to_wstring(float val)
284 {
285 // TODO: implement
[7dcce0a]286 __unimplemented();
[82b6716]287 return wstring{};
288 }
289
290 wstring to_wstring(double val)
291 {
292 // TODO: implement
[7dcce0a]293 __unimplemented();
[82b6716]294 return wstring{};
295 }
296
297 wstring to_wstring(long double val)
298 {
299 // TODO: implement
[7dcce0a]300 __unimplemented();
[82b6716]301 return wstring{};
302 }
303
304 /**
305 * 21.7, suffix for basic_string literals:
306 */
307
308 /**
309 * Note: According to the standard, literal suffixes that do not
310 * start with an underscore are reserved for future standardization,
311 * but since we are implementing the standard, we're going to ignore it.
312 * This should work (according to their documentation) work for clang,
313 * but that should be tested.
314 */
315#pragma GCC diagnostic push
316#pragma GCC diagnostic ignored "-Wliteral-suffix"
[cbf9099]317inline namespace literals {
318inline namespace string_literals
319{
[e7c6250]320 string operator "" s(const char* str, size_t len)
[82b6716]321 {
322 return string{str, len};
323 }
324
[e7c6250]325 u16string operator "" s(const char16_t* str, size_t len)
[82b6716]326 {
327 return u16string{str, len};
328 }
329
[e7c6250]330 u32string operator "" s(const char32_t* str, size_t len)
[82b6716]331 {
332 return u32string{str, len};
333 }
334
335 /* Problem: wchar_t == int in HelenOS, but standard forbids it.
[e7c6250]336 wstring operator "" s(const wchar_t* str, size_t len)
[82b6716]337 {
338 return wstring{str, len};
339 }
340 */
[cbf9099]341}}
[82b6716]342#pragma GCC diagnostic pop
343}
Note: See TracBrowser for help on using the repository browser.