diff options
author | pgen <p.gen.progs@gmail.com> | 2023-11-30 23:31:58 +0100 |
---|---|---|
committer | pgen <p.gen.progs@gmail.com> | 2023-11-30 23:31:58 +0100 |
commit | c18b9276910a07248a28f82c6d2874519d8ba923 (patch) | |
tree | 8dc80f043de58624808893e406292e97ff1e5837 | |
parent | 926f41e96aebf479f2dc27d95573f1b7f6989a57 (diff) |
Simplification of the is_integer function
-rw-r--r-- | utils.c | 14 |
1 files changed, 6 insertions, 8 deletions
@@ -316,20 +316,18 @@ my_wcscasecmp(const wchar_t *s1, const wchar_t *s2) return -*s2; } -/* ==================================================================== */ -/* Returns 1 if s can be converted into an integer otherwise returns 0. */ -/* ==================================================================== */ +/* ================================================================ */ +/* Returns 1 if s can be converted into an int otherwise returns 0. */ +/* ================================================================ */ int is_integer(const char * const s) { long int n; + char *endptr; - if (!s || (strspn(s, "0123456789 ") != strlen(s))) - return 0; - - n = strtol(s, NULL, 10); + n = strtol(s, &endptr, 10); - if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) + if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX && *endptr == '\0') return 1; return 0; |