Skip to content

Commit

Permalink
git-compat-util: add strtol_i2
Browse files Browse the repository at this point in the history
This function is an updated version of strtol_i function. It will
give more control to handle parsing of the characters after the
integer and better error handling while parsing numbers.

Signed-off-by: Mohit Marathe <[email protected]>
  • Loading branch information
mohit-marathe committed Jan 22, 2024
1 parent e02ecfc commit 3991862
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,29 @@ static inline int strtol_i(char const *s, int base, int *result)
return 0;
}

#define strtol_i(s,b,r) strtol_i2((s), (b), (r), NULL)
static inline int strtol_i2(char const *s, int base, int *result, char **endp)
{
long ul;
char *dummy = NULL;

if (!endp)
endp = &dummy;
errno = 0;
ul = strtol(s, endp, base);
if (errno ||
/*
* if we are told to parse to the end of the string by
* passing NULL to endp, it is an error to have any
* remaining character after the digits.
*/
(dummy && *dummy) ||
*endp == s || (int) ul != ul)
return -1;
*result = ul;
return 0;
}

void git_stable_qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
#ifdef INTERNAL_QSORT
Expand Down

0 comments on commit 3991862

Please sign in to comment.