From: Andre Noll <maan@tuebingen.mpg.de>
Date: Thu, 2 Oct 2014 21:12:34 +0000 (+0000)
Subject: string.c: Simplify and improve for_each_line().
X-Git-Tag: v0.5.4~9
X-Git-Url: https://git.tue.mpg.de/?a=commitdiff_plain;h=e833dd12e607e9e5951a657d6e99f19adddefb7d;p=paraslash.git

string.c: Simplify and improve for_each_line().

Much faster for large buffers. Idea: Search for zero byte
only until next cr.
---

diff --git a/string.c b/string.c
index 453a61c1..d75dbac2 100644
--- a/string.c
+++ b/string.c
@@ -380,14 +380,13 @@ int for_each_line(unsigned flags, char *buf, size_t size,
 		char *next_cr;
 
 		next_cr = memchr(start, '\n', buf + size - start);
-		next_null = memchr(start, '\0', buf + size - start);
+		next_null = memchr(start, '\0', next_cr?
+			next_cr - start : buf + size - start);
 		if (!next_cr && !next_null)
 			break;
-		if (next_cr && next_null) {
-			end = next_cr < next_null? next_cr : next_null;
-		} else if (next_null) {
+		if (next_null)
 			end = next_null;
-		} else
+		else
 			end = next_cr;
 		num_lines++;
 		if (!(flags & FELF_DISCARD_FIRST) || start != buf) {