[sr-dev] git:master:80c906d5: core: ut - functions to convert from str to long

Daniel-Constantin Mierla miconda at gmail.com
Mon May 3 08:00:54 CEST 2021


Module: kamailio
Branch: master
Commit: 80c906d5418ece5e4a3e993cc632a0e6ae3d24be
URL: https://github.com/kamailio/kamailio/commit/80c906d5418ece5e4a3e993cc632a0e6ae3d24be

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2021-05-03T08:00:32+02:00

core: ut - functions to convert from str to long

---

Modified: src/core/ut.h

---

Diff:  https://github.com/kamailio/kamailio/commit/80c906d5418ece5e4a3e993cc632a0e6ae3d24be.diff
Patch: https://github.com/kamailio/kamailio/commit/80c906d5418ece5e4a3e993cc632a0e6ae3d24be.patch

---

diff --git a/src/core/ut.h b/src/core/ut.h
index 4ab08024a7..61ff696e13 100644
--- a/src/core/ut.h
+++ b/src/core/ut.h
@@ -628,69 +628,88 @@ static inline void strlower(str* _s)
 }
 
 
+#define str2unval(_s, _r) do { \
+		int i; \
+		if (_r == NULL) return -1; \
+		*_r = 0; \
+		if (_s == NULL) return -1; \
+		if (_s->len < 0) return -1; \
+		if (_s->s == NULL) return -1; \
+		for(i = 0; i < _s->len; i++) { \
+			if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) { \
+				*_r *= 10; \
+				*_r += _s->s[i] - '0'; \
+			} else { \
+				return -1; \
+			} \
+		} \
+		return 0; \
+	} while(0)
+
 /*
- * Convert a str into integer
+ * Convert an str to unsigned long
  */
-static inline int str2int(str* _s, unsigned int* _r)
+static inline int str2ulong(str* _s, unsigned long* _r)
 {
-	int i;
+	str2unval(_s, _r);
+}
 
-	if (_r == NULL) return -1;
-	*_r = 0;
-	if (_s == NULL) return -1;
-	if (_s->len < 0) return -1;
-	if (_s->s == NULL) return -1;
+/*
+ * Convert an str to unsigned integer
+ */
+static inline int str2int(str* _s, unsigned int* _r)
+{
+	str2unval(_s, _r);
+}
 
-	for(i = 0; i < _s->len; i++) {
-		if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) {
-			*_r *= 10;
-			*_r += _s->s[i] - '0';
-		} else {
-			return -1;
-		}
-	}
+#define str2snval(_s, _r) do { \
+		int i; \
+		int sign; \
+		if (_s == NULL) return -1; \
+		if (_r == NULL) return -1; \
+		if (_s->len < 0) return -1; \
+		if (_s->s == NULL) return -1; \
+		*_r = 0; \
+		sign = 1; \
+		i = 0; \
+		if (_s->s[0] == '+') { \
+			i++; \
+		} else if (_s->s[0] == '-') { \
+			sign = -1; \
+			i++; \
+		} \
+		for(; i < _s->len; i++) { \
+			if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) { \
+				*_r *= 10; \
+				*_r += _s->s[i] - '0'; \
+			} else { \
+				return -1; \
+			} \
+		} \
+		*_r *= sign; \
+		return 0; \
+	} while(0)
 
-	return 0;
+/*
+ * Convert an str to signed long
+ */
+static inline int str2slong(str* _s, long* _r)
+{
+	str2snval(_s, _r);
 }
 
+
 /*
  * Convert an str to signed integer
  */
 static inline int str2sint(str* _s, int* _r)
 {
-	int i;
-	int sign;
-
-	if (_s == NULL) return -1;
-	if (_r == NULL) return -1;
-	if (_s->len < 0) return -1;
-	if (_s->s == NULL) return -1;
-
-	*_r = 0;
-	sign = 1;
-	i = 0;
-	if (_s->s[0] == '+') {
-		i++;
-	} else if (_s->s[0] == '-') {
-		sign = -1;
-		i++;
-	}
-	for(; i < _s->len; i++) {
-		if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) {
-			*_r *= 10;
-			*_r += _s->s[i] - '0';
-		} else {
-			return -1;
-		}
-	}
-	*_r *= sign;
-
-	return 0;
+	str2snval(_s, _r);
 }
 
 
 /*
- * Convert a str into integer
+ * Convert an strz to integer
  */
 static inline int strz2int(char* _s, unsigned int* _r)
 {
@@ -713,7 +732,7 @@ static inline int strz2int(char* _s, unsigned int* _r)
 }
 
 /*
- * Convert an str to signed integer
+ * Convert an strz to signed integer
  */
 static inline int strz2sint(char* _s, int* _r)
 {




More information about the sr-dev mailing list