[PATCH] patch: simplify hunk extents parsing

Patrick Mezard pmezard at gmail.com
Mon Nov 14 11:21:31 CST 2011


# HG changeset patch
# User Patrick Mezard <pmezard at gmail.com>
# Date 1321290961 -3600
# Node ID afd151d8816b84c3d5c08073e36a1b4b641d2d13
# Parent  1bb0a5b02da9ef3505bfb4ac02b2d51cf220220f
patch: simplify hunk extents parsing

Do not capture unwanted groups in regexps

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -566,8 +566,8 @@
         return self.changed | self.removed
 
 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1
-unidesc = re.compile('@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@')
-contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)')
+unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
+contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)')
 eolmodes = ['strict', 'crlf', 'lf', 'auto']
 
 class patchfile(object):
@@ -830,7 +830,7 @@
         m = unidesc.match(self.desc)
         if not m:
             raise PatchError(_("bad hunk #%d") % self.number)
-        self.starta, foo, self.lena, self.startb, foo2, self.lenb = m.groups()
+        self.starta, self.lena, self.startb, self.lenb = m.groups()
         if self.lena is None:
             self.lena = 1
         else:
@@ -857,7 +857,7 @@
         m = contextdesc.match(self.desc)
         if not m:
             raise PatchError(_("bad hunk #%d") % self.number)
-        foo, self.starta, foo2, aend, foo3 = m.groups()
+        self.starta, aend = m.groups()
         self.starta = int(self.starta)
         if aend is None:
             aend = self.starta
@@ -890,7 +890,7 @@
         m = contextdesc.match(l)
         if not m:
             raise PatchError(_("bad hunk #%d") % self.number)
-        foo, self.startb, foo2, bend, foo3 = m.groups()
+        self.startb, bend = m.groups()
         self.startb = int(self.startb)
         if bend is None:
             bend = self.startb


More information about the Mercurial-devel mailing list