[commonlib.h] Implemented c_pop_front...
authorahmedsamyh <ahmedsamyh10@gmail.com>
Tue, 25 Feb 2025 10:30:12 +0000 (15:30 +0500)
committerahmedsamyh <ahmedsamyh10@gmail.com>
Tue, 25 Feb 2025 10:30:12 +0000 (15:30 +0500)
- Make c_shift_args -> c_pop_front.

commonlib.h

index 08f889a1ccdb89812fc9eb13c0a1b4c9a964a66b..b147ffb5e45197c20675322148bb8779fd6b5a01 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdlib.h>\r
 #include <string.h>\r
 #include <ctype.h>\r
+#include <assert.h>\r
 \r
 // Memory allocation\r
 #define C_MALLOC malloc\r
@@ -67,6 +68,8 @@ typedef wchar_t wchar;
 typedef const char*  cstr;\r
 typedef const wchar* wstr;\r
 \r
+\r
+// Macros\r
 #define c_ASSERT(condition, msg) do {\\r
                 if (!(condition)) {\\r
                         fprintf(stderr, "%s:%d:0 [ASSERTION FAILED] %s: %s", __FILE__, __LINE__, #condition, msg);\\r
@@ -76,6 +79,9 @@ typedef const wchar* wstr;
 \r
 #define c_ARRAY_LEN(arr) (sizeof(arr) / sizeof(arr[0]))\r
 \r
+#define c_pop_front(xs, xsz) (assert(xsz > 0 && "Array is empty"), xsz--, *xs++)\r
+#define c_shift_args c_pop_front\r
+\r
 //\r
 // Struct pre-decls\r
 //\r
@@ -125,6 +131,8 @@ typedef struct c_Arena c_Arena;
                (da).items[(da).count++] = elm;\\r
        } while (0)\r
 \r
+#define c_da_pop_front(da) (c_ASSERT(da.count > 0, "Array is empty"), da.count--, *da.items++)\r
+\r
 //\r
 // OS\r
 //\r
@@ -235,12 +243,6 @@ bool c_sv_contains_char(c_String_view sv, char ch);
 bool c_sv_is_hex_numbers(c_String_view sv);\r
 bool c_sv_equals(c_String_view sv1, c_String_view sv2);\r
 \r
-//\r
-// Args\r
-//\r
-\r
-cstr c_shift_args(int* argc, char*** argv);\r
-\r
 #endif /* _COMMONLIB_H_ */\r
 \r
 //////////////////////////////////////////////////\r
@@ -617,18 +619,4 @@ bool c_sv_equals(c_String_view sv1, c_String_view sv2) {
     return true;\r
 }\r
 \r
-\r
-//\r
-// Args\r
-//\r
-\r
-cstr c_shift_args(int* argc, char*** argv) {\r
-    if (*argc <= 0) return NULL;\r
-\r
-    cstr res = *(argv)[0];\r
-    *argv = (*argv) + 1;\r
-    *argc = (*argc) - 1;\r
-    return res;\r
-}\r
-\r
 #endif\r