small improvements in str -> num convertors
This commit is contained in:
parent
f2408e6c3f
commit
43bddf31e9
@ -317,17 +317,11 @@ struct suffix_mult {
|
|||||||
};
|
};
|
||||||
#include "xatonum.h"
|
#include "xatonum.h"
|
||||||
/* Specialized: */
|
/* Specialized: */
|
||||||
unsigned xatou_range(const char *numstr, unsigned lower, unsigned upper);
|
|
||||||
unsigned xatou_sfx(const char *numstr, const struct suffix_mult *suffixes);
|
|
||||||
unsigned xatou(const char *numstr);
|
|
||||||
int xatoi_range(const char *numstr, int lower, int upper);
|
|
||||||
int xatoi(const char *numstr);
|
|
||||||
/* Using xatoi() instead of naive atoi() is not always convenient -
|
/* Using xatoi() instead of naive atoi() is not always convenient -
|
||||||
* in many places people want *non-negative* values, but store them
|
* in many places people want *non-negative* values, but store them
|
||||||
* in signed int. Therefore we need this one:
|
* in signed int. Therefore we need this one:
|
||||||
* dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc */
|
* dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc */
|
||||||
int xatoi_u(const char *numstr);
|
int xatoi_u(const char *numstr);
|
||||||
uint32_t xatou32(const char *numstr);
|
|
||||||
/* Useful for reading port numbers */
|
/* Useful for reading port numbers */
|
||||||
uint16_t xatou16(const char *numstr);
|
uint16_t xatou16(const char *numstr);
|
||||||
|
|
||||||
|
@ -92,3 +92,15 @@ DEFINE_EQUIV_STR_CONV(int, i, l, u, ul)
|
|||||||
#else
|
#else
|
||||||
DECLARE_STR_CONV(int, i, u)
|
DECLARE_STR_CONV(int, i, u)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Specialized */
|
||||||
|
|
||||||
|
int BUG_xatou32_unimplemented(void);
|
||||||
|
extern inline uint32_t xatou32(const char *numstr)
|
||||||
|
{
|
||||||
|
if (UINT_MAX == 0xffffffff)
|
||||||
|
return xatou(numstr);
|
||||||
|
if (ULONG_MAX == 0xffffffff)
|
||||||
|
return xatoul(numstr);
|
||||||
|
return BUG_xatou32_unimplemented();
|
||||||
|
}
|
||||||
|
@ -52,6 +52,15 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if UINT_MAX != ULONG_MAX
|
#if UINT_MAX != ULONG_MAX
|
||||||
|
extern inline unsigned bb_strtoui(const char *str, char **end, int b)
|
||||||
|
{
|
||||||
|
unsigned long v = strtoul(str, end, b);
|
||||||
|
if (v > UINT_MAX) {
|
||||||
|
errno = ERANGE;
|
||||||
|
return UINT_MAX;
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
#define type int
|
#define type int
|
||||||
#define xstrtou(rest) xstrtou##rest
|
#define xstrtou(rest) xstrtou##rest
|
||||||
#define xstrto(rest) xstrtoi##rest
|
#define xstrto(rest) xstrtoi##rest
|
||||||
@ -60,7 +69,8 @@
|
|||||||
#define XSTR_UTYPE_MAX UINT_MAX
|
#define XSTR_UTYPE_MAX UINT_MAX
|
||||||
#define XSTR_TYPE_MAX INT_MAX
|
#define XSTR_TYPE_MAX INT_MAX
|
||||||
#define XSTR_TYPE_MIN INT_MIN
|
#define XSTR_TYPE_MIN INT_MIN
|
||||||
#define XSTR_STRTOU strtoul
|
/* libc has no strtoui, so we need to create/use our own */
|
||||||
|
#define XSTR_STRTOU bb_strtoui
|
||||||
#include "xatonum_template.c"
|
#include "xatonum_template.c"
|
||||||
#undef type
|
#undef type
|
||||||
#undef xstrtou
|
#undef xstrtou
|
||||||
@ -77,7 +87,7 @@
|
|||||||
|
|
||||||
int xatoi_u(const char *numstr)
|
int xatoi_u(const char *numstr)
|
||||||
{
|
{
|
||||||
return xatoul_range(numstr, 0, INT_MAX);
|
return xatou_range(numstr, 0, INT_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t xatou32(const char *numstr)
|
uint32_t xatou32(const char *numstr)
|
||||||
@ -87,5 +97,5 @@ uint32_t xatou32(const char *numstr)
|
|||||||
|
|
||||||
uint16_t xatou16(const char *numstr)
|
uint16_t xatou16(const char *numstr)
|
||||||
{
|
{
|
||||||
return xatoul_range(numstr, 0, 0xffff);
|
return xatou_range(numstr, 0, 0xffff);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user