core: Fix normalizeWhitespace.

This commit is contained in:
Valentin Lorentz 2013-01-04 20:25:25 +01:00
parent 408f6e2288
commit 28dc3dd3ac

View File

@ -55,18 +55,19 @@ def rsplit(s, sep=None, maxsplit=-1):
else: else:
return s.rsplit(sep, maxsplit) return s.rsplit(sep, maxsplit)
def normalizeWhitespace(s, removeNewline=True): def normalizeWhitespace(s, removeNewline=False):
"""Normalizes the whitespace in a string; \s+ becomes one space.""" """Normalizes the whitespace in a string; \s+ becomes one space."""
replace_fn = lambda x, y, z: str.replace(x, y, z) if not s:
if isinstance(s, unicode): return str(s) # not the same reference
replace_fn = lambda x, y, z: unicode.replace(x, y, z) starts_with_space = (s[0] in ' \n\t')
else: ends_with_space = (s[-1] in ' \n\t')
s = str(s) s = ('' if removeNewline else ' ').join(filter(bool, s.split('\n')))
if removeNewline: s = ' '.join(filter(bool, s.split('\t')))
s = replace_fn(s, '\n', '') s = ' '.join(filter(bool, s.split(' ')))
s = replace_fn(s, '\t', ' ') if starts_with_space:
while ' ' in s: s = ' ' + s
s = replace_fn(s, ' ', ' ') if ends_with_space:
s += ' '
return s return s
def distance(s, t): def distance(s, t):