summaryrefslogtreecommitdiff
path: root/src/common.cc
diff options
context:
space:
mode:
authorFabrice <fabrice@schaub-dev.xyz>2026-03-03 09:42:37 +0100
committerFabrice <fabrice@schaub-dev.xyz>2026-03-03 09:42:37 +0100
commit3a9f7e6c9e1f44385c1950edc6267af8eea56a7e (patch)
treefbf7175fbaec5d9776d105a6488680c87a694a61 /src/common.cc
parent7ff9d2586a666bd420b98eb9f8de130cf57cd59d (diff)
formatting of tokens
Diffstat (limited to 'src/common.cc')
-rw-r--r--src/common.cc19
1 files changed, 9 insertions, 10 deletions
diff --git a/src/common.cc b/src/common.cc
index de2f6de..5e84c01 100644
--- a/src/common.cc
+++ b/src/common.cc
@@ -26,7 +26,7 @@ typedef intptr_t isize;
#define likely(cond) __builtin_expect(!!(cond), 1)
#define unlikely(cond) __builtin_expect(!!(cond), 0)
#define containerof(type, member, ptr) \
- ((type *)((char *)(ptr) - offsetof(type, member)))
+ ((type*)((char*)(ptr) - offsetof(type, member)))
/* error handling sort of */
[[noreturn]] void panic_impl(const char* file, i32 line, const char* fmt, ...) {
@@ -72,25 +72,24 @@ struct Slice {
}
const T* operator[](usize index) const {
- assert(index < this->size());
+ assert(index < this->total_length());
return this->ptr + index;
}
- inline usize size() const { return this->length * sizeof(T); }
+ inline usize total_length() const { return this->length * sizeof(T); }
};
-template <typename T>
-bool slice_write(const Slice<T>* slice, FILE* stream) {
- assert_neq(slice, nullptr);
+typedef Slice<const u8> String;
+
+bool string_write(const String* s, FILE* stream) {
+ assert_neq(s, nullptr);
assert_neq(stream, nullptr);
- usize rc = fwrite(slice->ptr, sizeof(T), slice->length, stream);
- if (rc == 0 || slice->size() > rc) return false;
+ i32 rc = fprintf(stream, "%.*s", (i32)s->length, s->ptr);
+ if (unlikely(rc < 0)) return false;
return true;
}
-typedef Slice<const u8> String;
-
/* linked list */
struct Link {