source: mainline/uspace/lib/cpp/src/string.cpp@ a4e78743

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a4e78743 was 7dcce0a, checked in by jxsvoboda <5887334+jxsvoboda@…>, 6 years ago

cpp: abort and report when an unimplemented function is called

  • Property mode set to 100644
File size: 8.3 KB
Line 
1/*
2 * Copyright (c) 2019 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <cassert>
30#include <string>
31
32namespace std
33{
34 int stoi(const string& str, size_t* idx, int base)
35 {
36 // TODO: implement using stol once we have numeric limits
37 __unimplemented();
38 return 0;
39 }
40
41 long stol(const string& str, size_t* idx, int base)
42 {
43 char* end;
44 long result = hel::strtol(str.c_str(), &end, base);
45
46 if (end != str.c_str())
47 {
48 if (idx)
49 *idx = static_cast<size_t>(end - str.c_str());
50 return result;
51 }
52 // TODO: no conversion -> invalid_argument
53 // TODO: ERANGE in errno -> out_of_range
54 return 0;
55 }
56
57 unsigned long stoul(const string& str, size_t* idx, int base)
58 {
59 char* end;
60 unsigned long result = hel::strtoul(str.c_str(), &end, base);
61
62 if (end != str.c_str())
63 {
64 if (idx)
65 *idx = static_cast<size_t>(end - str.c_str());
66 return result;
67 }
68 // TODO: no conversion -> invalid_argument
69 // TODO: ERANGE in errno -> out_of_range
70 return 0;
71 }
72
73 long long stoll(const string& str, size_t* idx, int base)
74 {
75 // TODO: implement using stol once we have numeric limits
76 __unimplemented();
77 return 0;
78 }
79
80 unsigned long long stoull(const string& str, size_t* idx, int base)
81 {
82 // TODO: implement using stoul once we have numeric limits
83 __unimplemented();
84 return 0;
85 }
86
87 float stof(const string& str, size_t* idx)
88 {
89 // TODO: implement
90 __unimplemented();
91 return 0.f;
92 }
93
94 double stod(const string& str, size_t* idx)
95 {
96 // TODO: implement
97 __unimplemented();
98 return 0.0;
99 }
100
101 long double stold(const string& str, size_t* idx)
102 {
103 // TODO: implement
104 __unimplemented();
105 return 0.0l;
106 }
107
108 namespace hel
109 {
110 extern "C" int asprintf(char**, const char*, ...);
111 }
112
113 string to_string(int val)
114 {
115 char* tmp;
116 hel::asprintf(&tmp, "%d", val);
117
118 std::string res{tmp};
119 free(tmp);
120
121 return res;
122 }
123
124 string to_string(unsigned val)
125 {
126 char* tmp;
127 hel::asprintf(&tmp, "%u", val);
128
129 std::string res{tmp};
130 free(tmp);
131
132 return res;
133 }
134
135 string to_string(long val)
136 {
137 char* tmp;
138 hel::asprintf(&tmp, "%ld", val);
139
140 std::string res{tmp};
141 free(tmp);
142
143 return res;
144 }
145
146 string to_string(unsigned long val)
147 {
148 char* tmp;
149 hel::asprintf(&tmp, "%lu", val);
150
151 std::string res{tmp};
152 free(tmp);
153
154 return res;
155 }
156
157 string to_string(long long val)
158 {
159 char* tmp;
160 hel::asprintf(&tmp, "%lld", val);
161
162 std::string res{tmp};
163 free(tmp);
164
165 return res;
166 }
167
168 string to_string(unsigned long long val)
169 {
170 char* tmp;
171 hel::asprintf(&tmp, "%llu", val);
172
173 std::string res{tmp};
174 free(tmp);
175
176 return res;
177 }
178
179 string to_string(float val)
180 {
181 char* tmp;
182 hel::asprintf(&tmp, "%f", val);
183
184 std::string res{tmp};
185 free(tmp);
186
187 return res;
188 }
189
190 string to_string(double val)
191 {
192 char* tmp;
193 hel::asprintf(&tmp, "%f", val);
194
195 std::string res{tmp};
196 free(tmp);
197
198 return res;
199 }
200
201 string to_string(long double val)
202 {
203 char* tmp;
204 hel::asprintf(&tmp, "%Lf", val);
205
206 std::string res{tmp};
207 free(tmp);
208
209 return res;
210 }
211
212 int stoi(const wstring& str, size_t* idx, int base)
213 {
214 // TODO: implement
215 __unimplemented();
216 return 0;
217 }
218
219 long stol(const wstring& str, size_t* idx, int base)
220 {
221 // TODO: implement
222 __unimplemented();
223 return 0;
224 }
225
226 unsigned long stoul(const wstring& str, size_t* idx, int base)
227 {
228 // TODO: implement
229 __unimplemented();
230 return 0;
231 }
232
233 long long stoll(const wstring& str, size_t* idx, int base)
234 {
235 // TODO: implement
236 __unimplemented();
237 return 0;
238 }
239
240 unsigned long long stoull(const wstring& str, size_t* idx, int base)
241 {
242 // TODO: implement
243 __unimplemented();
244 return 0;
245 }
246
247 float stof(const wstring& str, size_t* idx)
248 {
249 // TODO: implement
250 __unimplemented();
251 return 0.f;
252 }
253
254 double stod(const wstring& str, size_t* idx)
255 {
256 // TODO: implement
257 __unimplemented();
258 return 0.0;
259 }
260
261 long double stold(const wstring& str, size_t* idx)
262 {
263 // TODO: implement
264 __unimplemented();
265 return 0.0l;
266 }
267
268 wstring to_wstring(int val)
269 {
270 // TODO: implement
271 __unimplemented();
272 return wstring{};
273 }
274
275 wstring to_wstring(unsigned val)
276 {
277 // TODO: implement
278 __unimplemented();
279 return wstring{};
280 }
281
282 wstring to_wstring(long val)
283 {
284 // TODO: implement
285 __unimplemented();
286 return wstring{};
287 }
288
289 wstring to_wstring(unsigned long val)
290 {
291 // TODO: implement
292 __unimplemented();
293 return wstring{};
294 }
295
296 wstring to_wstring(long long val)
297 {
298 // TODO: implement
299 __unimplemented();
300 return wstring{};
301 }
302
303 wstring to_wstring(unsigned long long val)
304 {
305 // TODO: implement
306 __unimplemented();
307 return wstring{};
308 }
309
310 wstring to_wstring(float val)
311 {
312 // TODO: implement
313 __unimplemented();
314 return wstring{};
315 }
316
317 wstring to_wstring(double val)
318 {
319 // TODO: implement
320 __unimplemented();
321 return wstring{};
322 }
323
324 wstring to_wstring(long double val)
325 {
326 // TODO: implement
327 __unimplemented();
328 return wstring{};
329 }
330
331 /**
332 * 21.7, suffix for basic_string literals:
333 */
334
335 /**
336 * Note: According to the standard, literal suffixes that do not
337 * start with an underscore are reserved for future standardization,
338 * but since we are implementing the standard, we're going to ignore it.
339 * This should work (according to their documentation) work for clang,
340 * but that should be tested.
341 */
342#pragma GCC diagnostic push
343#pragma GCC diagnostic ignored "-Wliteral-suffix"
344inline namespace literals {
345inline namespace string_literals
346{
347 string operator "" s(const char* str, size_t len)
348 {
349 return string{str, len};
350 }
351
352 u16string operator "" s(const char16_t* str, size_t len)
353 {
354 return u16string{str, len};
355 }
356
357 u32string operator "" s(const char32_t* str, size_t len)
358 {
359 return u32string{str, len};
360 }
361
362 /* Problem: wchar_t == int in HelenOS, but standard forbids it.
363 wstring operator "" s(const wchar_t* str, size_t len)
364 {
365 return wstring{str, len};
366 }
367 */
368}}
369#pragma GCC diagnostic pop
370}
Note: See TracBrowser for help on using the repository browser.