summaryrefslogtreecommitdiff
path: root/pkstn.h
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2024-12-14 13:50:09 -0500
committerJonathan Bradley <jcb@pikum.xyz>2024-12-14 13:50:09 -0500
commit33484cccdea2790721fb20f75588d1ed4fb53017 (patch)
treee89020d1c2b9818378a47059e33f82bfb1af5091 /pkstn.h
parent243866a7ae51a7187832cc5bf7e5e6d8a0fd2202 (diff)
pkstn: more tests + pk_stn<T>
Diffstat (limited to 'pkstn.h')
-rw-r--r--pkstn.h88
1 files changed, 56 insertions, 32 deletions
diff --git a/pkstn.h b/pkstn.h
index 1d5430f..d3131f7 100644
--- a/pkstn.h
+++ b/pkstn.h
@@ -27,6 +27,46 @@ enum PK_STN_RES pk_stn_double(double *d, char const *s);
enum PK_STN_RES pk_stn_float_e(float *f, char const *s, char **pEnd);
enum PK_STN_RES pk_stn_double_e(double *d, char const *s, char **pEnd);
+#if defined(__cplusplus)
+
+template <typename T>
+enum PK_STN_RES pk_stn(T *n, char const *s, int base = 0)
+{
+ if constexpr(std::is_same<T, int64_t>::value) {
+ return pk_stn_int64_t(n, s, base);
+ }
+ if constexpr(std::is_same<T, uint64_t>::value) {
+ return pk_stn_uint64_t(n, s, base);
+ }
+ if constexpr(std::is_same<T, int32_t>::value) {
+ return pk_stn_int32_t(n, s, base);
+ }
+ if constexpr(std::is_same<T, uint32_t>::value) {
+ return pk_stn_uint32_t(n, s, base);
+ }
+ if constexpr(std::is_same<T, int16_t>::value) {
+ return pk_stn_int16_t(n, s, base);
+ }
+ if constexpr(std::is_same<T, uint16_t>::value) {
+ return pk_stn_uint16_t(n, s, base);
+ }
+ if constexpr(std::is_same<T, int8_t>::value) {
+ return pk_stn_int8_t(n, s, base);
+ }
+ if constexpr(std::is_same<T, uint8_t>::value) {
+ return pk_stn_uint8_t(n, s, base);
+ }
+ if constexpr(std::is_same<T, float>::value) {
+ return pk_stn_float(n, s);
+ }
+ if constexpr(std::is_same<T, double>::value) {
+ return pk_stn_double(n, s);
+ }
+ return (PK_STN_RES)-1;
+}
+
+#endif /* defined(__cplusplus) */
+
#endif /* PK_PK_STN_H */
#ifdef PK_IMPL_STN
@@ -38,10 +78,8 @@ pk_stn_int64_t(int64_t *i, char const *s, int base)
long long l;
errno = 0;
l = strtoll(s, &end, base);
- if (errno == ERANGE && l == LLONG_MAX) {
- return PK_STN_RES_OVERFLOW;
- }
- if (errno == ERANGE && l == LLONG_MIN) {
+ if (errno == ERANGE) {
+ if (l == LLONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
if (*s == '\0' || *end != '\0') {
@@ -58,10 +96,8 @@ pk_stn_uint64_t(uint64_t *i, char const *s, int base)
unsigned long long l;
errno = 0;
l = strtoull(s, &end, base);
- if (errno == ERANGE && l == LLONG_MAX) {
- return PK_STN_RES_OVERFLOW;
- }
- if (errno == ERANGE && l == LLONG_MIN) {
+ if (errno == ERANGE) {
+ if (l == ULLONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
if (*s == '\0' || *end != '\0') {
@@ -78,10 +114,8 @@ pk_stn_int32_t(int32_t *i, char const *s, int base)
long l;
errno = 0;
l = strtol(s, &end, base);
- if (errno == ERANGE && l == LONG_MAX) {
- return PK_STN_RES_OVERFLOW;
- }
- if (errno == ERANGE && l == LONG_MIN) {
+ if (errno == ERANGE) {
+ if (l == LONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
if (*s == '\0' || *end != '\0') {
@@ -98,10 +132,8 @@ pk_stn_uint32_t(uint32_t *i, char const *s, int base)
unsigned long l;
errno = 0;
l = strtoul(s, &end, base);
- if (errno == ERANGE && l == LONG_MAX) {
- return PK_STN_RES_OVERFLOW;
- }
- if (errno == ERANGE && l == LONG_MIN) {
+ if (errno == ERANGE) {
+ if (l == ULONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
if (*s == '\0' || *end != '\0') {
@@ -118,10 +150,8 @@ pk_stn_int16_t(int16_t *i, char const *s, int base)
long l;
errno = 0;
l = strtol(s, &end, base);
- if (errno == ERANGE && l == LONG_MAX) {
- return PK_STN_RES_OVERFLOW;
- }
- if (errno == ERANGE && l == LONG_MIN) {
+ if (errno == ERANGE) {
+ if (l == LONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
if (*s == '\0' || *end != '\0') {
@@ -138,10 +168,8 @@ pk_stn_uint16_t(uint16_t *i, char const *s, int base)
unsigned long l;
errno = 0;
l = strtoul(s, &end, base);
- if (errno == ERANGE && l == LONG_MAX) {
- return PK_STN_RES_OVERFLOW;
- }
- if (errno == ERANGE && l == LONG_MIN) {
+ if (errno == ERANGE) {
+ if (l == ULONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
if (*s == '\0' || *end != '\0') {
@@ -158,10 +186,8 @@ pk_stn_int8_t(int8_t *i, char const *s, int base)
long l;
errno = 0;
l = strtol(s, &end, base);
- if (errno == ERANGE && l == LONG_MAX) {
- return PK_STN_RES_OVERFLOW;
- }
- if (errno == ERANGE && l == LONG_MIN) {
+ if (errno == ERANGE) {
+ if (l == LONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
if (*s == '\0' || *end != '\0') {
@@ -178,10 +204,8 @@ pk_stn_uint8_t(uint8_t *i, char const *s, int base)
unsigned long l;
errno = 0;
l = strtoul(s, &end, base);
- if (errno == ERANGE && l == LONG_MAX) {
- return PK_STN_RES_OVERFLOW;
- }
- if (errno == ERANGE && l == LONG_MIN) {
+ if (errno == ERANGE) {
+ if (l == ULONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
if (*s == '\0' || *end != '\0') {