Finding \r\n without strstr is a little harder

This commit is contained in:
Pieter Noordhuis 2010-11-04 23:52:47 +01:00
parent 8b616d3547
commit 8ce0b3228d
2 changed files with 21 additions and 1 deletions

View File

@ -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;
}

11
test.c
View File

@ -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() {