feat: add cli_parse_ufloat for non-negative float parsing

This commit is contained in:
lohhiiccc 2026-03-29 15:17:17 +02:00
parent 546862c650
commit 5f0f989e98
3 changed files with 24 additions and 1 deletions

View file

@ -6,5 +6,6 @@
int cli_parse_uint64(const char *s, uint64_t *out);
int cli_parse_int64(const char *s, int64_t *out);
int cli_parse_float(const char *s, float *out);
int cli_parse_ufloat(const char *s, float *out);
#endif

View file

@ -5,7 +5,8 @@ libcli_la_SOURCES = \
help.c \
parse_utils/parse_uint.c \
parse_utils/parse_int.c \
parse_utils/parse_float.c
parse_utils/parse_float.c \
parse_utils/parse_ufloat.c
libcli_la_CPPFLAGS = -I$(top_srcdir)/include
libcli_la_CFLAGS = -std=c99 -Wall -Wextra

View file

@ -0,0 +1,21 @@
#include <errno.h>
#include <stdlib.h>
#include <math.h>
int
cli_parse_ufloat(const char *s, float *out)
{
char *end;
if (NULL == s || '\0' == *s)
return 1;
errno = 0;
float v = strtof(s, &end);
if (s == end || ERANGE == errno || '\0' != *end || 0 == isfinite(v) || v < 0.0f)
return 1;
*out = v;
return 0;
}