summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1996-02-13 00:01:28 (GMT)
committerGuido van Rossum <guido@python.org>1996-02-13 00:01:28 (GMT)
commit7bb7ecd39e42189033176ecb960e8b9e1555fa3a (patch)
tree1f4a9d83547b60e2a262aa4ec0e49659a2c780d3
parent93dc801b069dc3d4f628dc8c820ba32a51372ddf (diff)
downloadcpython-7bb7ecd39e42189033176ecb960e8b9e1555fa3a.zip
cpython-7bb7ecd39e42189033176ecb960e8b9e1555fa3a.tar.gz
cpython-7bb7ecd39e42189033176ecb960e8b9e1555fa3a.tar.bz2
even better way to normalize spaces in add_flowing_data
-rw-r--r--Lib/formatter.py26
1 files changed, 9 insertions, 17 deletions
diff --git a/Lib/formatter.py b/Lib/formatter.py
index 363222e..d1830e4 100644
--- a/Lib/formatter.py
+++ b/Lib/formatter.py
@@ -110,26 +110,18 @@ class AbstractFormatter:
if not data: return
# The following looks a bit convoluted but is a great improvement over
# data = regsub.gsub('[' + string.whitespace + ']+', ' ', data)
- if data[0] in string.whitespace:
- head = ' '
- else:
- head = ''
- if data[-1] in string.whitespace:
- tail = ' '
- else:
- tail = ''
- data = head + string.join(string.split(data))
- if data != ' ': data = data + tail
- #
- if self.nospace and data[0] == ' ':
- data = data[1:]
+ prespace = data[0] in string.whitespace
+ postspace = data[-1] in string.whitespace
+ data = string.join(string.split(data))
+ if self.nospace and prespace:
if not data: return
- elif self.softspace and data[0] != ' ':
- data = ' ' + data
+ prespace = 0
+ elif self.softspace:
+ prespace = 1
self.nospace = self.softspace = 0
- if data[-1] == ' ':
- data = data[:-1]
+ if postspace:
self.softspace = 1
+ if prespace: data = ' ' + data
self.writer.send_flowing_data(data)
def add_literal_data(self, data):