Commits

Jonathan Eunice committed 05681c8

fixed case of common non-blank prefixes

Comments (0)

Files changed (4)

    
  * Common line prefix is now computed without considering blank lines, so blank
    lines need not have any indentation on them just to "make things work."
+   
+ * The tricky case where all lines have a common prefix, but it's not entirely
+   composed of whitespace, now properly handled.
 
 Installation
 ============
 
 ::
 
-    pip install textdata
+    pip install -U textdata
     
 To ``easy_install`` under a specific Python version (3.3 in this example)::
 
-    python3.3 -m easy_install textdata
+    python3.3 -m easy_install --upgrade textdata
     
 (You may need to prefix these with "sudo " to authorize installation.)
 
 setup(
     name='textdata',
-    version=verno("0.407"),
+    version=verno("0.414"),
     author='Jonathan Eunice',
     author_email='jonathan.eunice@gmail.com',
     description='Get clean line or text data from multi-line strings',
     
           """)
     
-    assert lresult == tresult.splitlines()
+    assert lresult == tresult.splitlines()
+    
+def test_tricky_prefix():
+    """
+    Common prefixes need not be all spaces. This tests if common non-blank
+    prefixes are properly handled.
+    """
+    
+    t = textlines("""
+        something
+        something else
+    """)
+    assert t == "something\nsomething else"
+    
+    t2 = textlines("""
+xxx y
+xxx z
+""")
+    assert t2 == "xxx y\nxxx z"
     
     if dedent and not lstrip:
         nonblanklines = [ line for line in textlines if line.strip() != "" ]
-        prefix = os.path.commonprefix(nonblanklines) # piggyback!
-        if prefix:
-            prelen = len(prefix)
+        prefix = os.path.commonprefix(nonblanklines)
+        prelen, maxprelen = 0, len(prefix)
+        while prelen < maxprelen and prefix[prelen] == ' ':
+            prelen += 1
+        if prelen:
             textlines = [ line[prelen:] for line in textlines ]
 
     # perform requested left and right space stripping (must be done