acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
_string.h
1// Created by camilo on 2023-02-02 02:28 <3ThomasBorregaardSorensen!!
2#pragma once
3
4
13inline ::i32 utoa_base(char_pointer psz, size_t size, ::u64 u, ::i32 base);
14
23inline ::i32 itoa_base(char_pointer psz, size_t size, ::i64 i, ::i32 base);
24
25
26constexpr character_count _utoa_base(char_pointer psz, character_count size, ::u64 u, ::i32 base)
27{
28
29 while (true)
30 {
31
32 ::i32 digit = (u % base);
33
34 u /= base;
35
36 if (!u)
37 {
38
39 break;
40
41 }
42
43 if (psz && size > 0)
44 {
45
46 *psz = __digit(digit);
47
48 }
49
50 size--;
51
52 }
53
54 return -size;
55
56}
57
58
59inline character_count utoa_base(char_pointer psz, character_count size, ::u64 u, ::i32 base)
60{
61
62 if (base < 2 || base > 36)
63 {
64
65 throw error_bad_argument;
66
67 return -1;
68
69 }
70
71 auto res = _utoa_base(psz, size - 1, u, base);
72
73 if (psz)
74 {
75
76 psz[minimum(size - 1, size - 1 + res)] = '\0';
77
78 }
79
80 return res;
81
82}
83
84
85constexpr character_count _itoa_base(char_pointer psz, character_count size, ::i64 i, ::i32 base)
86{
87
88 while (true)
89 {
90
91 ::i32 digit = -(i % base);
92
93 i /= base;
94
95 if (!i)
96 {
97
98 break;
99
100 }
101
102 if (size > 0)
103 {
104
105 *psz = __digit(digit);
106
107 }
108
109 size--;
110
111 }
112
113 return -size;
114
115}
116
117
118inline character_count itoa_base(char_pointer psz, character_count size, ::i64 i, ::i32 base)
119{
120
121 if (base < 2 || base > 36)
122 {
123
124 throw_exception(error_bad_argument);
125
126 return -1;
127
128 }
129
130 if (i < 0)
131 {
132
133 *psz++ = '-';
134
135 size--;
136
137 }
138 else
139 {
140
141 i = -i;
142
143 }
144
145 auto res = _itoa_base(psz, size - 1, i, base);
146
147 if (psz)
148 {
149
150 psz[minimum(size - 1, size - 1 + res)] = '\0';
151
152 }
153
154 return res;
155
156}
157
158
159template < prototype_integral INTEGRAL >
160inline INTEGRAL& _consume(INTEGRAL& i, ::ansi_range& range, ::i32 iBase = 10)
161{
162
163 i = 0;
164
165 auto end = minimum(range.end(), range.begin() + maximum_digits<INTEGRAL>(iBase));
166
167 while (range.begin() < end)
168 {
169
170 auto digit = range.defer_consume_digit(iBase);
171
172 if (digit < 0)
173 {
174
175 throw "_string parsing error _consume";
176
177 }
178
179 i = digit + i * iBase;
180
181 }
182
183 return i;
184
185}
186
187
188template < prototype_unsigned UNSIGNED >
189inline UNSIGNED & consume(UNSIGNED& u, ::ansi_range& range, ::i32 iBase = 10)
190{
191
192 return _consume(u, range, iBase);
193
194}
195
196
197template < prototype_signed SIGNED >
198inline SIGNED & consume(SIGNED & s, ::ansi_range& range, ::i32 iBase = 10)
199{
200
201 bool bNegative = range.begins_eat('-');
202
203 _consume(s, range, iBase);
204
205 if(bNegative) s = -s;
206
207 return s;
208
209}
210
211
212template < prototype_integral INTEGRAL >
213INTEGRAL consume_integral(::ansi_range& range, ::i32 iBase = 10)
214{
215
216 INTEGRAL integral;
217
218 consume(integral, range, iBase);
219
220 return integral;
221
222}
223
224
225template < typename BASE_RANGE >
226inline ::i64 consume_i64(::string_range < BASE_RANGE > & range , ::i32 iBase = 10) { return consume_integral < ::i64 >(range, iBase); }
227template < typename BASE_RANGE >
228inline ::i32 consume_int(::string_range < BASE_RANGE > & range , ::i32 iBase = 10) { return consume_integral < ::i32 >(range, iBase); }
229template < typename BASE_RANGE >
230inline ::i16 consume_short(::string_range < BASE_RANGE > & range , ::i32 iBase = 10) { return consume_integral < ::i16 >(range, iBase); }
231template < typename BASE_RANGE >
232inline ::i8 consume_char(::string_range < BASE_RANGE > & range, ::i32 iBase = 10) { return consume_integral < ::i8 >(range, iBase); }
233
234
235template < typename BASE_RANGE >
236inline ::u64 consume_u64(::string_range < BASE_RANGE >& range, ::i32 iBase = 10) { return consume_integral < ::u64 >(range, iBase); }
237template < typename BASE_RANGE >
238inline ::u32 consume_u32(::string_range < BASE_RANGE >& range, ::i32 iBase = 10) { return consume_integral < ::u32 >(range, iBase); }
239template < typename BASE_RANGE >
240inline ::u16 consume_unsigned_short(::string_range < BASE_RANGE >& range, ::i32 iBase = 10) { return consume_integral < ::u16 >(range, iBase); }
241template < typename BASE_RANGE >
242inline ::u8 consume_unsigned_char(::string_range < BASE_RANGE >& range, ::i32 iBase = 10) { return consume_integral < ::u8 >(range, iBase); }
243
244template < typename BASE_RANGE >
245inline long consume_long(::string_range < BASE_RANGE >& range, ::i32 iBase = 10) { return consume_integral < long >(range, iBase); }
246template < typename BASE_RANGE >
247inline ulong consume_unsigned_long(::string_range < BASE_RANGE >& range, ::i32 iBase = 10) { return consume_integral < ulong >(range, iBase); }
248template < typename BASE_RANGE >
249inline size_t consume_size_t(::string_range < BASE_RANGE >& range, ::i32 iBase = 10) { return consume_integral < size_t >(range, iBase); }
250
251
252inline ::i64 as_i64(const ::scoped_string& scopedstr, ::i32 iBase = 10) { auto r = scopedstr(); return consume_i64(r, iBase); }
253inline ::i32 as_i32(const ::scoped_string& scopedstr, ::i32 iBase = 10) { auto r = scopedstr(); return consume_int(r, iBase); }
254inline ::i32 as_i16(const ::scoped_string& scopedstr, ::i32 iBase = 10) { auto r = scopedstr(); return consume_short(r, iBase); }
255inline ::i8 as_i8(const ::scoped_string& scopedstr, ::i32 iBase = 10) { auto r = scopedstr(); return consume_char(r, iBase); }
256
257
258inline ::u64 as_u64(const ::scoped_string& scopedstr, ::i32 iBase = 10) { auto r = scopedstr(); return consume_u64(r, iBase); }
259inline ::u32 as_u32(const ::scoped_string& scopedstr, ::i32 iBase = 10) { auto r = scopedstr(); return consume_u32(r, iBase); }
260inline ::u32 as_u16(const ::scoped_string& scopedstr, ::i32 iBase = 10) { auto r = scopedstr(); return consume_unsigned_short(r, iBase); }
261inline ::u8 as_u8(const ::scoped_string& scopedstr, ::i32 iBase = 10) { auto r = scopedstr(); return consume_unsigned_char(r, iBase); }
262
263
264inline long as_long(const ::scoped_string& scopedstr, ::i32 iBase = 10) { auto r = scopedstr(); return consume_long(r, iBase); }
265inline ulong as_unsigned_long(const ::scoped_string& scopedstr, ::i32 iBase = 10) { auto r = scopedstr(); return consume_unsigned_long(r, iBase); }
266inline size_t as_size_t(const ::scoped_string& scopedstr, ::i32 iBase = 10) { auto r = scopedstr(); return consume_size_t(r, iBase); }
267
268//inline ::i32 as_i32(const ::scoped_string & scopedstr, ::i32 iBase = 10)
269//{
270//
271// auto i = as_i64(scopedstr, iBase);
272//
273// if (i < INT_MIN || i > INT_MAX)
274// {
275//
276// throw_exception(error_overflow);
277//
278// }
279//
280// return (::i32)i;
281//
282//}
283//
284//
285//inline ::i16 as_i16(const ::scoped_string & scopedstr, ::i32 iBase = 10)
286//{
287//
288// auto i = as_i64(scopedstr, iBase);
289//
290// if (i < SHRT_MIN || i > SHRT_MAX)
291// {
292//
293// throw_exception(error_overflow);
294//
295// }
296//
297// return (::i16)i;
298//
299//}
300//
301//
302//inline ::i8 as_i8(const ::scoped_string & scopedstr, ::i32 iBase = 10)
303//{
304//
305// auto i = as_i64(scopedstr, iBase);
306//
307// if (i < CHAR_MIN || i > CHAR_MAX)
308// {
309//
310// throw_exception(error_overflow);
311//
312// }
313//
314// return (::i8)i;
315//
316//}
317//
318//
319//inline long as_long(const ::scoped_string & scopedstr, ::i32 iBase = 10)
320//{
321//
322// auto i = as_i64(scopedstr, iBase);
323//
324// if (i < LONG_MIN || i > LONG_MAX)
325// {
326//
327// throw_exception(error_overflow);
328//
329// }
330//
331// return (long)i;
332//
333//}
334//
335//
336//
337//inline ::u64 as_u64(const ::scoped_string & scopedstr, ::i32 iBase = 10)
338//{
339//
340// const_char_pointer pszEnd = nullptr;
341//
342// return ansi_to_u64(scopedstr, &pszEnd, iBase);
343//
344//}
345//
346//
347//inline ::u32 as_u32(const ::scoped_string & scopedstr, ::i32 iBase = 10)
348//{
349//
350// auto u = as_u64(scopedstr, iBase);
351//
352// if (u > UINT_MAX)
353// {
354//
355// throw_exception(error_overflow);
356//
357// }
358//
359// return (::u32)u;
360//
361//}
362//
363//
364//inline ::u16 as_u16(const ::scoped_string & scopedstr, ::i32 iBase = 10)
365//{
366//
367// auto u = as_u64(scopedstr, iBase);
368//
369// if (u > USHRT_MAX)
370// {
371//
372// throw_exception(error_overflow);
373//
374// }
375//
376// return (::u16)u;
377//
378//}
379//
380//
381//inline ::u8 as_u8(const ::scoped_string & scopedstr, ::i32 iBase = 10)
382//{
383//
384// auto u = as_u64(scopedstr, iBase);
385//
386// if (u > UCHAR_MAX)
387// {
388//
389// throw_exception(error_overflow);
390//
391// }
392//
393// return (::u8)u;
394//
395//}
396//
397//
398//inline ulong as_unsigned_long(const ::scoped_string & scopedstr, ::i32 iBase = 10)
399//{
400//
401// auto u = as_u64(scopedstr, iBase);
402//
403// if (u > ULONG_MAX)
404// {
405//
406// throw_exception(error_overflow);
407//
408// }
409//
410// return (ulong)u;
411//
412//}
413//
414//
415//inline size_t as_size_t(const ::scoped_string & scopedstr, ::i32 iBase = 10)
416//{
417//
418// auto u = as_u64(scopedstr, iBase);
419//
420// if (u > SIZE_MAX)
421// {
422//
423// throw_exception(error_overflow);
424//
425// }
426//
427// return (size_t)u;
428//
429//}
430
431
432
433//inline ::f64 as_f64(const ::scoped_string & scopedstr)
434//{
435//
436//
437// return (size_t)u;
438//
439//}
440//
441
442
443template < prototype_integral INTEGRAL >
444inline void from_string(INTEGRAL & i, const ::scoped_string & scopedstr, ::i32 iBase = 10)
445{
446
447 auto r = scopedstr();
448
449 if(r.case_insensitive_equals("FALSE") ||
450 r.case_insensitive_equals("OFF") ||
451 r.case_insensitive_equals("NO"))
452 {
453
454 i = 0;
455
456 }
457 else if(r.case_insensitive_equals("TRUE") ||
458 r.case_insensitive_equals("ON") ||
459 r.case_insensitive_equals("YES"))
460 {
461
462 i = 1;
463
464 }
465 else
466 {
467
468 consume(i, r, iBase);
469
470 }
471
472}
473
474
475template < prototype_floating FLOATING >
476inline void from_string(FLOATING& f, const ::scoped_string& scopedstr)
477{
478
479 ::string str(scopedstr);
480
481 f = (FLOATING) strtod(str.c_str(), nullptr);
482
483}
484
485
486
487
488//inline void fromString(const ::scoped_string& scopedstr, long& dst)
489//{
490// dst = ::as_long(scopedstr);
491//}
492//
493//inline void fromString(const ::scoped_string& scopedstr, size_t& dst)
494//{
495// dst = ::as_size_t(scopedstr);
496//}
497//
498//inline void fromString(const ::scoped_string& scopedstr, ::f32& dst)
499//{
500// dst = ::as_f32(scopedstr);
501//}
502//
503//inline void fromString(const ::scoped_string& scopedstr, ::nano2d::color& dst)
504//{
505// dst = stringToColor(scopedstr);
506//}
507
508
Definition range.h:223