summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Smith <aes7mv@virginia.edu>2020-10-15 19:17:58 -0400
committermichael-grunder <michael.grunder@gmail.com>2021-02-25 21:25:17 -0800
commit397fe2630170e33eee06533749b1802f9371cb3e (patch)
tree07cd533f1ba4ea44449a75c7774a3be38c2e0002
parent81c48a9821e3350191ed6a1e0a3ed575d75df35d (diff)
read: Use memchr() in seekNewline() instead of looping over entire string
-rw-r--r--read.c31
1 files changed, 12 insertions, 19 deletions
diff --git a/read.c b/read.c
index 1378671..22d6e47 100644
--- a/read.c
+++ b/read.c
@@ -123,29 +123,22 @@ static char *readBytes(redisReader *r, unsigned int bytes) {
/* Find pointer to \r\n. */
static char *seekNewline(char *s, size_t len) {
- int pos = 0;
+ char *_s = s, *ret;
int _len = len-1;
- /* Position should be < len-1 because the character at "pos" should be
- * followed by a \n. Note that strchr cannot be used because it doesn't
- * allow to search a limited length and the buffer that is being searched
- * might not have a trailing NULL character. */
- while (pos < _len) {
- while(pos < _len && s[pos] != '\r') pos++;
- if (pos==_len) {
- /* Not found. */
- return NULL;
- } else {
- if (s[pos+1] == '\n') {
- /* Found. */
- return s+pos;
- } else {
- /* Continue searching. */
- pos++;
- }
+ /* Exclude the last character from the searched length because the found
+ * '\r' should be followed by a '\n' */
+ while ((ret = memchr(_s, '\r', _len)) != NULL) {
+ if (ret[1] == '\n') {
+ /* Found. */
+ break;
}
+ /* Continue searching. */
+ ret++;
+ _len -= ret - _s;
+ _s = ret;
}
- return NULL;
+ return ret;
}
/* Convert a string into a long long. Returns REDIS_OK if the string could be