summaryrefslogtreecommitdiff
path: root/src/pk.h
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-03-27 21:36:10 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-03-27 21:36:10 -0400
commit119c83096a81c5aef45a0dbef029bd2e49d2e977 (patch)
tree730a07672c1db26f81a2b0008db6d452c506687b /src/pk.h
parente4604d5b84a71ac3cc8fe1a148d0a6250c7a715c (diff)
pk.h: update to 0.4.5
Diffstat (limited to 'src/pk.h')
-rw-r--r--src/pk.h78
1 files changed, 55 insertions, 23 deletions
diff --git a/src/pk.h b/src/pk.h
index c9e1d44..dce0d99 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.4
+* PK Single-Header-Library V0.4.5
*
* Author: Jonathan Bradley
* Copyright: © 2024-2025 Jonathan Bradley
@@ -254,7 +254,7 @@
*
*******************************************************************************/
-#define PK_VERSION "0.4.4"
+#define PK_VERSION "0.4.5"
#ifdef PK_IMPL_ALL
# ifndef PK_IMPL_MEM_TYPES
@@ -2269,12 +2269,15 @@ void pk_uuid_teardown();
struct pk_uuid pk_uuid_new_v7();
bool pk_uuid_equals(struct pk_uuid lhs, struct pk_uuid rhs);
+bool pk_uuid_parse(const char *s, struct pk_uuid *uuid);
#if defined(__cplusplus)
#include <ostream>
#include <iomanip>
std::ostream& operator<<(std::ostream &o, const struct pk_uuid& uuid);
std::istream& operator>>(std::istream &i, struct pk_uuid& uuid);
+const char* operator>>(const char *s, struct pk_uuid& uuid);
+struct pk_uuid& operator<<(struct pk_uuid& uuid, const char *s);
bool operator==(const pk_uuid &lhs, const pk_uuid &rhs);
bool operator!=(const pk_uuid &lhs, const pk_uuid &rhs);
#endif
@@ -2283,7 +2286,7 @@ bool operator!=(const pk_uuid &lhs, const pk_uuid &rhs);
#ifdef PK_IMPL_UUID
-#include <stdatomic.h>
+
#include <stdlib.h>
#include <stdint.h>
@@ -2397,6 +2400,33 @@ bool pk_uuid_equals(struct pk_uuid lhs, struct pk_uuid rhs)
return true;
}
+bool pk_uuid_parse(const char *s, struct pk_uuid *uuid)
+{
+ // ffffffff-ffff-ffff-ffff-ffffffffffff
+ // 0 8 13 18 23 35
+ char c[3] = {'\0','\0','\0'};
+ unsigned char k, kk;
+ if (s == nullptr) goto err_out;
+ for (k = 0, kk = 0; k < 36; k+=2, ++kk) {
+ if (s[k] == '\0' || s[k+1] == '\0') goto err_out;
+ if (k == 8 || k == 13 || k == 18 || k == 23) {
+ if (s[k] != '-') goto err_out;
+ k -= 1;
+ kk -= 1;
+ continue;
+ }
+ c[0] = s[k];
+ c[1] = s[k+1];
+ if (pk_stn_uint8_t(&uuid->uuid[kk], c, 16) != PK_STN_RES_SUCCESS) {
+ goto err_out;
+ }
+ }
+ return true;
+err_out:
+ *uuid = pk_uuid_zed;
+ return false;
+}
+
#if defined(__cplusplus)
std::ostream&
operator<<(std::ostream &o, const struct pk_uuid& uuid)
@@ -2437,33 +2467,35 @@ operator<<(std::ostream &o, const struct pk_uuid& uuid)
std::istream&
operator>>(std::istream &i, struct pk_uuid& uuid)
{
- char c[3];
- char k = 0;
- char offset = 0;
- c[2] = '\0';
- for (k = 0; k < 20; ++k) {
- if (k == 4 || k == 7 || k == 10 || k == 13) {
- offset += 1;
- c[0] = i.peek();
- if (c[0] != '-') {
- goto err_out;
- }
- i.get(); // burn
- continue;
- }
- i.get(c[0]);
- i.get(c[1]);
- if (pk_stn_uint8_t(&uuid.uuid[k - offset], c, 16) != PK_STN_RES_SUCCESS) {
- goto err_out;
- }
+ char u[36];
+ i.read(u, 36);
+ if (i.rdstate() & std::ios::failbit) {
+ goto err_out;
+ } else if (pk_uuid_parse(u, &uuid) == false) {
+ goto err_out;
}
return i;
err_out:
- i.seekg(-(((k + 1) * 2) + offset), std::ios_base::cur);
uuid = pk_uuid_zed;
+ i.seekg(-36, std::ios_base::cur);
+ i.setstate(std::ios::failbit);
return i;
}
+const char * operator>>(const char *s, struct pk_uuid& uuid)
+{
+ if (pk_uuid_parse(s, &uuid)) {
+ return s+36;
+ }
+ return s;
+}
+
+struct pk_uuid& operator<<(struct pk_uuid& uuid, const char *s)
+{
+ pk_uuid_parse(s, &uuid);
+ return uuid;
+}
+
bool operator==(const pk_uuid &lhs, const pk_uuid &rhs) { return pk_uuid_equals(lhs, rhs); }
bool operator!=(const pk_uuid &lhs, const pk_uuid &rhs) { return !pk_uuid_equals(lhs, rhs); }