summaryrefslogtreecommitdiff
path: root/src/dged/s8.c
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2024-09-17 08:47:03 +0200
committerAlbert Cervin <albert@acervin.com>2025-11-01 22:11:14 +0100
commit4459b8b3aa9d73895391785a99dcc87134e80601 (patch)
treea5204f447a0b2b05f63504c7fe958ef9bbf1918a /src/dged/s8.c
parent4689f3f38277bb64981fc960e8e384e2d065d659 (diff)
downloaddged-4459b8b3aa9d73895391785a99dcc87134e80601.tar.gz
dged-4459b8b3aa9d73895391785a99dcc87134e80601.tar.xz
dged-4459b8b3aa9d73895391785a99dcc87134e80601.zip
More lsp support
This makes the LSP support complete for now: - Completion - Diagnostics - Goto implementation/declaration - Rename - Documentation - Find references
Diffstat (limited to 'src/dged/s8.c')
-rw-r--r--src/dged/s8.c76
1 files changed, 73 insertions, 3 deletions
diff --git a/src/dged/s8.c b/src/dged/s8.c
index 71b6c6d..e641b11 100644
--- a/src/dged/s8.c
+++ b/src/dged/s8.c
@@ -1,17 +1,66 @@
#include "s8.h"
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+struct s8 s8new(const char *s, uint32_t len) {
+ uint8_t *mem = calloc(len, 1);
+ memcpy(mem, s, len);
+ return (struct s8){
+ .s = mem,
+ .l = len,
+ };
+}
+
+void s8delete(struct s8 s) {
+ if (s.s != NULL) {
+ free(s.s);
+ }
+ s.l = 0;
+ s.s = NULL;
+}
+
+struct s8 s8from_fmt(const char *fmt, ...) {
+ va_list args;
+
+ va_start(args, fmt);
+ ssize_t len = vsnprintf(NULL, 0, fmt, args);
+ va_end(args);
+
+ if (len == -1) {
+ return (struct s8){
+ .s = NULL,
+ .l = 0,
+ };
+ }
+
+ char *buf = calloc(len + 1, 1);
+
+ va_list args2;
+ va_start(args2, fmt);
+ vsnprintf(buf, len + 1, fmt, args2);
+ va_end(args2);
+
+ return (struct s8){
+ .s = (uint8_t *)buf,
+ .l = len,
+ };
+}
+
bool s8eq(struct s8 s1, struct s8 s2) {
return s1.l == s2.l && memcmp(s1.s, s2.s, s1.l) == 0;
}
int s8cmp(struct s8 s1, struct s8 s2) {
if (s1.l < s2.l) {
- return memcmp(s1.s, s2.s, s1.l);
+ int res = memcmp(s1.s, s2.s, s1.l);
+ return res == 0 ? -s2.s[s1.l] : res;
} else if (s2.l < s1.l) {
- return memcmp(s1.s, s2.s, s2.l);
+ int res = memcmp(s1.s, s2.s, s2.l);
+ return res == 0 ? s1.s[s2.l] : res;
}
return memcmp(s1.s, s2.s, s1.l);
@@ -25,13 +74,22 @@ char *s8tocstr(struct s8 s) {
}
bool s8startswith(struct s8 s, struct s8 prefix) {
- if (prefix.l > s.l) {
+ if (prefix.l == 0 || prefix.l > s.l) {
return false;
}
return memcmp(s.s, prefix.s, prefix.l) == 0;
}
+bool s8endswith(struct s8 s, struct s8 suffix) {
+ if (suffix.l > s.l) {
+ return false;
+ }
+
+ size_t ldiff = s.l - suffix.l;
+ return memcmp(s.s + ldiff, suffix.s, suffix.l) == 0;
+}
+
struct s8 s8dup(struct s8 s) {
struct s8 new = {0};
new.l = s.l;
@@ -41,3 +99,15 @@ struct s8 s8dup(struct s8 s) {
return new;
}
+
+bool s8empty(struct s8 s) { return s.s == NULL || s.l == 0; }
+
+bool s8onlyws(struct s8 s) {
+ for (size_t i = 0; i < s.l; ++i) {
+ if (!isspace(s.s[i])) {
+ return false;
+ }
+ }
+
+ return true;
+}