#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
#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
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
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