aboutsummaryrefslogtreecommitdiff
path: root/str.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-10-11 21:43:36 +1100
committerquou <quou@disroot.org>2024-10-11 21:43:36 +1100
commit24b72170d34cee515398f206f087bfeafc7b6b55 (patch)
tree24c2bc3d7e1c0bdac9de38dae3c5d2de7e669f25 /str.c
parent9add408984464bd6b3cc018bb14c3d69ad0a2898 (diff)
game is pretty much done kek
Diffstat (limited to 'str.c')
-rw-r--r--str.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/str.c b/str.c
new file mode 100644
index 0000000..e5f0cd6
--- /dev/null
+++ b/str.c
@@ -0,0 +1,88 @@
+#include "maths.h"
+#include "memory.h"
+#include "str.h"
+
+int string_equal(const char* a, const char* b) {
+ while (*a && *b) {
+ if (*a != *b) { return 0; }
+ a++; b++;
+ }
+ return 1;
+}
+
+int string_copy(char* dst, const char* src) {
+ int i;
+ for (i = 0; *src; src++, dst++, i++) {
+ *dst = *src;
+ }
+ return i;
+}
+
+int string_len(const char* s) {
+ int l;
+ for (l = 0; *s; s++, l++);
+ return l;
+}
+
+char* dup_string(Arena* a, const char* s) {
+ int size = string_len(s) + 1;
+ char* d = arena_alloc_aligned(a, size, 1);
+ string_copy(d, s);
+ return d;
+}
+
+int int_to_buf(int n, char* buf) {
+ int i, sign, t;
+ unsigned n1;
+
+ if(n == 0) {
+ buf[0] = '0';
+ buf[1] = '\0';
+ return 1;
+ }
+
+ i = 0;
+ sign = n < 0;
+
+ n1 = sign ? -n : n;
+
+ while (n1 != 0) {
+ buf[i++] = n1 % 10 + '0';
+ n1 =n1 / 10;
+ }
+
+ if(sign) {
+ buf[i++] = '-';
+ }
+
+ buf[i] = '\0';
+
+ for (t = 0; t < i/2; t++) {
+ buf[t] ^= buf[i-t-1];
+ buf[i-t-1] ^= buf[t];
+ buf[t] ^= buf[i-t-1];
+ }
+
+ return i;
+}
+
+int f_to_buf(int f, char* buf) {
+ int frac, w, i;
+
+ frac = (((f) % (1 << fbits)) * 100) / (1 << fbits);
+ w = f >> fbits;
+
+ i = 0;
+
+ if (f < 0) {
+ buf[i] = '-';
+ i++;
+ }
+
+ i += int_to_buf(absolute(w), buf + i);
+ buf[i] = '.';
+ i++;
+ i += int_to_buf(absolute(frac), buf + i);
+
+ return i;
+}