]> www.git.momoyon.org Git - commonlib.git/commitdiff
[commonlib.h] Improve sv_to_<NUMBER_TYPE> funcs
authorahmedsamyh <ahmedsamyh10@gmail.com>
Tue, 8 Apr 2025 23:50:24 +0000 (04:50 +0500)
committerahmedsamyh <ahmedsamyh10@gmail.com>
Tue, 8 Apr 2025 23:50:24 +0000 (04:50 +0500)
commonlib.h

index 4b597ee2c90772dfc5a2ab3dcc3e9688af7085ac..326fd569179f40880abfa940d97b857c7328a2bd 100644 (file)
@@ -8,6 +8,7 @@
 #include <string.h>\r
 #include <ctype.h>\r
 #include <assert.h>\r
+#include <limits.h>\r
 \r
 // Remove Prefix\r
 #ifdef COMMONLIB_REMOVE_PREFIX\r
@@ -66,7 +67,7 @@
 #define sv_trim c_sv_trim\r
 #define sv_to_cstr c_sv_to_cstr\r
 #define sv_to_int c_sv_to_int\r
-#define sv_to_uint64 c_sv_to_uint64\r
+#define sv_to_uint c_sv_to_uint\r
 #define sv_to_uint8_hex c_sv_to_uint8_hex\r
 #define sv_to_ptr c_sv_to_ptr\r
 #define sv_to_float c_sv_to_float\r
@@ -281,11 +282,10 @@ void c_sv_ltrim(c_String_view* sv);
 void c_sv_rtrim(c_String_view* sv);\r
 void c_sv_trim(c_String_view* sv);\r
 char* c_sv_to_cstr(c_String_view sv);\r
-int32 c_sv_to_int(c_String_view sv);\r
-uint64 c_sv_to_uint64(c_String_view sv);\r
-uint8 c_sv_to_uint8_hex(c_String_view sv);\r
-void* c_sv_to_ptr(c_String_view sv);\r
-float32 c_sv_to_float(c_String_view sv);\r
+int64 c_sv_to_int(c_String_view sv, int *count, int base);\r
+uint64 c_sv_to_uint(c_String_view sv, int *count, int base);\r
+void* c_sv_to_ptr(c_String_view sv, int *count, int base);\r
+float64 c_sv_to_float(c_String_view sv, int *count);\r
 bool c_sv_contains_char(c_String_view sv, char ch);\r
 bool c_sv_is_hex_numbers(c_String_view sv);\r
 bool c_sv_equals(c_String_view sv1, c_String_view sv2);\r
@@ -615,37 +615,59 @@ char* c_sv_to_cstr(c_String_view sv){
     return res;\r
 }\r
 \r
-// TODO: check for failure of conversion. returns 0/0.0 on failure, so just check if the str contains zero.\r
-int32 c_sv_to_int(c_String_view sv) {\r
-    char* str = c_sv_to_cstr(sv);\r
-    int32 res = atoi(str);\r
-    C_FREE(str);\r
-    return res;\r
-}\r
+int64 c_sv_to_int(c_String_view sv, int *count_out, int base) {\r
+    char *str = c_sv_to_cstr(sv);\r
+\r
+    char *endptr = NULL;\r
+\r
+    int64 res = strtol(str, &endptr, base);\r
+\r
+    if (endptr == str) {\r
+        *count_out = -1;\r
+        return res;\r
+    }\r
+\r
+    *count_out = (int)(endptr - str);\r
 \r
-uint64 c_sv_to_uint64(c_String_view sv) {\r
-    char* str = c_sv_to_cstr(sv);\r
-    uint64 res = (uint64)atoll(str);\r
     C_FREE(str);\r
     return res;\r
 }\r
 \r
-uint8 c_sv_to_uint8_hex(c_String_view sv) {\r
+uint64 c_sv_to_uint(c_String_view sv, int *count, int base) {\r
     char* str = c_sv_to_cstr(sv);\r
-    char* end = str + sv.count;\r
-    uint8 res = (uint8)strtol(str, &end, 16);\r
+\r
+    char *endptr = NULL;\r
+    uint64 res = strtoul(str, &endptr, base);\r
+\r
+    if (endptr == str) {\r
+        *count = -1;\r
+        return res;\r
+    }\r
+\r
+    *count = (int)(endptr - str);\r
+\r
     C_FREE(str);\r
     return res;\r
 }\r
 \r
-float32 c_sv_to_float(c_String_view sv) {\r
+float64 c_sv_to_float(c_String_view sv, int *count) {\r
     char* str = c_sv_to_cstr(sv);\r
-    float32 res = (float32)atof(str);\r
+\r
+    char *endptr = NULL;\r
+    float64 res = strtod(str, &endptr);\r
+\r
+    if (endptr == str) {\r
+        *count = -1;\r
+        return res;\r
+    }\r
+\r
+    *count = (int)(endptr - str);\r
+\r
     C_FREE(str);\r
     return res;\r
 }\r
 \r
-void*    c_sv_to_ptr(c_String_view sv) {\r
+void* c_sv_to_ptr(c_String_view sv, int *count, int base) {\r
     char* str = c_sv_to_cstr(sv);\r
     char* end = NULL;\r
     void* res = (void*)strtoull(str, &end, 16);\r