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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1834a01 was c735afb, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: fix problems caused by new HelenOS changes (and leftowers from rebase)

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