Skip to content

Commit

Permalink
WIP: buffer: improve 'buffer_consume_until' (#32)
Browse files Browse the repository at this point in the history
Use memchr() instead of iterating through the loop, which is slow.
  • Loading branch information
charlievieth authored Jan 25, 2019
1 parent 848f91d commit ff45ede
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ int buffer_consume(buffer_t *b, size_t amt)

int buffer_consume_until(buffer_t *b, char token)
{
for (;;) {
char* at_head = buffer_head(b);
int c = buffer_consume(b, 1);
if (at_head[0] == token || c == -1)
break;
char *head = buffer_head(b);
const size_t len = buffer_datacount(b);
char *p = memchr(head, token, len);
if (p != NULL) {
buffer_consume(b, p - head + 1);
} else {
buffer_consume(b, len);
}
return 0;
}
Expand Down

0 comments on commit ff45ede

Please sign in to comment.