-
Notifications
You must be signed in to change notification settings - Fork 19
/
strnum.c
907 lines (761 loc) · 21.8 KB
/
strnum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/*
*/
#ifndef STRNUM_NOFLOAT
#include <math.h>
#endif
#include <limits.h>
#include <ctype.h>
#include "strnum.h"
//********************************************************************************************************
// Local defines
//********************************************************************************************************
// #include <stdio.h>
// #define DBG(_fmtarg, ...) printf("%s:%.4i - "_fmtarg"\n" , __FILE__, __LINE__ ,##__VA_ARGS__)
#define BASE_PREFIX_LEN 2
#ifndef STRNUM_NOFLOAT
typedef struct float_components_t
{
int options;
strview_t num;
bool is_neg;
float special_value; // NAN, +INF, -INF, or 0.0 if not special
strview_t integral_view;
strview_t fractional_view;
bool got_exponent;
int exp_value;
}float_components_t;
#endif
//********************************************************************************************************
// Private prototypes
//********************************************************************************************************
static int consume_signed(long long* dst, strview_t* src, int options, long long limit_min, long long limit_max);
static int consume_unsigned(unsigned long long* dst, strview_t* src, int options, unsigned long long limit_max);
static bool consume_sign(strview_t* num);
static void consume_base_prefix(int* base, strview_t* src, int options);
// pop digits from source, until a non-digit is found, or the next digit would overflow an unsigned long long
static int consume_digits(unsigned long long* dst, strview_t *src, int base);
static int consume_decimal_digits(unsigned long long* dst, strview_t* str);
static int consume_hex_digits(unsigned long long* dst, strview_t* str);
static int consume_bin_digits(unsigned long long* dst, strview_t* str);
#ifndef STRNUM_NOFLOAT
static int process_float_components(float_components_t* fc);
static float consume_float_special(float_components_t* fc);
static int consume_exponent(float_components_t* fc);
static float make_float(strview_t integral_digits, strview_t fractional_digits, int exp_value);
static double make_double(strview_t integral_digits, strview_t fractional_digits, int exp_value);
static long double make_ldouble(strview_t integral_digits, strview_t fractional_digits, int exp_value);
#endif
static bool upper_nibble_ull_is_zero(unsigned long long i);
static bool upper_bit_ull_is_zero(unsigned long long i);
static int xdigit_value(char c);
static bool isbdigit(char c);
static strview_t split_digits(strview_t* src);
//********************************************************************************************************
// Public functions
//********************************************************************************************************
int strnum_consume_uchar(unsigned char* dst, strview_t* src, int options)
{
int err;
unsigned long long val;
err = consume_unsigned(&val, src, options, UCHAR_MAX);
if(!err && dst)
*dst = (unsigned char)val;
return err;
}
int strnum_consume_ushort(unsigned short* dst, strview_t* src, int options)
{
int err;
unsigned long long val;
err = consume_unsigned(&val, src, options, USHRT_MAX);
if(!err && dst)
*dst = (unsigned short)val;
return err;
}
int strnum_consume_uint(unsigned int* dst, strview_t* src, int options)
{
int err;
unsigned long long val;
err = consume_unsigned(&val, src, options, UINT_MAX);
if(!err && dst)
*dst = (unsigned int)val;
return err;
}
int strnum_consume_ulong(unsigned long* dst, strview_t* src, int options)
{
int err;
unsigned long long val;
err = consume_unsigned(&val, src, options, ULONG_MAX);
if(!err && dst)
*dst = (unsigned long)val;
return err;
}
int strnum_consume_ullong(unsigned long long* dst, strview_t* src, int options)
{
return consume_unsigned(dst, src, options, ULLONG_MAX);
}
int strnum_consume_char(char* dst, strview_t* src, int options)
{
int err;
long long val;
err = consume_signed(&val, src, options, CHAR_MIN, CHAR_MAX);
if(!err && dst)
*dst = (char)val;
return err;
}
int strnum_consume_short(short* dst, strview_t* src, int options)
{
int err;
long long val;
err = consume_signed(&val, src, options, SHRT_MIN, SHRT_MAX);
if(!err && dst)
*dst = (short)val;
return err;
}
int strnum_consume_int(int* dst, strview_t* src, int options)
{
int err;
long long val;
err = consume_signed(&val, src, options, INT_MIN, INT_MAX);
if(!err && dst)
*dst = (int)val;
return err;
}
int strnum_consume_long(long* dst, strview_t* src, int options)
{
int err;
long long val;
err = consume_signed(&val, src, options, LONG_MIN, LONG_MAX);
if(!err && dst)
*dst = (long)val;
return err;
}
int strnum_consume_llong(long long* dst, strview_t* src, int options)
{
return consume_signed(dst, src, options, LLONG_MIN, LLONG_MAX);
}
int8_t strnum_i8(strview_t src, int8_t dfault, int options)
{
long long v;
int err = consume_signed(&v, &src, options, INT8_MIN, INT8_MAX);
return err ? dfault : (int8_t)v;
}
int16_t strnum_i16(strview_t src, int16_t dfault, int options)
{
long long v;
int err = consume_signed(&v, &src, options, INT16_MIN, INT16_MAX);
return err ? dfault : (int16_t)v;
}
int32_t strnum_i32(strview_t src, int32_t dfault, int options)
{
long long v;
int err = consume_signed(&v, &src, options, INT32_MIN, INT32_MAX);
return err ? dfault : (int32_t)v;
}
int64_t strnum_i64(strview_t src, int64_t dfault, int options)
{
long long v;
int err = consume_signed(&v, &src, options, INT64_MIN, INT64_MAX);
return err ? dfault : (int64_t)v;
}
uint8_t strnum_u8(strview_t src, uint8_t dfault, int options)
{
unsigned long long v;
int err = consume_unsigned(&v, &src, options, UINT8_MAX);
return err ? dfault : (uint8_t)v;
}
uint16_t strnum_u16(strview_t src, uint16_t dfault, int options)
{
unsigned long long v;
int err = consume_unsigned(&v, &src, options, UINT16_MAX);
return err ? dfault : (uint16_t)v;
}
uint32_t strnum_u32(strview_t src, uint32_t dfault, int options)
{
unsigned long long v;
int err = consume_unsigned(&v, &src, options, UINT32_MAX);
return err ? dfault : (uint32_t)v;
}
uint64_t strnum_u64(strview_t src, uint64_t dfault, int options)
{
unsigned long long v;
int err = consume_unsigned(&v, &src, options, UINT64_MAX);
return err ? dfault : (uint64_t)v;
}
char strnum_char(strview_t src, char dfault, int options)
{
long long v;
int err = consume_signed(&v, &src, options, CHAR_MIN, CHAR_MAX);
return err ? dfault : (char)v;
}
short strnum_short(strview_t src, short dfault, int options)
{
long long v;
int err = consume_signed(&v, &src, options, SHRT_MIN, SHRT_MAX);
return err ? dfault : (short)v;
}
int strnum_int(strview_t src, int dfault, int options)
{
long long v;
int err = consume_signed(&v, &src, options, INT_MIN, INT_MAX);
return err ? dfault : (int)v;
}
long strnum_long(strview_t src, long dfault, int options)
{
long long v;
int err = consume_signed(&v, &src, options, LONG_MIN, LONG_MAX);
return err ? dfault : (long)v;
}
long long strnum_llong(strview_t src, long long dfault, int options)
{
long long v;
int err = consume_signed(&v, &src, options, LLONG_MIN, LLONG_MAX);
return err ? dfault : (long long)v;
}
unsigned char strnum_uchar(strview_t src, unsigned char dfault, int options)
{
unsigned long long v;
int err = consume_unsigned(&v, &src, options, UCHAR_MAX);
return err ? dfault : (unsigned char)v;
}
unsigned short strnum_ushort(strview_t src, unsigned short dfault, int options)
{
unsigned long long v;
int err = consume_unsigned(&v, &src, options, USHRT_MAX);
return err ? dfault : (unsigned short)v;
}
unsigned int strnum_uint(strview_t src, unsigned int dfault, int options)
{
unsigned long long v;
int err = consume_unsigned(&v, &src, options, UINT_MAX);
return err ? dfault : (unsigned int)v;
}
unsigned long strnum_ulong(strview_t src, unsigned long dfault, int options)
{
unsigned long long v;
int err = consume_unsigned(&v, &src, options, ULONG_MAX);
return err ? dfault : (unsigned long)v;
}
unsigned long long strnum_ullong(strview_t src, unsigned long long dfault, int options)
{
unsigned long long v;
int err = consume_unsigned(&v, &src, options, ULLONG_MAX);
return err ? dfault : (unsigned long long)v;
}
#ifndef STRNUM_NOFLOAT
int strnum_consume_float(float* dst, strview_t* src, int options)
{
int err = 0;
float value = 0.0;
float_components_t fc =
{
.options = options,
.num = STRVIEW_INVALID,
.is_neg = false,
.special_value = 0.0,
.integral_view = STRVIEW_INVALID,
.fractional_view = STRVIEW_INVALID,
.exp_value = 0,
.got_exponent=false
};
if(src)
{
fc.num = *src;
err = process_float_components(&fc);
};
// calculate/determine output value
if(!err && fc.special_value)
value = fc.special_value;
else if(!err)
{
value = make_float(fc.integral_view, fc.fractional_view, fc.exp_value);
if(value == INFINITY)
err = ERANGE;
};
if(!err && dst)
{
if(value != value)
*dst = NAN;
else
*dst = fc.is_neg ? -value:value;
};
if(!err)
*src = fc.num;
return err;
}
int strnum_consume_double(double* dst, strview_t* src, int options)
{
int err = 0;
double value;
float_components_t fc =
{
.options = options,
.num = STRVIEW_INVALID,
.is_neg = false,
.special_value = 0.0,
.integral_view = STRVIEW_INVALID,
.fractional_view = STRVIEW_INVALID,
.exp_value = 0,
.got_exponent=false
};
if(src)
fc.num = *src;
err = process_float_components(&fc);
value = fc.special_value;
// calculate/determine output value
if(!err && fc.special_value)
value = fc.special_value;
else if(!err)
{
value = make_double(fc.integral_view, fc.fractional_view, fc.exp_value);
if(value == INFINITY)
err = ERANGE;
};
if(!err && dst)
{
if(value != value)
*dst = NAN;
else
*dst = fc.is_neg ? -value:value;
};
if(!err)
*src = fc.num;
return err;
}
int strnum_consume_ldouble(long double* dst, strview_t* src, int options)
{
int err = 0;
long double value;
float_components_t fc =
{
.options = options,
.num = STRVIEW_INVALID,
.is_neg = false,
.special_value = 0.0,
.integral_view = STRVIEW_INVALID,
.fractional_view = STRVIEW_INVALID,
.exp_value = 0,
.got_exponent=false
};
if(src)
fc.num = *src;
err = process_float_components(&fc);
value = fc.special_value;
// calculate/determine output value
if(!err && fc.special_value)
value = fc.special_value;
else if(!err)
{
value = make_ldouble(fc.integral_view, fc.fractional_view, fc.exp_value);
if(value == INFINITY)
err = ERANGE;
};
if(!err && dst)
{
if(value != value)
*dst = NAN;
else
*dst = fc.is_neg ? -value:value;
};
if(!err)
*src = fc.num;
return err;
}
float strnum_float(strview_t src, float dfault, int options)
{
float v;
int err = strnum_value(&v, &src, options);
return err ? dfault : v;
}
double strnum_double(strview_t src, double dfault, int options)
{
double v;
int err = strnum_value(&v, &src, options);
return err ? dfault : v;
}
long double strnum_ldouble(strview_t src, long double dfault, int options)
{
long double v;
int err = strnum_value(&v, &src, options);
return err ? dfault : v;
}
#endif
//********************************************************************************************************
// Private functions
//********************************************************************************************************
#ifndef STRNUM_NOFLOAT
static int process_float_components(float_components_t* fc)
{
int err = 0;
fc->integral_view = STRVIEW_INVALID;
fc->fractional_view = STRVIEW_INVALID;
if(!strview_is_valid(fc->num))
err = EINVAL;
//consume whitespace
if(!err && !(fc->options & STRNUM_NOSPACE))
fc->num = strview_trim_start(fc->num, " ");
//consume sign
if(!err && !(fc->options & STRNUM_NOSIGN))
fc->is_neg = consume_sign(&fc->num);
//consume special cases inf and nan
if(!err)
fc->special_value = consume_float_special(fc);
//consume views of integral and fractional digits (but don't convert them yet)
if(!err && !fc->special_value)
{
fc->integral_view = split_digits(&fc->num);
if(fc->num.size && fc->num.data[0]=='.')
{
strview_pop_first_char(&fc->num);
fc->fractional_view = split_digits(&fc->num);
};
if(!strview_is_valid(fc->integral_view) && !strview_is_valid(fc->fractional_view))
err = EINVAL;
};
//consume exponent
if(!err && !fc->special_value && !(fc->options & STRNUM_NOEXP) && strview_starts_with_nocase(fc->num, "E"))
err = consume_exponent(fc);
return err;
}
static float consume_float_special(float_components_t* fc)
{
float value = 0.0;
if(strview_starts_with_nocase(fc->num, "infinty"))
{
value = INFINITY;
fc->num = strview_sub(fc->num, strlen("infinity"), INT_MAX);
}
else if(strview_starts_with_nocase(fc->num, "inf"))
{
value = INFINITY;
fc->num = strview_sub(fc->num, strlen("inf"), INT_MAX);
}
else if(strview_starts_with_nocase(fc->num, "nan"))
{
value = NAN;
fc->num = strview_sub(fc->num, strlen("nan"), INT_MAX);
};
return value;
}
static int consume_exponent(float_components_t* fc)
{
int err = 0;
strview_t exp_view;
exp_view = strview_sub(fc->num, 1, INT_MAX);
err = strnum_consume_int(&fc->exp_value, &exp_view, STRNUM_NOSPACE | STRNUM_NOBX);
fc->got_exponent = (err == 0);
if(fc->got_exponent)
fc->num = exp_view;
else if(err == EINVAL)
err = 0; // an invalid exponent (non-numeric) simply means we don't consume the exponent.
return err;
}
#define make_float_type(typ, powfunc) \
typ result = 0.0; \
typ fractional_part; \
unsigned long long ull; \
int fractional_power = 0; \
while(integral_digits.size) \
{ \
consume_decimal_digits(&ull, &integral_digits); \
result += (typ)ull; \
result *= powfunc(10, integral_digits.size); \
}; \
fractional_digits = strview_trim_end(fractional_digits, "0"); \
while(fractional_digits.size) \
{ \
fractional_power -= fractional_digits.size; \
consume_decimal_digits(&ull, &fractional_digits); \
fractional_power += fractional_digits.size; \
fractional_part = (typ)ull; \
fractional_part *= powfunc(10, fractional_power); \
result += fractional_part; \
}; \
result *= powfunc(10, exp_value); \
return result;
static float make_float(strview_t integral_digits, strview_t fractional_digits, int exp_value)
{
make_float_type(float, powf)
}
static double make_double(strview_t integral_digits, strview_t fractional_digits, int exp_value)
{
make_float_type(double, pow)
}
static long double make_ldouble(strview_t integral_digits, strview_t fractional_digits, int exp_value)
{
make_float_type(long double, powl)
}
#endif
static int consume_signed(long long* dst, strview_t* src, int options, long long limit_min, long long limit_max)
{
int err = 0;
bool is_neg = false;
strview_t num = STRVIEW_INVALID;
int base = 10;
unsigned long long val_ull;
long long val_ll;
if(options & STRNUM_BASE_BIN)
base = 2;
else if(options & STRNUM_BASE_HEX)
base = 16;
if(src)
num = *src;
if(!strview_is_valid(num))
err = EINVAL;
//consume whitespace
if(!err && !(options & STRNUM_NOSPACE))
num = strview_trim_start(num, " ");
//consume sign
if(!err && !(options & STRNUM_NOSIGN))
is_neg = consume_sign(&num);
//consume base prefix and digits
if(!err)
{
consume_base_prefix(&base, &num, options);
err = consume_digits(&val_ull, &num, base);
};
//check if over range for signed ll before applying sign
if(!err)
{
if(is_neg)
{
if(val_ull > (unsigned long long)LLONG_MIN)
err = ERANGE;
else
val_ll = val_ull * -1;
}
else if(val_ull > LLONG_MAX)
err = ERANGE;
else
val_ll = val_ull;
};
//check if within range of destination type
if(!err && !(limit_min <= val_ll && val_ll <= limit_max))
err = ERANGE;
//provide output
if(!err)
{
if(dst)
*dst = val_ll;
if(src)
*src = num;
};
return err;
}
static int consume_unsigned(unsigned long long* dst, strview_t* src, int options, unsigned long long limit_max)
{
int err = 0;
strview_t num = STRVIEW_INVALID;
int base = 10;
unsigned long long val_ull;
if(options & STRNUM_BASE_BIN)
base = 2;
else if(options & STRNUM_BASE_HEX)
base = 16;
if(src)
num = *src;
if(!strview_is_valid(num))
err = EINVAL;
//consume whitespace
if(!err && !(options & STRNUM_NOSPACE))
num = strview_trim_start(num, " ");
//consume base prefix and digits
if(!err)
{
consume_base_prefix(&base, &num, options);
err = consume_digits(&val_ull, &num, base);
};
//check if within range of destination type
if(!err && val_ull > limit_max)
err = ERANGE;
//provide output
if(!err)
{
if(dst)
*dst = val_ull;
if(src)
*src = num;
};
return err;
}
static void consume_base_prefix(int* base, strview_t* src, int options)
{
strview_t base_prefix = strview_sub(*src, 0, BASE_PREFIX_LEN);
if(!(options & STRNUM_NOBX))
{
if(!(options & STRNUM_BASE_HEX) && strview_is_match_nocase(base_prefix, "0b"))
{
*src = strview_sub(*src, BASE_PREFIX_LEN, INT_MAX);
*base = 2;
}
else if(!(options & STRNUM_BASE_BIN) && strview_is_match_nocase(base_prefix, "0x"))
{
*src = strview_sub(*src, BASE_PREFIX_LEN, INT_MAX);
*base = 16;
};
};
}
static bool consume_sign(strview_t* num)
{
bool is_neg = false;
if(num->size && num->data[0] == '-')
{
is_neg = true;
strview_pop_first_char(num);
}
else if(num->size && num->data[0] == '+')
strview_pop_first_char(num);
return is_neg;
}
// pop digits from source, until a non-digit is found, or the next digit would overflow an unsigned long long
// return value is ERANGE if conversion stopped to prevent overflow, or EINVAL if no digits were found
// dst is always written to with a value representing the digits converted, or 0 if no digits exist (EINVAL)
static int consume_digits(unsigned long long* dst, strview_t *src, int base)
{
int err;
if(base == 10)
err = consume_decimal_digits(dst, src);
else if(base == 16)
err = consume_hex_digits(dst, src);
else
err = consume_bin_digits(dst, src);
return err;
}
static int consume_decimal_digits(unsigned long long* dst, strview_t* str)
{
#define UI_LIMIT ((unsigned int)(UINT_MAX/10-9))
#define UL_LIMIT ((unsigned long)(ULONG_MAX/10-9))
#define ULL_LIMIT ((unsigned long long)(ULLONG_MAX/10-9))
int err;
unsigned int res_ui = 0;
unsigned long res_ul = 0;
unsigned long long res_ull = 0;
unsigned long long post_add;
unsigned long long next_weight;
if(str->size && isdigit(str->data[0]))
err = 0;
else
err = EINVAL;
if(!err)
{
*str = strview_trim_start(*str, "0");
while(str->size && isdigit(str->data[0]) && res_ui < UI_LIMIT)
{
res_ui *= 10;
res_ui += strview_pop_first_char(str) & 0x0F;
};
res_ul = res_ui;
while(str->size && isdigit(str->data[0]) && res_ul < UL_LIMIT)
{
res_ul *= 10;
res_ul += strview_pop_first_char(str) & 0x0F;
};
res_ull = res_ul;
while(str->size && isdigit(str->data[0]) && !err)
{
if(res_ull > ULLONG_MAX/10) // if number is too big to multiply by 10, ERANGE
err = ERANGE;
else
{
next_weight = res_ull * 10;
post_add = next_weight + (str->data[0] & 0x0F);
if(post_add < next_weight) // if digit addition results in a lower value, ERANGE
err = ERANGE;
else
{ // else digit is consumed, and value is valid
strview_pop_first_char(str);
res_ull = post_add;
};
};
};
};
if(dst)
*dst = res_ull;
#undef UI_LIMIT
#undef UL_LIMIT
#undef ULL_LIMIT
return err;
}
static int consume_hex_digits(unsigned long long* dst, strview_t* str)
{
unsigned long long result = 0;
int err;
if(str->size && isxdigit(str->data[0]))
{
err = 0;
*str = strview_trim_start(*str, "0");
}
else
err = EINVAL;
while(str->size && isxdigit(str->data[0]) && !err)
{
if(!upper_nibble_ull_is_zero(result))
err = ERANGE;
else
{
result <<= 4;
result += xdigit_value(strview_pop_first_char(str));
};
};
if(dst)
*dst = result;
return err;
}
static int consume_bin_digits(unsigned long long* dst, strview_t* str)
{
unsigned long long result = 0;
int err;
if(str->size && isbdigit(str->data[0]))
{
err = 0;
*str = strview_trim_start(*str, "0");
}
else
err = EINVAL;
while(str->size && isbdigit(str->data[0]) && !err)
{
if(!upper_bit_ull_is_zero(result))
err = ERANGE;
else
{
result <<= 1;
result |= strview_pop_first_char(str) & 1;
};
};
if(dst)
*dst = result;
return err;
}
static bool upper_nibble_ull_is_zero(unsigned long long i)
{
return !(i & (0x0Full << (sizeof(unsigned long long)*8-4)));
}
static bool upper_bit_ull_is_zero(unsigned long long i)
{
return !(i & (0x01ull << (sizeof(unsigned long long)*8-1)));
}
static int xdigit_value(char c)
{
c &= 0x5F;
if(c & 0x40)
c += 9;
return c & 0x0F;
}
static bool isbdigit(char c)
{
return (c & 0x7E) == 0x30;
}
static strview_t split_digits(strview_t* src)
{
strview_t result = STRVIEW_INVALID;
if(src && src->size && isdigit(src->data[0]))
{
result.data = src->data;
result.size = 0;
while(src->size && isdigit(src->data[0]))
{
strview_pop_first_char(src);
result.size++;
};
};
return result;
}