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