aboutsummaryrefslogtreecommitdiff
path: root/lib/bob
diff options
context:
space:
mode:
Diffstat (limited to 'lib/bob')
-rw-r--r--lib/bob/u8strfit.c9
-rw-r--r--lib/bob/u8strfree.c9
-rw-r--r--lib/bob/u8strgrow.c39
-rw-r--r--lib/bob/u8strinit.c16
-rw-r--r--lib/bob/u8strpushr.c11
-rw-r--r--lib/bob/u8strpushstr.c9
-rw-r--r--lib/bob/u8strpushu8.c14
7 files changed, 107 insertions, 0 deletions
diff --git a/lib/bob/u8strfit.c b/lib/bob/u8strfit.c
new file mode 100644
index 0000000..332a5d7
--- /dev/null
+++ b/lib/bob/u8strfit.c
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+
+#include "bob.h"
+
+struct u8str *
+u8strfit(struct u8str *b)
+{
+ return (b->p = realloc(b->p, b->len)) ? b : nullptr;
+}
diff --git a/lib/bob/u8strfree.c b/lib/bob/u8strfree.c
new file mode 100644
index 0000000..7e25ca8
--- /dev/null
+++ b/lib/bob/u8strfree.c
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+
+#include "bob.h"
+
+void
+u8strfree(struct u8str b)
+{
+ free(b.p);
+}
diff --git a/lib/bob/u8strgrow.c b/lib/bob/u8strgrow.c
new file mode 100644
index 0000000..f1f86d3
--- /dev/null
+++ b/lib/bob/u8strgrow.c
@@ -0,0 +1,39 @@
+#include <stdlib.h>
+
+#include "bob.h"
+
+static size_t nextpow2(size_t);
+
+struct u8str *
+u8strgrow(struct u8str *b, size_t n)
+{
+ if (n > b->cap) {
+ b->cap = nextpow2(n);
+ if (!(b->p = realloc(b->p, b->cap)))
+ return nullptr;
+ }
+ return b;
+}
+
+size_t
+nextpow2(size_t x)
+{
+#if defined(__has_builtin) && __has_builtin(__builtin_clzl)
+ x = x <= 1 ? 1 : 1 << (64 - __builtin_clzl(x - 1));
+#else
+ if (x) {
+ x--;
+ x |= x >> 1;
+ x |= x >> 2;
+ x |= x >> 4;
+ x |= x >> 8;
+ if (sizeof(size_t) >= 4)
+ x |= x >> 16;
+ if (sizeof(size_t) >= 8)
+ x |= x >> 32;
+ }
+ x++;
+#endif
+
+ return x;
+}
diff --git a/lib/bob/u8strinit.c b/lib/bob/u8strinit.c
new file mode 100644
index 0000000..60423c1
--- /dev/null
+++ b/lib/bob/u8strinit.c
@@ -0,0 +1,16 @@
+#include <stdlib.h>
+
+#include "bob.h"
+
+struct u8str *
+u8strinit(struct u8str *b, size_t n)
+{
+ if (n) {
+ if (!(b->p = malloc(n)))
+ return nullptr;
+ } else
+ b->p = nullptr;
+ b->len = 0;
+ b->cap = n;
+ return b;
+}
diff --git a/lib/bob/u8strpushr.c b/lib/bob/u8strpushr.c
new file mode 100644
index 0000000..6fe5fd9
--- /dev/null
+++ b/lib/bob/u8strpushr.c
@@ -0,0 +1,11 @@
+#include "bob.h"
+#include "mbstring.h"
+
+struct u8str *
+u8strpushr(struct u8str *b, rune ch)
+{
+ if (!u8strgrow(b, b->len + rtou8(nullptr, ch, 0)))
+ return nullptr;
+ b->len += rtou8(b->p + b->len, ch, b->cap - b->len);
+ return b;
+}
diff --git a/lib/bob/u8strpushstr.c b/lib/bob/u8strpushstr.c
new file mode 100644
index 0000000..64b123d
--- /dev/null
+++ b/lib/bob/u8strpushstr.c
@@ -0,0 +1,9 @@
+#include <string.h>
+
+#include "bob.h"
+
+struct u8str *
+u8strpushstr(struct u8str *b, const char *s)
+{
+ return u8strpushu8(b, (struct u8view){.p = s, .len = strlen(s)});
+}
diff --git a/lib/bob/u8strpushu8.c b/lib/bob/u8strpushu8.c
new file mode 100644
index 0000000..8358e01
--- /dev/null
+++ b/lib/bob/u8strpushu8.c
@@ -0,0 +1,14 @@
+#include <string.h>
+
+#include "bob.h"
+#include "mbstring.h"
+
+struct u8str *
+u8strpushu8(struct u8str *b, struct u8view v)
+{
+ if (!u8strgrow(b, b->len + v.len))
+ return nullptr;
+ memcpy(b->p + b->len, v.p, v.len);
+ b->len += v.len;
+ return b;
+}