diff options
Diffstat (limited to 'src/common.cc')
| -rw-r--r-- | src/common.cc | 19 |
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 { |
