MCPcopy Index your code
hub / github.com/clips/pattern / collapse_spaces

Function collapse_spaces

pattern/web/__init__.py:848–857  ·  view source on GitHub ↗

Returns a string with consecutive spaces collapsed to a single space. Whitespace on empty lines and at the end of each line is removed. With indentation=True, retains leading whitespace on each line.

(string, indentation=False, replace=" ")

Source from the content-addressed store, hash-verified

846RE_TABS = re.compile(r"\t+", re.M) # Matches one or more tabs.
847
848def collapse_spaces(string, indentation=False, replace=" "):
849 """ Returns a string with consecutive spaces collapsed to a single space.
850 Whitespace on empty lines and at the end of each line is removed.
851 With indentation=True, retains leading whitespace on each line.
852 """
853 p = []
854 for x in string.splitlines():
855 n = indentation and len(x) - len(x.lstrip()) or 0
856 p.append(x[:n] + RE_SPACES.sub(replace, x[n:]).strip())
857 return "\n".join(p)
858
859def collapse_tabs(string, indentation=False, replace=" "):
860 """ Returns a string with (consecutive) tabs replaced by a single space.

Callers 2

plaintextFunction · 0.85
_parseMethod · 0.85

Calls 3

lenFunction · 0.85
stripMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…