From 4ab00506b3840d073b127733ab871e6a8bcbaec6 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Mon, 30 Mar 2020 13:28:07 -0400 Subject: all around bugfixes on buf.c --- buf.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/buf.c b/buf.c index a485708..df35d0f 100644 --- a/buf.c +++ b/buf.c @@ -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; } -- cgit