aboutsummaryrefslogtreecommitdiff
path: root/vendor/librune/lib/builder
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-02-13 13:02:28 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-02-13 13:11:47 +0100
commit79e6af86ca526d5fb56af6f6ca3da713e3a5e9f9 (patch)
tree752f1c26d1f122dcf58374ac78db109c9578be45 /vendor/librune/lib/builder
Genesis commit
Diffstat (limited to 'vendor/librune/lib/builder')
-rw-r--r--vendor/librune/lib/builder/u8strfit.c11
-rw-r--r--vendor/librune/lib/builder/u8strfree.c9
-rw-r--r--vendor/librune/lib/builder/u8strgrow.c41
-rw-r--r--vendor/librune/lib/builder/u8strinit.c18
-rw-r--r--vendor/librune/lib/builder/u8strpushr.c13
-rw-r--r--vendor/librune/lib/builder/u8strpushstr.c17
-rw-r--r--vendor/librune/lib/builder/u8strpushu8.c16
7 files changed, 125 insertions, 0 deletions
diff --git a/vendor/librune/lib/builder/u8strfit.c b/vendor/librune/lib/builder/u8strfit.c
new file mode 100644
index 0000000..d0f0ecb
--- /dev/null
+++ b/vendor/librune/lib/builder/u8strfit.c
@@ -0,0 +1,11 @@
+#include <stdlib.h>
+
+#include "builder.h"
+
+#include "internal/common.h"
+
+struct u8str *
+u8strfit(struct u8str *b)
+{
+ return (b->p = realloc(b->p, b->len)) ? b : nullptr;
+}
diff --git a/vendor/librune/lib/builder/u8strfree.c b/vendor/librune/lib/builder/u8strfree.c
new file mode 100644
index 0000000..506c71b
--- /dev/null
+++ b/vendor/librune/lib/builder/u8strfree.c
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+
+#include "builder.h"
+
+void
+u8strfree(struct u8str b)
+{
+ free(b.p);
+}
diff --git a/vendor/librune/lib/builder/u8strgrow.c b/vendor/librune/lib/builder/u8strgrow.c
new file mode 100644
index 0000000..022b216
--- /dev/null
+++ b/vendor/librune/lib/builder/u8strgrow.c
@@ -0,0 +1,41 @@
+#include <stdlib.h>
+
+#include "builder.h"
+
+#include "internal/common.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/vendor/librune/lib/builder/u8strinit.c b/vendor/librune/lib/builder/u8strinit.c
new file mode 100644
index 0000000..29947e8
--- /dev/null
+++ b/vendor/librune/lib/builder/u8strinit.c
@@ -0,0 +1,18 @@
+#include <stdlib.h>
+
+#include "builder.h"
+
+#include "internal/common.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/vendor/librune/lib/builder/u8strpushr.c b/vendor/librune/lib/builder/u8strpushr.c
new file mode 100644
index 0000000..3fe31f2
--- /dev/null
+++ b/vendor/librune/lib/builder/u8strpushr.c
@@ -0,0 +1,13 @@
+#include "builder.h"
+#include "mbstring.h"
+
+#include "internal/common.h"
+
+struct u8str *
+u8strpushr(struct u8str *b, rune ch)
+{
+ if (!u8strgrow(b, b->len + u8wdth(ch)))
+ return nullptr;
+ b->len += rtou8(b->p + b->len, ch, b->cap - b->len);
+ return b;
+}
diff --git a/vendor/librune/lib/builder/u8strpushstr.c b/vendor/librune/lib/builder/u8strpushstr.c
new file mode 100644
index 0000000..8329c81
--- /dev/null
+++ b/vendor/librune/lib/builder/u8strpushstr.c
@@ -0,0 +1,17 @@
+#include <string.h>
+
+#include "builder.h"
+#include "mbstring.h"
+
+#include "internal/common.h"
+
+struct u8str *
+u8strpushstr(struct u8str *b, const char *s)
+{
+ size_t n = strlen(s);
+ if (!u8strgrow(b, b->len + n))
+ return nullptr;
+ memcpy(b->p + b->len, s, n);
+ b->len += n;
+ return b;
+}
diff --git a/vendor/librune/lib/builder/u8strpushu8.c b/vendor/librune/lib/builder/u8strpushu8.c
new file mode 100644
index 0000000..2ef2563
--- /dev/null
+++ b/vendor/librune/lib/builder/u8strpushu8.c
@@ -0,0 +1,16 @@
+#include <string.h>
+
+#include "builder.h"
+#include "mbstring.h"
+
+#include "internal/common.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;
+}