Lubomir Rintel 0efa5e
From 0c56b664a73764ed01607f47731c8e4607f478d5 Mon Sep 17 00:00:00 2001
Lubomir Rintel 0efa5e
From: Lubomir Rintel <lkundrak@v3.sk>
Lubomir Rintel 0efa5e
Date: Sun, 23 Nov 2008 17:25:57 +0100
Lubomir Rintel 0efa5e
Subject: [PATCH] Fix line wrapping in PCRE backend
Lubomir Rintel 0efa5e
Lubomir Rintel 0efa5e
PCRE can't limit the matching to space between newlines (i.e
Lubomir Rintel 0efa5e
[^a] will allways match newline, see pcreposix(3) for details),
Lubomir Rintel 0efa5e
therefore whe have to split the buffer into lines and match each
Lubomir Rintel 0efa5e
line in the buffer separately.
Lubomir Rintel 0efa5e
Lubomir Rintel 0efa5e
Original ticket: https://bugzilla.redhat.com/show_bug.cgi?id=324781
Lubomir Rintel 0efa5e
---
Lubomir Rintel 0efa5e
 src/search.c |   33 ++++++++++++++++++++++++++++-----
Lubomir Rintel 0efa5e
 1 files changed, 28 insertions(+), 5 deletions(-)
Lubomir Rintel 0efa5e
Lubomir Rintel 0efa5e
diff --git a/src/search.c b/src/search.c
Lubomir Rintel 0efa5e
index 0b3e0e8..7f5f187 100644
Lubomir Rintel 0efa5e
--- a/src/search.c
Lubomir Rintel 0efa5e
+++ b/src/search.c
Lubomir Rintel 0efa5e
@@ -689,9 +689,32 @@ EXECUTE_FCT(Pexecute)
Lubomir Rintel 0efa5e
      is just for performance improvement in pcre_exec.  */
Lubomir Rintel 0efa5e
   int sub[300];
Lubomir Rintel 0efa5e
 
Lubomir Rintel 0efa5e
-  int e = pcre_exec (cre, extra, buf, size,
Lubomir Rintel 0efa5e
-		     start_ptr ? (start_ptr - buf) : 0, 0,
Lubomir Rintel 0efa5e
-		     sub, sizeof sub / sizeof *sub);
Lubomir Rintel 0efa5e
+  char *line_buf = buf;
Lubomir Rintel 0efa5e
+  int line_size = 0;
Lubomir Rintel 0efa5e
+  int e = 0;
Lubomir Rintel 0efa5e
+
Lubomir Rintel 0efa5e
+  /* PCRE can't limit the matching to space between newlines (i.e
Lubomir Rintel 0efa5e
+     [^a] will allways match newline, see pcreposix(3) for details),
Lubomir Rintel 0efa5e
+     therefore whe have to match each line in the buffer separately */
Lubomir Rintel 0efa5e
+  do {
Lubomir Rintel 0efa5e
+    /* We're not at the of buffer or end of line, get another char */
Lubomir Rintel 0efa5e
+    if (line_buf + line_size < buf + size && line_buf[line_size++] != eolbyte) {
Lubomir Rintel 0efa5e
+      continue;
Lubomir Rintel 0efa5e
+    }
Lubomir Rintel 0efa5e
+
Lubomir Rintel 0efa5e
+    /* Match the part of buffer that constitutes a line */
Lubomir Rintel 0efa5e
+    e = pcre_exec (cre, extra, line_buf, line_size - 1,
Lubomir Rintel 0efa5e
+		   start_ptr ? (start_ptr - buf) : 0, 0,
Lubomir Rintel 0efa5e
+		   sub, sizeof sub / sizeof *sub);
Lubomir Rintel 0efa5e
+
Lubomir Rintel 0efa5e
+    /* Don't try other lines if this one matched or returned an error */
Lubomir Rintel 0efa5e
+    if (e != PCRE_ERROR_NOMATCH)
Lubomir Rintel 0efa5e
+      break;
Lubomir Rintel 0efa5e
+
Lubomir Rintel 0efa5e
+    /* Wrap up */
Lubomir Rintel 0efa5e
+    line_buf += line_size;
Lubomir Rintel 0efa5e
+    line_size = 0;
Lubomir Rintel 0efa5e
+  } while (line_buf < buf + size);
Lubomir Rintel 0efa5e
 
Lubomir Rintel 0efa5e
   if (e <= 0)
Lubomir Rintel 0efa5e
     {
Lubomir Rintel 0efa5e
@@ -710,8 +733,8 @@ EXECUTE_FCT(Pexecute)
Lubomir Rintel 0efa5e
   else
Lubomir Rintel 0efa5e
     {
Lubomir Rintel 0efa5e
       /* Narrow down to the line we've found.  */
Lubomir Rintel 0efa5e
-      char const *beg = buf + sub[0];
Lubomir Rintel 0efa5e
-      char const *end = buf + sub[1];
Lubomir Rintel 0efa5e
+      char const *beg = line_buf + sub[0];
Lubomir Rintel 0efa5e
+      char const *end = line_buf + sub[1];
Lubomir Rintel 0efa5e
       char const *buflim = buf + size;
Lubomir Rintel 0efa5e
       char eol = eolbyte;
Lubomir Rintel 0efa5e
       if (!start_ptr)
Lubomir Rintel 0efa5e
-- 
Lubomir Rintel 0efa5e
1.5.5.1
Lubomir Rintel 0efa5e