From ba5e902a1da77985921884de562fb1ec89dd3fc0 Mon Sep 17 00:00:00 2001
From: Abdelrahman <said.abdelrahman89@gmail.com>
Date: Sat, 22 Feb 2025 17:48:04 +0000
Subject: [PATCH] Add wapp_str8_push_back and wapp_str8_list_empty

---
 src/core/strings/str8/str8.c | 20 ++++++++++++++++++++
 src/core/strings/str8/str8.h |  2 ++
 tests/str8/test_str8.c       | 22 ++++++++++++++++++++++
 tests/str8/test_str8.h       |  1 +
 tests/str8/test_str8_list.c  | 16 ++++++++++++++++
 tests/str8/test_str8_list.h  |  1 +
 6 files changed, 62 insertions(+)

diff --git a/src/core/strings/str8/str8.c b/src/core/strings/str8/str8.c
index a465fbb..6abb626 100644
--- a/src/core/strings/str8/str8.c
+++ b/src/core/strings/str8/str8.c
@@ -120,6 +120,15 @@ void wapp_str8_set(Str8 *str, u64 index, c8 c) {
   str->buf[index] = c;
 }
 
+void wapp_str8_push_back(Str8 *str, c8 c) {
+  if (!(str->size < str->capacity)) {
+    return;
+  }
+
+  u64 index = (str->size)++;
+  wapp_str8_set(str, index, c);
+}
+
 bool wapp_str8_equal(Str8RO *s1, Str8RO *s2) {
   if (s1->size != s2->size) {
     return false;
@@ -608,6 +617,17 @@ RETURN_STR8_LIST_REMOVE:
   return output;
 }
 
+void wapp_str8_list_empty(Str8List *list) {
+  if (!list) {
+    return;
+  }
+
+  u64 count = list->node_count;
+  for (u64 i = 0; i < count; ++i) {
+    wapp_str8_list_pop_back(list);
+  }
+}
+
 internal Str8List node_to_list(Str8Node *node) {
   Str8List output = {.first = node, .last = node, .total_size = node->string->size, .node_count = 1};
 
diff --git a/src/core/strings/str8/str8.h b/src/core/strings/str8/str8.h
index f8632b1..800dafd 100644
--- a/src/core/strings/str8/str8.h
+++ b/src/core/strings/str8/str8.h
@@ -77,6 +77,7 @@ void wapp_str8_dealloc_buf(const Allocator *allocator, Str8 **str);
  */
 c8   wapp_str8_get(Str8RO *str, u64 index);
 void wapp_str8_set(Str8 *str, u64 index, c8 c);
+void wapp_str8_push_back(Str8 *str, c8 c);
 bool wapp_str8_equal(Str8RO *s1, Str8RO *s2);
 bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count);
 Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end);
@@ -113,6 +114,7 @@ void     wapp_str8_list_insert(Str8List *list, Str8Node *node, u64 index);
 Str8Node *wapp_str8_list_pop_front(Str8List *list);
 Str8Node *wapp_str8_list_pop_back(Str8List *list);
 Str8Node *wapp_str8_list_remove(Str8List *list, u64 index);
+void     wapp_str8_list_empty(Str8List *list);
 
 #ifdef __cplusplus
 END_C_LINKAGE
diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c
index 266c0f1..427e763 100644
--- a/tests/str8/test_str8.c
+++ b/tests/str8/test_str8.c
@@ -236,6 +236,28 @@ TestFuncResult test_str8_set(void) {
   return wapp_tester_result(result);
 }
 
+TestFuncResult test_str8_push_back(void) {
+  bool result;
+
+  Str8 expected = wapp_str8_lit("Abdelrahman");
+  Str8 buf      = wapp_str8_buf(64);
+  wapp_str8_push_back(&buf, 'A');
+  wapp_str8_push_back(&buf, 'b');
+  wapp_str8_push_back(&buf, 'd');
+  wapp_str8_push_back(&buf, 'e');
+  wapp_str8_push_back(&buf, 'l');
+  wapp_str8_push_back(&buf, 'r');
+  wapp_str8_push_back(&buf, 'a');
+  wapp_str8_push_back(&buf, 'h');
+  wapp_str8_push_back(&buf, 'm');
+  wapp_str8_push_back(&buf, 'a');
+  wapp_str8_push_back(&buf, 'n');
+
+  result = wapp_str8_equal(&buf, &expected);
+
+  return wapp_tester_result(result);
+}
+
 TestFuncResult test_str8_equal(void) {
   bool result;
 
diff --git a/tests/str8/test_str8.h b/tests/str8/test_str8.h
index 71d6b4c..a4837df 100644
--- a/tests/str8/test_str8.h
+++ b/tests/str8/test_str8.h
@@ -18,6 +18,7 @@ TestFuncResult test_str8_alloc_concat(void);
 TestFuncResult test_str8_get_index_within_bounds(void);
 TestFuncResult test_str8_get_index_out_of_bounds(void);
 TestFuncResult test_str8_set(void);
+TestFuncResult test_str8_push_back(void);
 TestFuncResult test_str8_equal(void);
 TestFuncResult test_str8_slice(void);
 TestFuncResult test_str8_concat_capped(void);
diff --git a/tests/str8/test_str8_list.c b/tests/str8/test_str8_list.c
index a5800b6..ed6dfc5 100644
--- a/tests/str8/test_str8_list.c
+++ b/tests/str8/test_str8_list.c
@@ -246,3 +246,19 @@ TestFuncResult test_str8_list_remove(void) {
 
   return wapp_tester_result(result);
 }
+
+TestFuncResult test_str8_list_empty(void) {
+  bool result;
+
+  Str8List list = {0};
+  wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("Hello"));
+  wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("from"));
+  wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("wizapp"));
+  wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("stdlib"));
+
+  wapp_str8_list_empty(&list);
+
+  result = list.first == NULL && list.last == NULL && list.node_count == 0 && list.total_size == 0;
+
+  return wapp_tester_result(result);
+}
diff --git a/tests/str8/test_str8_list.h b/tests/str8/test_str8_list.h
index 55df574..514e292 100644
--- a/tests/str8/test_str8_list.h
+++ b/tests/str8/test_str8_list.h
@@ -14,6 +14,7 @@ TestFuncResult test_str8_list_insert(void);
 TestFuncResult test_str8_list_pop_front(void);
 TestFuncResult test_str8_list_pop_back(void);
 TestFuncResult test_str8_list_remove(void);
+TestFuncResult test_str8_list_empty(void);
 
 #ifdef __cplusplus
 END_C_LINKAGE