| ... | ... | @@ -15,6 +15,8 @@ |
| 15 | 15 | |
| 16 | 16 | #define BUF_INIT {{0}} |
| 17 | 17 | |
| 18 | // Note, you must call one of the alloc, init, or resize functions to have an |
| 19 | // initialized buffer. The assertions should help with this. |
| 18 | 20 | struct Buf { |
| 19 | 21 | ZigList<char> list; |
| 20 | 22 | }; |
| ... | ... | @@ -23,10 +25,12 @@ Buf *buf_sprintf(const char *format, ...) |
| 23 | 25 | __attribute__ ((format (printf, 1, 2))); |
| 24 | 26 | |
| 25 | 27 | static inline int buf_len(Buf *buf) { |
| 28 | assert(buf->list.length); |
| 26 | 29 | return buf->list.length - 1; |
| 27 | 30 | } |
| 28 | 31 | |
| 29 | 32 | static inline char *buf_ptr(Buf *buf) { |
| 33 | assert(buf->list.length); |
| 30 | 34 | return buf->list.items; |
| 31 | 35 | } |
| 32 | 36 | |
| ... | ... | @@ -76,6 +80,7 @@ static inline Buf *buf_create_from_str(const char *str) { |
| 76 | 80 | } |
| 77 | 81 | |
| 78 | 82 | static inline Buf *buf_slice(Buf *in_buf, int start, int end) { |
| 83 | assert(in_buf->list.length); |
| 79 | 84 | assert(start >= 0); |
| 80 | 85 | assert(end >= 0); |
| 81 | 86 | assert(start < buf_len(in_buf)); |
| ... | ... | @@ -88,6 +93,7 @@ static inline Buf *buf_slice(Buf *in_buf, int start, int end) { |
| 88 | 93 | } |
| 89 | 94 | |
| 90 | 95 | static inline void buf_append_mem(Buf *buf, const char *mem, int mem_len) { |
| 96 | assert(buf->list.length); |
| 91 | 97 | assert(mem_len >= 0); |
| 92 | 98 | int old_len = buf_len(buf); |
| 93 | 99 | buf_resize(buf, old_len + mem_len); |
| ... | ... | @@ -96,14 +102,17 @@ static inline void buf_append_mem(Buf *buf, const char *mem, int mem_len) { |
| 96 | 102 | } |
| 97 | 103 | |
| 98 | 104 | static inline void buf_append_str(Buf *buf, const char *str) { |
| 105 | assert(buf->list.length); |
| 99 | 106 | buf_append_mem(buf, str, strlen(str)); |
| 100 | 107 | } |
| 101 | 108 | |
| 102 | 109 | static inline void buf_append_buf(Buf *buf, Buf *append_buf) { |
| 110 | assert(buf->list.length); |
| 103 | 111 | buf_append_mem(buf, buf_ptr(append_buf), buf_len(append_buf)); |
| 104 | 112 | } |
| 105 | 113 | |
| 106 | 114 | static inline void buf_append_char(Buf *buf, uint8_t c) { |
| 115 | assert(buf->list.length); |
| 107 | 116 | buf_append_mem(buf, (const char *)&c, 1); |
| 108 | 117 | } |
| 109 | 118 | |
| ... | ... | @@ -111,20 +120,25 @@ void buf_appendf(Buf *buf, const char *format, ...) |
| 111 | 120 | __attribute__ ((format (printf, 2, 3))); |
| 112 | 121 | |
| 113 | 122 | static inline bool buf_eql_mem(Buf *buf, const char *mem, int mem_len) { |
| 123 | assert(buf->list.length); |
| 114 | 124 | if (buf_len(buf) != mem_len) |
| 115 | 125 | return false; |
| 116 | 126 | return memcmp(buf_ptr(buf), mem, mem_len) == 0; |
| 117 | 127 | } |
| 118 | 128 | |
| 119 | 129 | static inline bool buf_eql_str(Buf *buf, const char *str) { |
| 130 | assert(buf->list.length); |
| 120 | 131 | return buf_eql_mem(buf, str, strlen(str)); |
| 121 | 132 | } |
| 122 | 133 | |
| 123 | 134 | static inline bool buf_eql_buf(Buf *buf, Buf *other) { |
| 135 | assert(buf->list.length); |
| 124 | 136 | return buf_eql_mem(buf, buf_ptr(other), buf_len(other)); |
| 125 | 137 | } |
| 126 | 138 | |
| 127 | 139 | static inline void buf_splice_buf(Buf *buf, int start, int end, Buf *other) { |
| 140 | assert(buf->list.length); |
| 141 | |
| 128 | 142 | if (start != end) |
| 129 | 143 | zig_panic("TODO buf_splice_buf"); |
| 130 | 144 | |
| ... | ... | @@ -135,6 +149,7 @@ static inline void buf_splice_buf(Buf *buf, int start, int end, Buf *other) { |
| 135 | 149 | } |
| 136 | 150 | |
| 137 | 151 | static inline uint32_t buf_hash(Buf *buf) { |
| 152 | assert(buf->list.length); |
| 138 | 153 | // FNV 32-bit hash |
| 139 | 154 | uint32_t h = 2166136261; |
| 140 | 155 | for (int i = 0; i < buf_len(buf); i += 1) { |