Browse code

core: ut - functions to convert from str to long

Daniel-Constantin Mierla authored on 02/05/2021 15:32:28
Showing 1 changed files
... ...
@@ -628,69 +628,88 @@ static inline void strlower(str* _s)
628 628
 }
629 629
 
630 630
 
631
+#define str2unval(_s, _r) do { \
632
+		int i; \
633
+		if (_r == NULL) return -1; \
634
+		*_r = 0; \
635
+		if (_s == NULL) return -1; \
636
+		if (_s->len < 0) return -1; \
637
+		if (_s->s == NULL) return -1; \
638
+		for(i = 0; i < _s->len; i++) { \
639
+			if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) { \
640
+				*_r *= 10; \
641
+				*_r += _s->s[i] - '0'; \
642
+			} else { \
643
+				return -1; \
644
+			} \
645
+		} \
646
+		return 0; \
647
+	} while(0)
648
+
631 649
 /*
632
- * Convert a str into integer
650
+ * Convert an str to unsigned long
633 651
  */
634
-static inline int str2int(str* _s, unsigned int* _r)
652
+static inline int str2ulong(str* _s, unsigned long* _r)
635 653
 {
636
-	int i;
654
+	str2unval(_s, _r);
655
+}
637 656
 
638
-	if (_r == NULL) return -1;
639
-	*_r = 0;
640
-	if (_s == NULL) return -1;
641
-	if (_s->len < 0) return -1;
642
-	if (_s->s == NULL) return -1;
657
+/*
658
+ * Convert an str to unsigned integer
659
+ */
660
+static inline int str2int(str* _s, unsigned int* _r)
661
+{
662
+	str2unval(_s, _r);
663
+}
643 664
 
644
-	for(i = 0; i < _s->len; i++) {
645
-		if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) {
646
-			*_r *= 10;
647
-			*_r += _s->s[i] - '0';
648
-		} else {
649
-			return -1;
650
-		}
651
-	}
665
+#define str2snval(_s, _r) do { \
666
+		int i; \
667
+		int sign; \
668
+		if (_s == NULL) return -1; \
669
+		if (_r == NULL) return -1; \
670
+		if (_s->len < 0) return -1; \
671
+		if (_s->s == NULL) return -1; \
672
+		*_r = 0; \
673
+		sign = 1; \
674
+		i = 0; \
675
+		if (_s->s[0] == '+') { \
676
+			i++; \
677
+		} else if (_s->s[0] == '-') { \
678
+			sign = -1; \
679
+			i++; \
680
+		} \
681
+		for(; i < _s->len; i++) { \
682
+			if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) { \
683
+				*_r *= 10; \
684
+				*_r += _s->s[i] - '0'; \
685
+			} else { \
686
+				return -1; \
687
+			} \
688
+		} \
689
+		*_r *= sign; \
690
+		return 0; \
691
+	} while(0)
652 692
 
653
-	return 0;
693
+/*
694
+ * Convert an str to signed long
695
+ */
696
+static inline int str2slong(str* _s, long* _r)
697
+{
698
+	str2snval(_s, _r);
654 699
 }
655 700
 
701
+
656 702
 /*
657 703
  * Convert an str to signed integer
658 704
  */
659 705
 static inline int str2sint(str* _s, int* _r)
660 706
 {
661
-	int i;
662
-	int sign;
663
-
664
-	if (_s == NULL) return -1;
665
-	if (_r == NULL) return -1;
666
-	if (_s->len < 0) return -1;
667
-	if (_s->s == NULL) return -1;
668
-
669
-	*_r = 0;
670
-	sign = 1;
671
-	i = 0;
672
-	if (_s->s[0] == '+') {
673
-		i++;
674
-	} else if (_s->s[0] == '-') {
675
-		sign = -1;
676
-		i++;
677
-	}
678
-	for(; i < _s->len; i++) {
679
-		if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) {
680
-			*_r *= 10;
681
-			*_r += _s->s[i] - '0';
682
-		} else {
683
-			return -1;
684
-		}
685
-	}
686
-	*_r *= sign;
687
-
688
-	return 0;
707
+	str2snval(_s, _r);
689 708
 }
690 709
 
691 710
 
692 711
 /*
693
- * Convert a str into integer
712
+ * Convert an strz to integer
694 713
  */
695 714
 static inline int strz2int(char* _s, unsigned int* _r)
696 715
 {
... ...
@@ -713,7 +732,7 @@ static inline int strz2int(char* _s, unsigned int* _r)
713 732
 }
714 733
 
715 734
 /*
716
- * Convert an str to signed integer
735
+ * Convert an strz to signed integer
717 736
  */
718 737
 static inline int strz2sint(char* _s, int* _r)
719 738
 {