[PATCH 2 of 2] mdiff: use str.startswith/endswith() instead of slicing

Yuya Nishihara yuya at tcha.org
Sun Feb 4 06:17:51 EST 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1517707994 -32400
#      Sun Feb 04 10:33:14 2018 +0900
# Node ID 16b4cc4f0cd6f72f5b7be575a7498ed0017ccea5
# Parent  d41b22b06360ceee3a9e6e66df5f57f267318314
mdiff: use str.startswith/endswith() instead of slicing

diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py
--- a/mercurial/mdiff.py
+++ b/mercurial/mdiff.py
@@ -274,7 +274,7 @@ def unidiff(a, ad, b, bd, fn1, fn2, bina
         headerlines = []
         hunks = (None, ['Binary file %s has changed\n' % fn1]),
     elif not a:
-        without_newline = b[-1:] != '\n'
+        without_newline = not b.endswith('\n')
         b = splitnewlines(b)
         if a is None:
             l1 = '--- /dev/null%s' % datetag(epoch)
@@ -290,7 +290,7 @@ def unidiff(a, ad, b, bd, fn1, fn2, bina
             hunklines.append(_missing_newline_marker)
         hunks = (hunkrange, hunklines),
     elif not b:
-        without_newline = a[-1:] != '\n'
+        without_newline = not a.endswith('\n')
         a = splitnewlines(a)
         l1 = "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1))
         if b is None:
@@ -383,17 +383,17 @@ def _unidiff(t1, t2, opts=defaultopts):
         # a newline, print only one marker. That's the only case in
         # which the hunk can end in a shared line without a newline.
         skip = False
-        if t1[-1:] != '\n' and astart + alen == len(l1) + 1:
+        if not t1.endswith('\n') and astart + alen == len(l1) + 1:
             for i in xrange(len(hunklines) - 1, -1, -1):
-                if hunklines[i][0:1] in ('-', ' '):
-                    if hunklines[i][0:1] == ' ':
+                if hunklines[i].startswith(('-', ' ')):
+                    if hunklines[i].startswith(' '):
                         skip = True
                     hunklines[i] += '\n'
                     hunklines.insert(i + 1, _missing_newline_marker)
                     break
-        if not skip and t2[-1:] != '\n' and bstart + blen == len(l2) + 1:
+        if not skip and not t2.endswith('\n') and bstart + blen == len(l2) + 1:
             for i in xrange(len(hunklines) - 1, -1, -1):
-                if hunklines[i][0:1] == '+':
+                if hunklines[i].startswith('+'):
                     hunklines[i] += '\n'
                     hunklines.insert(i + 1, _missing_newline_marker)
                     break


More information about the Mercurial-devel mailing list