This is an automated email from Gerrit.

"Antonio Borneo <borneo.anto...@gmail.com>" just uploaded a new patch set to 
Gerrit, which you can find at https://review.openocd.org/c/openocd/+/8394

-- gerrit

commit 94c7849b2dfac78cfd50195af94d3d1046854c96
Author: Antonio Borneo <borneo.anto...@gmail.com>
Date:   Sun Jul 14 12:09:15 2024 +0200

    binarybuffer: simplify the prototype of str_to_buf()
    
    With 'radix' always zero and '_detected_radix' always NULL, drop
    the two parameters and simplify str_to_buf().
    
    While there:
    - drop some redundant assert(),
    - drop the re-check for the base prefix,
    - simplify str_strip_number_prefix_if_present() and rename it, as
      the prefix MUST be present,
    - fix a minor typo,
    - update the doxygen description of str_to_buf().
    
    Change-Id: I1abdc8ec0587b23881953d3094101c04d5bb1c58
    Signed-off-by: Antonio Borneo <borneo.anto...@gmail.com>

diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c
index 3e09143c68..da6e10bab7 100644
--- a/src/helper/binarybuffer.c
+++ b/src/helper/binarybuffer.c
@@ -225,49 +225,37 @@ static bool str_has_octal_prefix(const char *s)
  */
 static unsigned int str_radix_guess(const char *str)
 {
-       assert(str);
-
        if (str_has_hex_prefix(str))
                return 16;
 
        if (str_has_octal_prefix(str))
                return 8;
 
-       /* Otherwise assume a decadic number. */
+       /* Otherwise assume a decimal number. */
        return 10;
 }
 
 /** Strip leading "0x" or "0X" from hex numbers or "0" from octal numbers. */
-static void str_strip_number_prefix_if_present(const char **_str, unsigned int 
radix)
+static const char *str_strip_number_prefix(const char *str, unsigned int radix)
 {
-       assert(radix == 16 || radix == 10 || radix == 8);
-       assert(_str);
-
-       const char *str = *_str;
-       assert(str);
-
-       if (radix == 16 && str_has_hex_prefix(str))
-               str += 2;
-       else if (radix == 8 && str_has_octal_prefix(str))
-               str += 1;
-
-       /* No prefix to strip for radix == 10. */
-
-       *_str = str;
+       switch (radix) {
+       case 16:
+               return str + 2;
+       case 8:
+               return str + 1;
+       case 10:
+       default:
+               return str;
+       }
 }
 
-int str_to_buf(const char *str, void *_buf, unsigned int buf_len,
-       unsigned int radix, unsigned int *_detected_radix)
+int str_to_buf(const char *str, void *_buf, unsigned int buf_len)
 {
-       assert(radix == 0 || radix == 8 || radix == 10 || radix == 16);
-
-       if (radix == 0)
-               radix = str_radix_guess(str);
+       assert(str);
 
-       if (_detected_radix)
-               *_detected_radix = radix;
+       unsigned int radix = str_radix_guess(str);
 
-       str_strip_number_prefix_if_present(&str, radix);
+       str = str_strip_number_prefix(str, radix);
 
        const size_t str_len = strlen(str);
        if (str_len == 0)
diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h
index 441374330e..6cff86bd92 100644
--- a/src/helper/binarybuffer.h
+++ b/src/helper/binarybuffer.h
@@ -194,15 +194,14 @@ void *buf_set_buf(const void *src, unsigned src_start,
 
 /**
  * Parse an unsigned number (provided as a zero-terminated string)
- * into a bit buffer whose size is buf_len bits.
+ * into a bit buffer whose size is buf_len bits. The base of the
+ * number is detected between decimal, hexadecimal and octal.
  * @param str Input number, zero-terminated string
  * @param _buf Output buffer, allocated by the caller
  * @param buf_len Output buffer size in bits
- * @param radix Base of the input number - 16, 10, 8 or 0.
- *              0 means auto-detect the radix.
+ * @returns Error on invalid or overflowing number
  */
-int str_to_buf(const char *str, void *_buf, unsigned int buf_len,
-       unsigned int radix, unsigned int *_detected_radix);
+int str_to_buf(const char *str, void *_buf, unsigned int buf_len);
 
 char *buf_to_hex_str(const void *buf, unsigned size);
 
diff --git a/src/helper/command.c b/src/helper/command.c
index b5dd927f25..907869325d 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -1365,7 +1365,7 @@ COMMAND_HELPER(command_parse_str_to_buf, const char *str, 
void *buf, unsigned in
        assert(str);
        assert(buf);
 
-       int ret = str_to_buf(str, buf, buf_len, 0, NULL);
+       int ret = str_to_buf(str, buf, buf_len);
        if (ret == ERROR_OK)
                return ret;
 

-- 

Reply via email to