Skip to content

Commit

Permalink
Finding \r\n without strstr is a little harder
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed Nov 4, 2010
1 parent 8b616d3 commit 8ce0b32
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion hiredis.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,17 @@ static char *readBytes(redisReader *r, unsigned int bytes) {

static char *seekNewline(char *s) {
/* Find pointer to \r\n without strstr */
while(s != NULL && s[0] != '\r' && s[1] != '\n')
while (s != NULL) {
s = strchr(s,'\r');
if (s != NULL) {
if (s[1] == '\n')
break;
else
s++;
} else {
break;
}
}
return s;
}

Expand Down
11 changes: 11 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ static void test_reply_reader() {
ret = redisReplyReaderGetReply(reader,&reply);
test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
redisReplyReaderFree(reader);

test("Works when a single newline (\\r\\n) covers two calls to feed: ");
reader = redisReplyReaderCreate();
redisReplyReaderSetReplyObjectFunctions(reader,NULL);
redisReplyReaderFeed(reader,(char*)"+OK\r",4);
ret = redisReplyReaderGetReply(reader,&reply);
assert(ret == REDIS_OK && reply == NULL);
redisReplyReaderFeed(reader,(char*)"\n",1);
ret = redisReplyReaderGetReply(reader,&reply);
test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
redisReplyReaderFree(reader);
}

static void test_throughput() {
Expand Down

0 comments on commit 8ce0b32

Please sign in to comment.