Ambroz Bizjak 11 лет назад
Родитель
Сommit
bb2c521ac2

+ 2 - 2
examples/parse_number_test.c

@@ -54,7 +54,7 @@ static void test_random (int num_digits, int digit_modulo)
     int std_res = !*endptr && !(std_num == UINTMAX_MAX && errno == ERANGE);
     
     uintmax_t num = 0;
-    int res = parse_unsigned_integer_bin((const char *)digits, num_digits, &num);
+    int res = parse_unsigned_integer(MemRef_Make((const char *)digits, num_digits), &num);
     
     if (res != std_res) {
         printf("fail1 %s\n", (const char *)digits);
@@ -85,7 +85,7 @@ static void test_value (uintmax_t x)
     char str[40];
     sprintf(str, "%" PRIuMAX, x);
     uintmax_t y;
-    int res = parse_unsigned_integer_bin(str, strlen(str), &y);
+    int res = parse_unsigned_integer(MemRef_MakeCstr(str), &y);
     ASSERT_FORCE(res);
     ASSERT_FORCE(y == x);
     

+ 2 - 2
misc/ipaddr.h

@@ -77,7 +77,7 @@ int ipaddr_parse_ipv4_addr (MemRef name, uint32_t *out_addr)
         }
         
         uintmax_t d;
-        if (!parse_unsigned_integer_bin(name.ptr, j, &d)) {
+        if (!parse_unsigned_integer(MemRef_SubTo(name, j), &d)) {
             return 0;
         }
         
@@ -99,7 +99,7 @@ int ipaddr_parse_ipv4_addr (MemRef name, uint32_t *out_addr)
 int ipaddr_parse_ipv4_prefix (MemRef str, int *num)
 {
     uintmax_t d;
-    if (!parse_unsigned_integer_bin(str.ptr, str.len, &d)) {
+    if (!parse_unsigned_integer(str, &d)) {
         return 0;
     }
     if (d > 32) {

+ 1 - 1
misc/ipaddr6.h

@@ -207,7 +207,7 @@ ipv4_ending:
 int ipaddr6_parse_ipv6_prefix (MemRef str, int *out_num)
 {
     uintmax_t d;
-    if (!parse_unsigned_integer_bin(str.ptr, str.len, &d)) {
+    if (!parse_unsigned_integer(str, &d)) {
         return 0;
     }
     if (d > 128) {

+ 24 - 41
misc/parse_number.h

@@ -39,17 +39,15 @@
 #include <stddef.h>
 #include <limits.h>
 
+#include <misc/memref.h>
 #include <misc/debug.h>
 
 // public parsing functions
 static int decode_decimal_digit (char c);
 static int decode_hex_digit (char c);
-static int parse_unsigned_integer_bin (const char *str, size_t str_len, uintmax_t *out) WARN_UNUSED;
-static int parse_unsigned_integer (const char *str, uintmax_t *out) WARN_UNUSED;
-static int parse_unsigned_hex_integer_bin (const char *str, size_t str_len, uintmax_t *out) WARN_UNUSED;
-static int parse_unsigned_hex_integer (const char *str, uintmax_t *out) WARN_UNUSED;
-static int parse_signmag_integer_bin (const char *str, size_t str_len, int *out_sign, uintmax_t *out_mag) WARN_UNUSED;
-static int parse_signmag_integer (const char *str, int *out_sign, uintmax_t *out_mag) WARN_UNUSED;
+static int parse_unsigned_integer (MemRef str, uintmax_t *out) WARN_UNUSED;
+static int parse_unsigned_hex_integer (MemRef str, uintmax_t *out) WARN_UNUSED;
+static int parse_signmag_integer (MemRef str, int *out_sign, uintmax_t *out_mag) WARN_UNUSED;
 
 // public generation functions
 static int compute_decimal_repr_size (uintmax_t x);
@@ -125,44 +123,39 @@ static int parse__no_overflow (const char *str, size_t str_len, uintmax_t *out)
     return 1;
 }
 
-int parse_unsigned_integer_bin (const char *str, size_t str_len, uintmax_t *out)
+int parse_unsigned_integer (MemRef str, uintmax_t *out)
 {
     // we do not allow empty strings
-    if (str_len == 0) {
+    if (str.len == 0) {
         return 0;
     }
     
     // remove leading zeros
-    while (str_len > 0 && *str == '0') {
-        str++;
-        str_len--;
+    while (str.len > 0 && *str.ptr == '0') {
+        str.ptr++;
+        str.len--;
     }
     
     // detect overflow
-    if (str_len > sizeof(parse_number__uintmax_max_str) - 1 ||
-        (str_len == sizeof(parse_number__uintmax_max_str) - 1 && memcmp(str, parse_number__uintmax_max_str, sizeof(parse_number__uintmax_max_str) - 1) > 0)) {
+    if (str.len > sizeof(parse_number__uintmax_max_str) - 1 ||
+        (str.len == sizeof(parse_number__uintmax_max_str) - 1 && memcmp(str.ptr, parse_number__uintmax_max_str, sizeof(parse_number__uintmax_max_str) - 1) > 0)) {
         return 0;
     }
     
     // will not overflow (but can still have invalid characters)
-    return parse__no_overflow(str, str_len, out);
-}
-
-int parse_unsigned_integer (const char *str, uintmax_t *out)
-{
-    return parse_unsigned_integer_bin(str, strlen(str), out);
+    return parse__no_overflow(str.ptr, str.len, out);
 }
 
-int parse_unsigned_hex_integer_bin (const char *str, size_t str_len, uintmax_t *out)
+int parse_unsigned_hex_integer (MemRef str, uintmax_t *out)
 {
     uintmax_t n = 0;
     
-    if (str_len == 0) {
+    if (str.len == 0) {
         return 0;
     }
     
-    while (str_len > 0) {
-        int digit = decode_hex_digit(*str);
+    while (str.len > 0) {
+        int digit = decode_hex_digit(*str.ptr);
         if (digit < 0) {
             return 0;
         }
@@ -177,29 +170,24 @@ int parse_unsigned_hex_integer_bin (const char *str, size_t str_len, uintmax_t *
         }
         n += digit;
         
-        str++;
-        str_len--;
+        str.ptr++;
+        str.len--;
     }
     
     *out = n;
     return 1;
 }
 
-int parse_unsigned_hex_integer (const char *str, uintmax_t *out)
-{
-    return parse_unsigned_hex_integer_bin(str, strlen(str), out);
-}
-
-int parse_signmag_integer_bin (const char *str, size_t str_len, int *out_sign, uintmax_t *out_mag)
+int parse_signmag_integer (MemRef str, int *out_sign, uintmax_t *out_mag)
 {
     int sign = 1;
-    if (str_len > 0 && (str[0] == '+' || str[0] == '-')) {
-        sign = 1 - 2 * (str[0] == '-');
-        str++;
-        str_len--;
+    if (str.len > 0 && (str.ptr[0] == '+' || str.ptr[0] == '-')) {
+        sign = 1 - 2 * (str.ptr[0] == '-');
+        str.ptr++;
+        str.len--;
     }
     
-    if (!parse_unsigned_integer_bin(str, str_len, out_mag)) {
+    if (!parse_unsigned_integer(str, out_mag)) {
         return 0;
     }
     
@@ -207,11 +195,6 @@ int parse_signmag_integer_bin (const char *str, size_t str_len, int *out_sign, u
     return 1;
 }
 
-int parse_signmag_integer (const char *str, int *out_sign, uintmax_t *out_mag)
-{
-    return parse_signmag_integer_bin(str, strlen(str), out_sign, out_mag);
-}
-
 int compute_decimal_repr_size (uintmax_t x)
 {
     int size = 0;

+ 1 - 1
ncd/NCDConfigTokenizer.c

@@ -236,7 +236,7 @@ void NCDConfigTokenizer_Tokenize (char *str, size_t left, NCDConfigTokenizer_out
                             }
                             
                             uintmax_t hex_val;
-                            if (!parse_unsigned_hex_integer_bin(&str[l + 2], 2, &hex_val)) {
+                            if (!parse_unsigned_hex_integer(MemRef_Make(&str[l + 2], 2), &hex_val)) {
                                 BLog(BLOG_ERROR, "hexadecimal escape found in string but two hex characters don't follow");
                                 goto string_fail1;
                             }

+ 1 - 1
ncd/extra/value_utils.c

@@ -86,7 +86,7 @@ int ncd_read_uintmax (NCDValRef val, uintmax_t *out)
         return 0;
     }
     
-    return parse_unsigned_integer_bin(NCDVal_StringData(val), NCDVal_StringLength(val), out);
+    return parse_unsigned_integer(NCDVal_StringMemRef(val), out);
 }
 
 int ncd_read_time (NCDValRef val, btime_t *out)

+ 1 - 2
ncd/modules/file_open.c

@@ -442,8 +442,7 @@ static void seek_func_new (void *unused, NCDModuleInst *i, const struct NCDModul
     // parse position
     int position_sign;
     uintmax_t position_mag;
-    MemRef position_mr = NCDVal_StringMemRef(position_arg);
-    if (!parse_signmag_integer_bin(position_mr.ptr, position_mr.len, &position_sign, &position_mag)) {
+    if (!parse_signmag_integer(NCDVal_StringMemRef(position_arg), &position_sign, &position_mag)) {
         ModuleLog(i, BLOG_ERROR, "wrong position");
         goto fail0;
     }

+ 1 - 1
ncd/modules/net_backend_waitdevice.c

@@ -79,7 +79,7 @@ static void client_handler (struct instance *o, char *devpath, int have_map, BSt
         const char *ifindex_str = BStringMap_Get(cache_map, "IFINDEX");
         
         uintmax_t ifindex;
-        if (!(!match_res && interface && strlen(interface) == o->ifname.len && !memcmp(interface, o->ifname.ptr, o->ifname.len) && ifindex_str && parse_unsigned_integer(ifindex_str, &ifindex))) {
+        if (!(!match_res && interface && strlen(interface) == o->ifname.len && !memcmp(interface, o->ifname.ptr, o->ifname.len) && ifindex_str && parse_unsigned_integer(MemRef_MakeCstr(ifindex_str), &ifindex))) {
             goto out;
         }
         

+ 1 - 1
ncd/modules/net_watch_interfaces.c

@@ -312,7 +312,7 @@ static void client_handler (struct instance *o, char *devpath, int have_map, BSt
     const char *ifindex_str = BStringMap_Get(cache_map, "IFINDEX");
     
     uintmax_t ifindex;
-    if (!(!match_res && interface && ifindex_str && parse_unsigned_integer(ifindex_str, &ifindex))) {
+    if (!(!match_res && interface && ifindex_str && parse_unsigned_integer(MemRef_MakeCstr(ifindex_str), &ifindex))) {
         if (ex_device) {
             remove_device(o, ex_device);
         }

+ 1 - 1
ncd/modules/parse.c

@@ -92,7 +92,7 @@ typedef int (*parse_func) (NCDModuleInst *i, const char *str, size_t str_len, NC
 static int parse_number (NCDModuleInst *i, const char *str, size_t str_len, NCDValMem *mem, NCDValRef *out)
 {
     uintmax_t n;
-    if (!parse_unsigned_integer_bin(str, str_len, &n)) {
+    if (!parse_unsigned_integer(MemRef_Make(str, str_len), &n)) {
         ModuleLog(i, BLOG_ERROR, "failed to parse number");
         return 0;
     }

+ 1 - 1
ncd/modules/regex_match.c

@@ -161,7 +161,7 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     
     size_t pos;
     uintmax_t n;
-    if ((pos = string_begins_with(name, "match")) && parse_unsigned_integer(name + pos, &n)) {
+    if ((pos = string_begins_with(name, "match")) && parse_unsigned_integer(MemRef_MakeCstr(name + pos), &n)) {
         if (o->succeeded && n < MAX_MATCHES && o->matches[n].rm_so >= 0) {
             regmatch_t *m = &o->matches[n];
             

+ 2 - 2
ncd/modules/sys_watch_usb.c

@@ -283,8 +283,8 @@ static void client_handler (struct instance *o, char *devpath, int have_map, BSt
     if (!(subsystem && !strcmp(subsystem, "usb") &&
           devname &&
           devtype && !strcmp(devtype, "usb_device") &&
-          vendor_id_str && parse_unsigned_hex_integer(vendor_id_str, &vendor_id) &&
-          model_id_str && parse_unsigned_hex_integer(model_id_str, &model_id)
+          vendor_id_str && parse_unsigned_hex_integer(MemRef_MakeCstr(vendor_id_str), &vendor_id) &&
+          model_id_str && parse_unsigned_hex_integer(MemRef_MakeCstr(model_id_str), &model_id)
     )) {
         if (ex_device) {
             remove_device(o, ex_device);