summaryrefslogtreecommitdiffstats
path: root/Lib/difflib.py
diff options
context:
space:
mode:
authorSemyon Moroz <donbarbos@proton.me>2025-05-01 04:11:36 (GMT)
committerGitHub <noreply@github.com>2025-05-01 04:11:36 (GMT)
commitbce45bcaf88c579c6fffcc4b20147d60caca5364 (patch)
tree3508e3f65f60b19a62510fb8b78eaec220eabb41 /Lib/difflib.py
parentb99d970bcde68543a213a0c2f484b932e77636d1 (diff)
downloadcpython-bce45bcaf88c579c6fffcc4b20147d60caca5364.zip
cpython-bce45bcaf88c579c6fffcc4b20147d60caca5364.tar.gz
cpython-bce45bcaf88c579c6fffcc4b20147d60caca5364.tar.bz2
gh-130167: Improve ``difflib.IS_LINE_JUNK`` performance by using string methods (#130170)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Tim Peters <tim.peters@gmail.com>
Diffstat (limited to 'Lib/difflib.py')
-rw-r--r--Lib/difflib.py12
1 files changed, 7 insertions, 5 deletions
diff --git a/Lib/difflib.py b/Lib/difflib.py
index 4bba9e7..f1f4e62 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -1038,11 +1038,9 @@ class Differ:
# remaining is that perhaps it was really the case that " volatile"
# was inserted after "private". I can live with that <wink>.
-import re
-
-def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
+def IS_LINE_JUNK(line, pat=None):
r"""
- Return True for ignorable line: iff `line` is blank or contains a single '#'.
+ Return True for ignorable line: if `line` is blank or contains a single '#'.
Examples:
@@ -1054,6 +1052,11 @@ def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
False
"""
+ if pat is None:
+ # Default: match '#' or the empty string
+ return line.strip() in '#'
+ # Previous versions used the undocumented parameter 'pat' as a
+ # match function. Retain this behaviour for compatibility.
return pat(line) is not None
def IS_CHARACTER_JUNK(ch, ws=" \t"):
@@ -2027,7 +2030,6 @@ class HtmlDiff(object):
replace('\1','</span>'). \
replace('\t','&nbsp;')
-del re
def restore(delta, which):
r"""