summaryrefslogtreecommitdiff
path: root/src/pk.h
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-04-07 11:19:35 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-04-07 11:19:35 -0400
commit2ffd9e47e72fd30b2e64738b275b88593f08fe40 (patch)
treecfa932ebe9aa81638f05e2e62b2c5de9e8881975 /src/pk.h
parenta02c7589c6c9e902c59a632aa650635336fe648c (diff)
pk.h: update to 0.4.6
Diffstat (limited to 'src/pk.h')
-rw-r--r--src/pk.h123
1 files changed, 62 insertions, 61 deletions
diff --git a/src/pk.h b/src/pk.h
index dce0d99..587c05b 100644
--- a/src/pk.h
+++ b/src/pk.h
@@ -1,7 +1,7 @@
#ifndef PK_SINGLE_HEADER_FILE_H
#define PK_SINGLE_HEADER_FILE_H
/*******************************************************************************
-* PK Single-Header-Library V0.4.5
+* PK Single-Header-Library V0.4.6
*
* Author: Jonathan Bradley
* Copyright: © 2024-2025 Jonathan Bradley
@@ -254,7 +254,7 @@
*
*******************************************************************************/
-#define PK_VERSION "0.4.5"
+#define PK_VERSION "0.4.6"
#ifdef PK_IMPL_ALL
# ifndef PK_IMPL_MEM_TYPES
@@ -1961,53 +1961,56 @@ enum PK_STN_RES {
PK_STN_RES_INCONVERTIBLE
};
-enum PK_STN_RES pk_stn_int64_t(int64_t *i, char const *s, int base);
-enum PK_STN_RES pk_stn_uint64_t(uint64_t *i, char const *s, int base);
-enum PK_STN_RES pk_stn_int32_t(int32_t *i, char const *s, int base);
-enum PK_STN_RES pk_stn_uint32_t(uint32_t *i, char const *s, int base);
-enum PK_STN_RES pk_stn_int16_t(int16_t *i, char const *s, int base);
-enum PK_STN_RES pk_stn_uint16_t(uint16_t *i, char const *s, int base);
-enum PK_STN_RES pk_stn_int8_t(int8_t *i, char const *s, int base);
-enum PK_STN_RES pk_stn_uint8_t(uint8_t *i, char const *s, int base);
-enum PK_STN_RES pk_stn_float(float *f, char const *s);
-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);
+enum PK_STN_RES pk_stn_int64_t(int64_t *i, char const *s, char **pEnd, int base);
+enum PK_STN_RES pk_stn_uint64_t(uint64_t *i, char const *s, char **pEnd, int base);
+enum PK_STN_RES pk_stn_int32_t(int32_t *i, char const *s, char **pEnd, int base);
+enum PK_STN_RES pk_stn_uint32_t(uint32_t *i, char const *s, char **pEnd, int base);
+enum PK_STN_RES pk_stn_int16_t(int16_t *i, char const *s, char **pEnd, int base);
+enum PK_STN_RES pk_stn_uint16_t(uint16_t *i, char const *s, char **pEnd, int base);
+enum PK_STN_RES pk_stn_int8_t(int8_t *i, char const *s, char **pEnd, int base);
+enum PK_STN_RES pk_stn_uint8_t(uint8_t *i, char const *s, char **pEnd, int base);
+enum PK_STN_RES pk_stn_float(float *f, char const *s, char **pEnd);
+enum PK_STN_RES pk_stn_double(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)
+enum PK_STN_RES pk_stn(T *n, char const *s, char **pEnd, int base = 0)
{
if constexpr(std::is_same<T, int64_t>::value) {
- return pk_stn_int64_t(n, s, base);
+ return pk_stn_int64_t(n, s, pEnd, base);
}
if constexpr(std::is_same<T, uint64_t>::value) {
- return pk_stn_uint64_t(n, s, base);
+ return pk_stn_uint64_t(n, s, pEnd, base);
}
if constexpr(std::is_same<T, int32_t>::value) {
- return pk_stn_int32_t(n, s, base);
+ return pk_stn_int32_t(n, s, pEnd, base);
}
if constexpr(std::is_same<T, uint32_t>::value) {
- return pk_stn_uint32_t(n, s, base);
+ return pk_stn_uint32_t(n, s, pEnd, base);
}
if constexpr(std::is_same<T, int16_t>::value) {
- return pk_stn_int16_t(n, s, base);
+ return pk_stn_int16_t(n, s, pEnd, base);
}
if constexpr(std::is_same<T, uint16_t>::value) {
- return pk_stn_uint16_t(n, s, base);
+ return pk_stn_uint16_t(n, s, pEnd, base);
}
if constexpr(std::is_same<T, int8_t>::value) {
- return pk_stn_int8_t(n, s, base);
+ return pk_stn_int8_t(n, s, pEnd, base);
}
if constexpr(std::is_same<T, uint8_t>::value) {
- return pk_stn_uint8_t(n, s, base);
+ return pk_stn_uint8_t(n, s, pEnd, base);
+ }
+ if constexpr(std::is_same<T, bool>::value) {
+ static_assert(sizeof(bool) == sizeof(uint8_t));
+ static_assert(alignof(bool) == alignof(uint8_t));
+ return pk_stn_uint8_t((uint8_t*)n, s, pEnd, base);
}
if constexpr(std::is_same<T, float>::value) {
- return pk_stn_float(n, s);
+ return pk_stn_float(n, s, pEnd);
}
if constexpr(std::is_same<T, double>::value) {
- return pk_stn_double(n, s);
+ return pk_stn_double(n, s, pEnd);
}
return (PK_STN_RES)-1;
}
@@ -2019,17 +2022,18 @@ enum PK_STN_RES pk_stn(T *n, char const *s, int base = 0)
#ifdef PK_IMPL_STN
enum PK_STN_RES
-pk_stn_int64_t(int64_t *i, char const *s, int base)
+pk_stn_int64_t(int64_t *i, char const *s, char **pEnd, int base)
{
char *end;
long long l;
errno = 0;
l = strtoll(s, &end, base);
+ if (pEnd != nullptr) *pEnd = end;
if (errno == ERANGE) {
if (l == LLONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
- if (*s == '\0' || *end != '\0') {
+ if (s == end) {
return PK_STN_RES_INCONVERTIBLE;
}
*i = l;
@@ -2037,17 +2041,18 @@ pk_stn_int64_t(int64_t *i, char const *s, int base)
}
enum PK_STN_RES
-pk_stn_uint64_t(uint64_t *i, char const *s, int base)
+pk_stn_uint64_t(uint64_t *i, char const *s, char **pEnd, int base)
{
char *end;
unsigned long long l;
errno = 0;
l = strtoull(s, &end, base);
+ if (pEnd != nullptr) *pEnd = end;
if (errno == ERANGE) {
if (l == ULLONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
- if (*s == '\0' || *end != '\0') {
+ if (s == end) {
return PK_STN_RES_INCONVERTIBLE;
}
*i = l;
@@ -2055,17 +2060,18 @@ pk_stn_uint64_t(uint64_t *i, char const *s, int base)
}
enum PK_STN_RES
-pk_stn_int32_t(int32_t *i, char const *s, int base)
+pk_stn_int32_t(int32_t *i, char const *s, char **pEnd, int base)
{
char *end;
long l;
errno = 0;
l = strtol(s, &end, base);
+ if (pEnd != nullptr) *pEnd = end;
if (errno == ERANGE) {
if (l == LONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
- if (*s == '\0' || *end != '\0') {
+ if (s == end) {
return PK_STN_RES_INCONVERTIBLE;
}
*i = l;
@@ -2073,17 +2079,18 @@ pk_stn_int32_t(int32_t *i, char const *s, int base)
}
enum PK_STN_RES
-pk_stn_uint32_t(uint32_t *i, char const *s, int base)
+pk_stn_uint32_t(uint32_t *i, char const *s, char **pEnd, int base)
{
char *end;
unsigned long l;
errno = 0;
l = strtoul(s, &end, base);
+ if (pEnd != nullptr) *pEnd = end;
if (errno == ERANGE) {
if (l == ULONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
- if (*s == '\0' || *end != '\0') {
+ if (s == end) {
return PK_STN_RES_INCONVERTIBLE;
}
*i = l;
@@ -2091,17 +2098,18 @@ pk_stn_uint32_t(uint32_t *i, char const *s, int base)
}
enum PK_STN_RES
-pk_stn_int16_t(int16_t *i, char const *s, int base)
+pk_stn_int16_t(int16_t *i, char const *s, char **pEnd, int base)
{
char *end;
long l;
errno = 0;
l = strtol(s, &end, base);
+ if (pEnd != nullptr) *pEnd = end;
if (errno == ERANGE) {
if (l == LONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
- if (*s == '\0' || *end != '\0') {
+ if (s == end) {
return PK_STN_RES_INCONVERTIBLE;
}
*i = l;
@@ -2109,17 +2117,18 @@ pk_stn_int16_t(int16_t *i, char const *s, int base)
}
enum PK_STN_RES
-pk_stn_uint16_t(uint16_t *i, char const *s, int base)
+pk_stn_uint16_t(uint16_t *i, char const *s, char **pEnd, int base)
{
char *end;
unsigned long l;
errno = 0;
l = strtoul(s, &end, base);
+ if (pEnd != nullptr) *pEnd = end;
if (errno == ERANGE) {
if (l == ULONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
- if (*s == '\0' || *end != '\0') {
+ if (s == end) {
return PK_STN_RES_INCONVERTIBLE;
}
*i = l;
@@ -2127,17 +2136,18 @@ pk_stn_uint16_t(uint16_t *i, char const *s, int base)
}
enum PK_STN_RES
-pk_stn_int8_t(int8_t *i, char const *s, int base)
+pk_stn_int8_t(int8_t *i, char const *s, char **pEnd, int base)
{
char *end;
long l;
errno = 0;
l = strtol(s, &end, base);
+ if (pEnd != nullptr) *pEnd = end;
if (errno == ERANGE) {
if (l == LONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
- if (*s == '\0' || *end != '\0') {
+ if (s == end) {
return PK_STN_RES_INCONVERTIBLE;
}
*i = l;
@@ -2145,17 +2155,18 @@ pk_stn_int8_t(int8_t *i, char const *s, int base)
}
enum PK_STN_RES
-pk_stn_uint8_t(uint8_t *i, char const *s, int base)
+pk_stn_uint8_t(uint8_t *i, char const *s, char **pEnd, int base)
{
char *end;
unsigned long l;
errno = 0;
l = strtoul(s, &end, base);
+ if (pEnd != nullptr) *pEnd = end;
if (errno == ERANGE) {
if (l == ULONG_MAX) return PK_STN_RES_OVERFLOW;
return PK_STN_RES_UNDERFLOW;
}
- if (*s == '\0' || *end != '\0') {
+ if (s == end) {
return PK_STN_RES_INCONVERTIBLE;
}
*i = l;
@@ -2163,32 +2174,20 @@ pk_stn_uint8_t(uint8_t *i, char const *s, int base)
}
enum PK_STN_RES
-pk_stn_float(float *f, char const *s)
-{
- char *end;
- return pk_stn_float_e(f, s, &end);
-}
-
-enum PK_STN_RES
-pk_stn_double(double *d, char const *s)
+pk_stn_float(float *f, char const *s, char **pEnd)
{
char *end;
- return pk_stn_double_e(d, s, &end);
-}
-
-enum PK_STN_RES
-pk_stn_float_e(float *f, char const *s, char **pEnd)
-{
float l;
errno = 0;
- l = strtof(s, pEnd);
+ l = strtof(s, &end);
+ if (pEnd != nullptr) *pEnd = end;
if (errno == ERANGE && l == HUGE_VALF) {
return PK_STN_RES_OVERFLOW;
}
if (errno == ERANGE && l == -HUGE_VALF) {
return PK_STN_RES_UNDERFLOW;
}
- if (*s == '\0' || &s == (const char **)pEnd) {
+ if (s == end) {
return PK_STN_RES_INCONVERTIBLE;
}
*f = l;
@@ -2196,18 +2195,20 @@ 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)
+pk_stn_double(double *d, char const *s, char **pEnd)
{
+ char *end;
double l;
errno = 0;
- l = strtod(s, pEnd);
+ l = strtod(s, &end);
+ if (pEnd != nullptr) *pEnd = end;
if (errno == ERANGE && l == HUGE_VAL) {
return PK_STN_RES_OVERFLOW;
}
if (errno == ERANGE && l == -HUGE_VAL) {
return PK_STN_RES_UNDERFLOW;
}
- if (*s == '\0' || &s == (const char **)pEnd) {
+ if (s == end) {
return PK_STN_RES_INCONVERTIBLE;
}
*d = l;
@@ -2417,7 +2418,7 @@ bool pk_uuid_parse(const char *s, struct pk_uuid *uuid)
}
c[0] = s[k];
c[1] = s[k+1];
- if (pk_stn_uint8_t(&uuid->uuid[kk], c, 16) != PK_STN_RES_SUCCESS) {
+ if (pk_stn_uint8_t(&uuid->uuid[kk], c, nullptr, 16) != PK_STN_RES_SUCCESS) {
goto err_out;
}
}