summaryrefslogtreecommitdiff
path: root/textutils.py
diff options
context:
space:
mode:
authorSylvain <syt@logilab.fr>2008-01-10 13:06:34 +0100
committerSylvain <syt@logilab.fr>2008-01-10 13:06:34 +0100
commit66d38b17019659f950a2924fb389f4e05c7527bb (patch)
tree88f8ac38c1725616950880284367b488b827ca84 /textutils.py
parent4c13f730d22d0e32eb4412fb550d409c00e2ca50 (diff)
downloadlogilab-common-66d38b17019659f950a2924fb389f4e05c7527bb.tar.gz
textutils: fix a bug in normalize_[rest_]paragraph which may cause
infinite loop if an indent string containing some spaces is given
Diffstat (limited to 'textutils.py')
-rw-r--r--textutils.py16
1 files changed, 9 insertions, 7 deletions
diff --git a/textutils.py b/textutils.py
index cf73f8d..88167a8 100644
--- a/textutils.py
+++ b/textutils.py
@@ -13,7 +13,7 @@
"""Some text manipulation utility functions.
:author: Logilab
-:copyright: 2003-2007 LOGILAB S.A. (Paris, FRANCE)
+:copyright: 2003-2008 LOGILAB S.A. (Paris, FRANCE)
:contact: http://www.logilab.fr/ -- mailto:python-projects@logilab.org
:group text formatting: normalize_text, normalize_paragraph, pretty_match,\
@@ -156,10 +156,11 @@ def normalize_paragraph(text, line_len=80, indent=''):
indentation string
"""
text = _NORM_SPACES_RGX.sub(' ', text)
+ line_len = line_len - len(indent)
lines = []
while text:
- aline, text = splittext(indent + text.strip(), line_len)
- lines.append(aline)
+ aline, text = splittext(text.strip(), line_len)
+ lines.append(indent + aline)
return linesep.join(lines)
def normalize_rest_paragraph(text, line_len=80, indent=''):
@@ -185,20 +186,21 @@ def normalize_rest_paragraph(text, line_len=80, indent=''):
"""
toreport = ''
lines = []
+ line_len = line_len - len(indent)
for line in text.splitlines():
- line = indent + toreport + _NORM_SPACES_RGX.sub(' ', line.strip())
+ line = toreport + _NORM_SPACES_RGX.sub(' ', line.strip())
toreport = ''
while len(line) > line_len:
# too long line, need split
line, toreport = splittext(line, line_len)
- lines.append(line)
+ lines.append(indent + line)
if toreport:
- line = indent + toreport + ' '
+ line = toreport + ' '
toreport = ''
else:
line = ''
if line:
- lines.append(line.strip())
+ lines.append(indent + line.strip())
return linesep.join(lines)
def splittext(text, line_len):