[meld: 1/3] issue: #dirdiff.remove_blank_lines - make remove blank lines runs faster; commit: mixin ignore_blank



commit 186993ab01b80c8007adf08bae477a2a7a3cfe72
Author: hugosenari <hugosenari gmail com>
Date:   Thu Jul 5 01:24:20 2018 -0300

    issue: #dirdiff.remove_blank_lines - make remove blank lines runs faster; commit: mixin 
ignore_blank_lines and ignore line ending difference

 meld/dirdiff.py                         | 20 ++++++++++----------
 test/dirdiff/test_remove_blank_lines.py | 29 +++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 10 deletions(-)
---
diff --git a/meld/dirdiff.py b/meld/dirdiff.py
index 7a050814..0d26d368 100644
--- a/meld/dirdiff.py
+++ b/meld/dirdiff.py
@@ -19,6 +19,7 @@ import copy
 import errno
 import functools
 import os
+import re
 import shutil
 import stat
 import sys
@@ -83,11 +84,11 @@ CHUNK_SIZE = 4096
 
 
 def remove_blank_lines(text):
-    splits = text.splitlines()
-    lines = text.splitlines(True)
-    blanks = set([i for i, l in enumerate(splits) if not l])
-    lines = [l for i, l in enumerate(lines) if i not in blanks]
-    return b''.join(lines)
+    """
+    Remove blank lines from text.
+    And normalize line ending
+    """
+    return b'\n'.join(filter(bool, text.splitlines()))
 
 
 def _files_same(files, regexes, comparison_args):
@@ -184,13 +185,12 @@ def _files_same(files, regexes, comparison_args):
     if result == Different and need_contents:
         contents = [b"".join(c) for c in contents]
         # For probable text files, discard newline differences to match
-        # file comparisons.
-        contents = [b"\n".join(c.splitlines()) for c in contents]
-
-        contents = [misc.apply_text_filters(c, regexes) for c in contents]
-
         if ignore_blank_lines:
             contents = [remove_blank_lines(c) for c in contents]
+        else:
+            contents = [b"\n".join(c.splitlines()) for c in contents]
+
+        contents = [misc.apply_text_filters(c, regexes) for c in contents]
         result = SameFiltered if all_same(contents) else Different
 
     _cache[cache_key] = CacheResult(stats, result)
diff --git a/test/dirdiff/test_remove_blank_lines.py b/test/dirdiff/test_remove_blank_lines.py
new file mode 100644
index 00000000..5daae854
--- /dev/null
+++ b/test/dirdiff/test_remove_blank_lines.py
@@ -0,0 +1,29 @@
+import pytest
+from meld.dirdiff import remove_blank_lines
+
+@pytest.mark.parametrize('txt, expected', [
+    # blank to be equal blank
+    (b'', b''),
+    # one line with spaces
+    (b' ', b' '),
+    # two lines empty
+    (b'\n', b''),
+    (b'\n ', b' '),
+    (b' \n', b' '),
+    (b' \n ', b' \n '),
+    # tree lines empty
+    (b'\n\n', b''),
+    (b'\n\n ', b' '),
+    (b'\n \n', b' '),
+    (b'\n \n ', b' \n '),
+    (b' \n \n ', b' \n \n '),
+    # one line with space and content
+    (b' content', b' content'),
+    # empty line between content
+    (b'content\n\ncontent', b'content\ncontent'),
+    # multiple leading and trailing newlines
+    (b'\n\n\ncontent\n\n\ncontent\n\n\n', b'content\ncontent'),
+])
+def test_remove_blank_lines(txt, expected):
+    result = remove_blank_lines(txt)
+    assert result == expected
\ No newline at end of file


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]