Skip to content

Commit

Permalink
lib: Add str_is_float check function
Browse files Browse the repository at this point in the history
  • Loading branch information
cmouse committed Jan 29, 2016
1 parent f0d1de4 commit e764092
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/lib/strnum.c
Expand Up @@ -17,6 +17,32 @@ bool str_is_numeric(const char *str, char end_char)
return TRUE;
}

bool str_is_float(const char *str, char end_char)
{
bool dot_seen = FALSE;
bool num_seen = FALSE;

if (*str == '\0' || *str == end_char)
return FALSE;

while (*str != '\0' && *str != end_char) {
if (*str == '.') {
if (dot_seen || !num_seen) return FALSE;
dot_seen = TRUE;
num_seen = FALSE;
str++;
/* enforce that number follows dot */
continue;
}
if (*str < '0' || *str > '9')
return FALSE;
num_seen = TRUE;
str++;
}

return num_seen;
}

/*
* Unsigned decimal
*/
Expand Down
4 changes: 4 additions & 0 deletions src/lib/strnum.h
Expand Up @@ -180,6 +180,10 @@ int str_to_time(const char *str, time_t *num_r)
Stop when `end_char' is found from string. */
bool str_is_numeric(const char *str, char end_char) ATTR_PURE;

/* Return TRUE when string has one or more numbers, followed
with zero or one dot, followed with at least one number. */
bool str_is_float(const char *str, char end_char) ATTR_PURE;

/* Returns human readable string about what is wrong with the string.
This function assumes that str_to_*() had already returned -1 for the
string. */
Expand Down

0 comments on commit e764092

Please sign in to comment.