source: mainline/uspace/lib/cpp/src/string.cpp@ 28a5ebd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 28a5ebd was bc56f30, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Make some libc and libposix headers usable in C++

These headers either get included from standard C++ headers,
or are standard themselves, which means any unnamespaced nonstandard
identifiers are a problem. This commit attempts to fix those
issues, and removes hacks previously used in libcpp to work around it.

  • Property mode set to 100644
File size: 8.2 KB
RevLine 
[82b6716]1/*
[7dcce0a]2 * Copyright (c) 2019 Jaroslav Jindrak
[82b6716]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
[7dcce0a]29#include <cassert>
[82b6716]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
[7dcce0a]37 __unimplemented();
[82b6716]38 return 0;
39 }
40
41 long stol(const string& str, size_t* idx, int base)
42 {
43 char* end;
[bc56f30]44 long result = ::strtol(str.c_str(), &end, base);
[82b6716]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;
[bc56f30]60 unsigned long result = ::strtoul(str.c_str(), &end, base);
[82b6716]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
[7dcce0a]76 __unimplemented();
[82b6716]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
[7dcce0a]83 __unimplemented();
[82b6716]84 return 0;
85 }
86
87 float stof(const string& str, size_t* idx)
88 {
89 // TODO: implement
[7dcce0a]90 __unimplemented();
[82b6716]91 return 0.f;
92 }
93
94 double stod(const string& str, size_t* idx)
95 {
96 // TODO: implement
[7dcce0a]97 __unimplemented();
[82b6716]98 return 0.0;
99 }
100
101 long double stold(const string& str, size_t* idx)
102 {
103 // TODO: implement
[7dcce0a]104 __unimplemented();
[82b6716]105 return 0.0l;
106 }
107
108 string to_string(int val)
109 {
110 char* tmp;
[bc56f30]111 ::asprintf(&tmp, "%d", val);
[82b6716]112
113 std::string res{tmp};
114 free(tmp);
115
116 return res;
117 }
118
119 string to_string(unsigned val)
120 {
121 char* tmp;
[bc56f30]122 ::asprintf(&tmp, "%u", val);
[82b6716]123
124 std::string res{tmp};
125 free(tmp);
126
127 return res;
128 }
129
130 string to_string(long val)
131 {
132 char* tmp;
[bc56f30]133 ::asprintf(&tmp, "%ld", val);
[82b6716]134
135 std::string res{tmp};
136 free(tmp);
137
138 return res;
139 }
140
141 string to_string(unsigned long val)
142 {
143 char* tmp;
[bc56f30]144 ::asprintf(&tmp, "%lu", val);
[82b6716]145
146 std::string res{tmp};
147 free(tmp);
148
149 return res;
150 }
151
152 string to_string(long long val)
153 {
154 char* tmp;
[bc56f30]155 ::asprintf(&tmp, "%lld", val);
[82b6716]156
157 std::string res{tmp};
158 free(tmp);
159
160 return res;
161 }
162
163 string to_string(unsigned long long val)
164 {
165 char* tmp;
[bc56f30]166 ::asprintf(&tmp, "%llu", val);
[82b6716]167
168 std::string res{tmp};
169 free(tmp);
170
171 return res;
172 }
173
174 string to_string(float val)
175 {
176 char* tmp;
[bc56f30]177 ::asprintf(&tmp, "%f", val);
[82b6716]178
179 std::string res{tmp};
180 free(tmp);
181
182 return res;
183 }
184
185 string to_string(double val)
186 {
187 char* tmp;
[bc56f30]188 ::asprintf(&tmp, "%f", val);
[82b6716]189
190 std::string res{tmp};
191 free(tmp);
192
193 return res;
194 }
195
196 string to_string(long double val)
197 {
198 char* tmp;
[bc56f30]199 ::asprintf(&tmp, "%Lf", val);
[82b6716]200
201 std::string res{tmp};
202 free(tmp);
203
204 return res;
205 }
206
207 int stoi(const wstring& str, size_t* idx, int base)
208 {
209 // TODO: implement
[7dcce0a]210 __unimplemented();
[82b6716]211 return 0;
212 }
213
214 long stol(const wstring& str, size_t* idx, int base)
215 {
216 // TODO: implement
[7dcce0a]217 __unimplemented();
[82b6716]218 return 0;
219 }
220
221 unsigned long stoul(const wstring& str, size_t* idx, int base)
222 {
223 // TODO: implement
[7dcce0a]224 __unimplemented();
[82b6716]225 return 0;
226 }
227
228 long long stoll(const wstring& str, size_t* idx, int base)
229 {
230 // TODO: implement
[7dcce0a]231 __unimplemented();
[82b6716]232 return 0;
233 }
234
235 unsigned long long stoull(const wstring& str, size_t* idx, int base)
236 {
237 // TODO: implement
[7dcce0a]238 __unimplemented();
[82b6716]239 return 0;
240 }
241
242 float stof(const wstring& str, size_t* idx)
243 {
244 // TODO: implement
[7dcce0a]245 __unimplemented();
[82b6716]246 return 0.f;
247 }
248
249 double stod(const wstring& str, size_t* idx)
250 {
251 // TODO: implement
[7dcce0a]252 __unimplemented();
[82b6716]253 return 0.0;
254 }
255
256 long double stold(const wstring& str, size_t* idx)
257 {
258 // TODO: implement
[7dcce0a]259 __unimplemented();
[82b6716]260 return 0.0l;
261 }
262
263 wstring to_wstring(int val)
264 {
265 // TODO: implement
[7dcce0a]266 __unimplemented();
[82b6716]267 return wstring{};
268 }
269
270 wstring to_wstring(unsigned val)
271 {
272 // TODO: implement
[7dcce0a]273 __unimplemented();
[82b6716]274 return wstring{};
275 }
276
277 wstring to_wstring(long val)
278 {
279 // TODO: implement
[7dcce0a]280 __unimplemented();
[82b6716]281 return wstring{};
282 }
283
284 wstring to_wstring(unsigned long val)
285 {
286 // TODO: implement
[7dcce0a]287 __unimplemented();
[82b6716]288 return wstring{};
289 }
290
291 wstring to_wstring(long long val)
292 {
293 // TODO: implement
[7dcce0a]294 __unimplemented();
[82b6716]295 return wstring{};
296 }
297
298 wstring to_wstring(unsigned long long val)
299 {
300 // TODO: implement
[7dcce0a]301 __unimplemented();
[82b6716]302 return wstring{};
303 }
304
305 wstring to_wstring(float val)
306 {
307 // TODO: implement
[7dcce0a]308 __unimplemented();
[82b6716]309 return wstring{};
310 }
311
312 wstring to_wstring(double val)
313 {
314 // TODO: implement
[7dcce0a]315 __unimplemented();
[82b6716]316 return wstring{};
317 }
318
319 wstring to_wstring(long double val)
320 {
321 // TODO: implement
[7dcce0a]322 __unimplemented();
[82b6716]323 return wstring{};
324 }
325
326 /**
327 * 21.7, suffix for basic_string literals:
328 */
329
330 /**
331 * Note: According to the standard, literal suffixes that do not
332 * start with an underscore are reserved for future standardization,
333 * but since we are implementing the standard, we're going to ignore it.
334 * This should work (according to their documentation) work for clang,
335 * but that should be tested.
336 */
337#pragma GCC diagnostic push
338#pragma GCC diagnostic ignored "-Wliteral-suffix"
[cbf9099]339inline namespace literals {
340inline namespace string_literals
341{
[e7c6250]342 string operator "" s(const char* str, size_t len)
[82b6716]343 {
344 return string{str, len};
345 }
346
[e7c6250]347 u16string operator "" s(const char16_t* str, size_t len)
[82b6716]348 {
349 return u16string{str, len};
350 }
351
[e7c6250]352 u32string operator "" s(const char32_t* str, size_t len)
[82b6716]353 {
354 return u32string{str, len};
355 }
356
357 /* Problem: wchar_t == int in HelenOS, but standard forbids it.
[e7c6250]358 wstring operator "" s(const wchar_t* str, size_t len)
[82b6716]359 {
360 return wstring{str, len};
361 }
362 */
[cbf9099]363}}
[82b6716]364#pragma GCC diagnostic pop
365}
Note: See TracBrowser for help on using the repository browser.