diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-27 16:13:10 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-27 16:13:10 -0400 |
| commit | c65204648f0a81cce25c12c6dcd58d76810ac303 (patch) | |
| tree | 4634f98672d00e321ab19d51629af130f1a27fbf /pkuuid.h | |
| parent | be07962dc264b732f9facafec7bbe4019bf91ad2 (diff) | |
pkuuid: add << and >> for const char *
Diffstat (limited to 'pkuuid.h')
| -rw-r--r-- | pkuuid.h | 76 |
1 files changed, 54 insertions, 22 deletions
@@ -19,12 +19,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 @@ -33,7 +36,8 @@ bool operator!=(const pk_uuid &lhs, const pk_uuid &rhs); #ifdef PK_IMPL_UUID -#include <stdatomic.h> +#include "./pkstn.h" /* deleteme */ + #include <stdlib.h> #include <stdint.h> @@ -147,8 +151,34 @@ 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) -#include "./pkstn.h" /* deleteme */ std::ostream& operator<<(std::ostream &o, const struct pk_uuid& uuid) { @@ -188,33 +218,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); } |
