]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
od: refactor parse_old_offset
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 30 Jun 2025 01:06:22 +0000 (18:06 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 30 Jun 2025 05:26:14 +0000 (22:26 -0700)
* src/od.c (parse_old_offset): Refactor for brevity and clarity.

src/od.c

index 3381ee484f59f5edc8c9ed75fcde8f73650ab0c6..8acf205e1ddbf741a2186368a50f2cfb0e984f17 100644 (file)
--- a/src/od.c
+++ b/src/od.c
@@ -1416,43 +1416,29 @@ xstr2nonneg (char const *restrict nptr, int base, intmax_t *val,
 static bool
 parse_old_offset (char *s, intmax_t *offset)
 {
-  int radix;
-
   if (*s == '\0')
     return false;
 
-  /* Skip over any leading '+'. */
-  if (s[0] == '+')
-    ++s;
+  /* Skip over any leading '+'.  */
+  s += s[0] == '+';
 
-  /* Determine the radix we'll use to interpret S.  If there is a '.',
-     optionally followed by 'B' or 'b' and then end of string,
-     it's decimal, otherwise, if the string begins with '0X'or '0x',
+  /* Determine the radix for S.  If there is a '.', followed first by
+     optional 'b' or (undocumented) 'B' and then by end of string,
+     it's decimal, otherwise, if the string begins with '0X' or '0x',
      it's hexadecimal, else octal.  */
   char *dot = strchr (s, '.');
-  if (dot)
-    {
-      bool b = dot[1] == 'B' || dot[1] == 'b';
-      if (dot[b + 1])
-        dot = nullptr;
-    }
+  if (dot && dot[(dot[1] == 'b' || dot[1] == 'B') + 1])
+    dot = nullptr;
+  int radix = dot ? 10 : s[0] == '0' && (s[1] == 'x' || s[1] == 'X') ? 16 : 8;
 
   if (dot)
     {
       /* Temporarily remove the '.' from the decimal string.  */
       dot[0] = dot[1];
       dot[1] = '\0';
-      radix = 10;
-    }
-  else
-    {
-      if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
-        radix = 16;
-      else
-        radix = 8;
     }
 
-  enum strtol_error s_err = xstr2nonneg (s, radix, offset, "Bb");
+  enum strtol_error s_err = xstr2nonneg (s, radix, offset, "bB");
 
   if (dot)
     {