summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2024-10-11 22:37:13 -0400
committerJonathan Bradley <jcb@pikum.xyz>2024-10-11 22:37:13 -0400
commit7889de050105a9fba827c46eaae767490c75177c (patch)
treebd05d8ec6779e947e45c25887ef31890525b7f51
parentb70fe8a9a945b9822d437c09f157a3fd81781617 (diff)
pkstr: add
-rw-r--r--Makefile20
-rw-r--r--pkstr.h84
-rw-r--r--test/pkstr.c56
-rw-r--r--test/pkstr.cpp57
4 files changed, 216 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 6acbc79..4891508 100644
--- a/Makefile
+++ b/Makefile
@@ -9,12 +9,15 @@ SRC = \
pkmacros.h \
pkmem-types.h \
pkmem.h \
+ pkstr.h \
test/pkmacros.c \
test/pkmacros.cpp \
test/pkmem-types.c \
test/pkmem-types.cpp \
test/pkmem.c \
test/pkmem.cpp \
+ test/pkstr.c \
+ test/pkstr.cpp \
OBJ = $(SRC:%.c=.o)
PPOBJ = $(SRC:%.cpp=.so)
@@ -22,10 +25,14 @@ HOBJ = $(SRC:%.h=.gch)
HPPOBJ = $(SRC:%.h=.gchpp)
all: options .WAIT clean .WAIT \
- pkmacros pkmem-types pkmem \
+ pkmacros \
+ pkmem-types \
+ pkmem \
+ pkstr \
test-pkmem test-pkmem-cpp \
test-types-pkmem test-types-pkmem-cpp \
test-pkmacros test-pkmacros-cpp \
+ test-pkstr test-pkstr-cpp \
options:
@echo at-suite build options:
@@ -55,6 +62,8 @@ pkmem-types: pkmacros pkmem-types.gch pkmem-types.gchpp
pkmem: pkmem-types pkmem.gch pkmem.gchpp
+pkstr: pkmacros pkstr.gch pkstr.gchpp
+
test-pkmacros: test/pkmacros.o
$(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS)
@@ -73,10 +82,17 @@ test-pkmem: test/pkmem.o
test-pkmem-cpp: test/pkmem.so
$(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS)
+test-pkstr: test/pkstr.o
+ $(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS)
+
+test-pkstr-cpp: test/pkstr.so
+ $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS)
+
test: pkmacros pkmem-types pkmem
test: test-pkmacros test-pkmacros-cpp
test: test-pkmem-types test-pkmem-types-cpp
test: test-pkmem test-pkmem-cpp
+test: test-pkstr test-pkstr-cpp
test:
@echo ""
./test/test-pkmacros ; echo Result: $$? "\n"
@@ -85,6 +101,8 @@ test:
./test/test-pkmem-types-cpp ; echo Result: $$? "\n"
./test/test-pkmem ; echo Result: $$? "\n"
./test/test-pkmem-cpp ; echo Result: $$? "\n"
+ ./test/test-pkstr ; echo Result: $$? "\n"
+ ./test/test-pkstr-cpp ; echo Result: $$? "\n"
clean:
rm -f *.plist *.gch *.gchpp *.o *.so test/*.o test/*.so test/test-*
diff --git a/pkstr.h b/pkstr.h
new file mode 100644
index 0000000..fc53c49
--- /dev/null
+++ b/pkstr.h
@@ -0,0 +1,84 @@
+#ifndef PK_STR_H
+#define PK_STR_H
+
+#include <stdint.h>
+
+struct pk_str {
+ char *val;
+ uint32_t length;
+ uint32_t reserved;
+};
+struct pk_cstr {
+ const char *val;
+ uint32_t length;
+ uint32_t reserved;
+};
+
+struct pk_str cstring_to_pk_str(char *s);
+struct pk_cstr cstring_to_pk_cstr(const char *s);
+struct pk_str pk_cstr_to_pk_str(const struct pk_cstr *s);
+struct pk_cstr pk_str_to_pk_cstr(const struct pk_str *s);
+int pk_compare_str(const struct pk_str *lhs, const struct pk_str *rhs);
+int pk_compare_cstr(const struct pk_cstr *lhs, const struct pk_cstr *rhs);
+
+#endif /* PK_STR_H */
+
+#ifdef PK_IMPLEMENTATION
+
+#include "./pkmacros.h"
+
+#include <string.h>
+
+struct pk_str
+cstring_to_pk_str(char *s)
+{
+ return (struct pk_str) {
+ .val = s,
+ .length = (uint32_t)(strlen(s)),
+ .reserved = 0,
+ };
+}
+
+struct pk_cstr
+cstring_to_pk_cstr(const char *s)
+{
+ return (struct pk_cstr) {
+ .val = s,
+ .length = (uint32_t)(strlen(s)),
+ .reserved = 0,
+ };
+}
+
+struct pk_str
+pk_cstr_to_pk_str(const struct pk_cstr *s)
+{
+ return (struct pk_str) {
+ .val = (char *)(s->val),
+ .length = s->length,
+ .reserved = s->reserved,
+ };
+}
+
+struct pk_cstr
+pk_str_to_pk_cstr(const struct pk_str *s)
+{
+ return (struct pk_cstr) {
+ .val = (char *)(s->val),
+ .length = s->length,
+ .reserved = s->reserved,
+ };
+}
+
+int
+pk_compare_str(const struct pk_str *lhs, const struct pk_str *rhs)
+{
+ return strncmp(lhs->val, rhs->val, PK_MIN(lhs->length, rhs->length));
+}
+
+int
+pk_compare_cstr(const struct pk_cstr *lhs, const struct pk_cstr *rhs)
+{
+ return strncmp(lhs->val, rhs->val, PK_MIN(lhs->length, rhs->length));
+}
+
+#endif /* PK_IMPLEMENTATION */
diff --git a/test/pkstr.c b/test/pkstr.c
new file mode 100644
index 0000000..7b91fe7
--- /dev/null
+++ b/test/pkstr.c
@@ -0,0 +1,56 @@
+
+#include "../pkstr.h"
+
+#include "../pkmacros.h"
+
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+ (void)stdout;
+
+ struct pk_str s;
+ struct pk_cstr cs;
+ struct pk_str os;
+ struct pk_cstr ocs;
+
+ // cstring_to_pk_str
+ {
+ s = cstring_to_pk_str("this is a test");
+ PK_LOGV_INF("cstring_to_pk_str: '%s' '%i' '%i'\n", s.val, s.length, s.reserved);
+ }
+
+ // cstring_to_pk_cstr
+ {
+ cs = cstring_to_pk_cstr("this is a const test");
+ PK_LOGV_INF("cstring_to_pk_cstr: '%s' '%i' '%i'\n", cs.val, cs.length, cs.reserved);
+ }
+
+ // pk_cstr_to_pk_str
+ {
+ os = pk_cstr_to_pk_str(&cs);
+ PK_LOGV_INF("pk_cstr_to_pk_str: '%s' '%i' '%i'\n", os.val, os.length, os.reserved);
+ }
+
+ // pk_str_to_pk_cstr
+ {
+ ocs = pk_str_to_pk_cstr(&s);
+ PK_LOGV_INF("pk_str_to_pk_cstr: '%s' '%i' '%i'\n", ocs.val, ocs.length, ocs.reserved);
+ }
+
+ // pk_compare_str
+ {
+ os = cstring_to_pk_str("this is a different test");
+ PK_LOGV_INF("pk_compare_str, ('%s' cmp '%s') = '%i'\n", s.val, os.val, pk_compare_str(&s, &os));
+ }
+
+ // pk_compare_cstr
+ {
+ ocs = cstring_to_pk_cstr("this is a different const test");
+ PK_LOGV_INF("pk_compare_cstr, ('%s' cmp '%s') = '%i'\n", ocs.val, cs.val, pk_compare_cstr(&ocs, &cs));
+ }
+
+ return 0;
+}
diff --git a/test/pkstr.cpp b/test/pkstr.cpp
new file mode 100644
index 0000000..58b60cd
--- /dev/null
+++ b/test/pkstr.cpp
@@ -0,0 +1,57 @@
+
+#include "../pkstr.h"
+
+#include "../pkmacros.h"
+
+#include <cstdio>
+#include <string>
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+ (void)stdout;
+
+ struct pk_str s;
+ struct pk_cstr cs;
+ struct pk_str os;
+ struct pk_cstr ocs;
+
+ // cstring_to_pk_str
+ {
+ s = cstring_to_pk_str((char *)"this is a test");
+ PK_LOGV_INF("cstring_to_pk_str: '%s' '%i' '%i'\n", s.val, s.length, s.reserved);
+ }
+
+ // cstring_to_pk_cstr
+ {
+ cs = cstring_to_pk_cstr("this is a const test");
+ PK_LOGV_INF("cstring_to_pk_cstr: '%s' '%i' '%i'\n", cs.val, cs.length, cs.reserved);
+ }
+
+ // pk_cstr_to_pk_str
+ {
+ os = pk_cstr_to_pk_str(&cs);
+ PK_LOGV_INF("pk_cstr_to_pk_str: '%s' '%i' '%i'\n", os.val, os.length, os.reserved);
+ }
+
+ // pk_str_to_pk_cstr
+ {
+ ocs = pk_str_to_pk_cstr(&s);
+ PK_LOGV_INF("pk_str_to_pk_cstr: '%s' '%i' '%i'\n", ocs.val, ocs.length, ocs.reserved);
+ }
+
+ // pk_compare_str
+ {
+ os = cstring_to_pk_str((char *)"this is a different test");
+ PK_LOGV_INF("pk_compare_str, ('%s' cmp '%s') = '%i'\n", s.val, os.val, pk_compare_str(&s, &os));
+ }
+
+ // pk_compare_cstr
+ {
+ ocs = cstring_to_pk_cstr("this is a different const test");
+ PK_LOGV_INF("pk_compare_cstr, ('%s' cmp '%s') = '%i'\n", ocs.val, cs.val, pk_compare_cstr(&ocs, &cs));
+ }
+
+ return 0;
+}