diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-03-30 13:28:07 -0400 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-03-30 13:28:07 -0400 |
commit | 4ab00506b3840d073b127733ab871e6a8bcbaec6 (patch) | |
tree | 45d4bc92ca1f32a746108a9efe9a7594440d7091 | |
parent | 7288881e19afbcce382523788ae2bd2bf7b2b5b2 (diff) |
all around bugfixes on buf.c
-rw-r--r-- | buf.c | 24 |
1 files changed, 10 insertions, 14 deletions
@@ -34,9 +34,9 @@ buf* newbuf(size_t cap){ size_t buflen(buf* buffer){ buf_node* cur = buffer->tail; - size_t size = buffer->ins-buffer->pop+buffer->cap; + size_t size = buffer->ins-buffer->pop; while (cur != buffer->head) { - cur = cur->next; // skips one iter ^^^ + cur = cur->next; size += buffer->cap; }; return size; @@ -74,8 +74,8 @@ char* popstrbuf(buf* buffer, size_t ct){ void inschrbuf(buf* buffer, char chr){ if (buffer->ins == buffer->cap) extendbuf(buffer); - buffer->cap++; - buffer->head->text[buffer->cap-1] = chr; + buffer->ins++; + buffer->head->text[buffer->ins-1] = chr; } void insstrbuf(buf* buffer, char* str){ size_t len = strlen(str); @@ -93,17 +93,13 @@ char* peekstrbuf(buf* buffer, size_t loc, size_t len){ char* str = malloc(sizeof(char)*(len+1)); str[len] = 0; buf_node* start; - loc += buffer->ins; - for (start = buffer->tail; loc>0; - loc -= buffer->cap) + loc += buffer->pop; + for (start = buffer->tail; loc>buffer->cap; loc -= buffer->cap) start = start->next; // start,loc is where one starts streaming - if (len > buffer->cap){ - memcpy(str, start->text+loc, buffer->cap-loc); - len -= buffer->cap - loc; + while (len > 0){ + memcpy(str, start->text+loc, min(buffer->cap-loc, len)); + len -= min(buffer->cap - loc, len); + loc = 0; } - for (; len > buffer->cap; len -= buffer->cap ){ - memcpy(str, start->text, buffer->cap); - } - memcpy(str, start->text, len); return str; } |