Skip to content

Commit

Permalink
lib/getrange.c: getrange(): Use goto to deduplicate code
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar authored and hallyn committed May 4, 2024
1 parent 7af7361 commit f40bd94
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions lib/getrange.c
Expand Up @@ -39,16 +39,8 @@ getrange(const char *range,
*has_max = false;

if ('-' == range[0]) {
if (!isdigit(range[1]))
return -1;

errno = 0;
*max = strtoul_noneg(&range[1], &endptr, 10);
if (('\0' != *endptr) || (0 != errno))
return -1;
*has_max = true;

return 0; /* -<long> */
endptr = range + 1;
goto parse_max;
}

errno = 0;
Expand All @@ -57,16 +49,16 @@ getrange(const char *range,
return -1;
*has_min = true;

switch (*endptr) {
switch (*endptr++) {
case '\0':
*has_max = true;
*max = *min;
return 0; /* <long> */

case '-':
endptr++;
if ('\0' == *endptr)
return 0; /* <long>- */
parse_max:
if (!isdigit(*endptr))
return -1;

Expand All @@ -76,7 +68,7 @@ getrange(const char *range,
return -1;
*has_max = true;

return 0; /* <long>-<long> */
return 0; /* <long>-<long>, or -<long> */

default:
return -1;
Expand Down

0 comments on commit f40bd94

Please sign in to comment.