changeset 1059:4eab07ef66e2

grep: speed up matching, and only return one match per line.
author bos@serpentine.internal.keyresearch.com
date Thu, 25 Aug 2005 17:13:48 -0700
parents 402279974aea
children e453d2053b2e
files mercurial/commands.py
diffstat 1 files changed, 11 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Thu Aug 25 10:11:54 2005 -0700
+++ b/mercurial/commands.py	Thu Aug 25 17:13:48 2005 -0700
@@ -806,13 +806,17 @@
         return fcache[fn]
 
     def matchlines(body):
-        # massively inefficient. rewrite.
-        for match in regexp.finditer(body):
-            start, end = match.span()
-            lnum = body.count('\n', 0, start) + 1
-            lstart = body.rfind('\n', 0, start) + 1
-            lend = body.find('\n', end)
-            yield lnum, start - lstart, end - lstart, body[lstart:lend]
+        begin = 0
+        linenum = 0
+        while True:
+            match = regexp.search(body, begin)
+            if not match: break
+            mstart, mend = match.span()
+            linenum += body.count('\n', begin, mstart) + 1
+            lstart = body.rfind('\n', begin, mstart) + 1 or begin
+            lend = body.find('\n', mend)
+            yield linenum, mstart - lstart, mend - lstart, body[lstart:lend]
+            begin = lend + 1
 
     class linestate:
         def __init__(self, line, linenum, colstart, colend):