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

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

cpp: added hel:: where needed and moved string literals to the proper namespace

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