Skip to content

Commit 3de2378

Browse files
Compact version
1 parent 68c1b1e commit 3de2378

File tree

1 file changed

+10
-29
lines changed

1 file changed

+10
-29
lines changed

Tools/clinic/clinic.py

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -222,39 +222,20 @@ def c_repr(s: str) -> str:
222222

223223

224224
def wrapped_c_string_literal(
225-
line: str,
225+
text: str,
226+
*,
226227
width: int = 72,
227-
indent: int = 0,
228228
suffix: str = '',
229+
initial_indent: int = 0,
229230
subsequent_indent: int = 4
230231
) -> str:
231-
add, out = text_accumulator()
232-
words = line.split(' ')
233-
first = True
234-
while True:
235-
if first:
236-
add(' ' * indent)
237-
first = False
238-
else:
239-
add(' ' * subsequent_indent)
240-
add('"')
241-
sz = indent + 1
242-
while True:
243-
try:
244-
word = words[0]
245-
except IndexError:
246-
add('"')
247-
return out()
248-
sz += len(word) + 1
249-
if sz > width:
250-
break
251-
add(word)
252-
del words[0]
253-
if words:
254-
add(' ')
255-
add('"')
256-
add(suffix)
257-
add('\n')
232+
wrapped = textwrap.wrap(text, replace_whitespace=False,
233+
drop_whitespace=False, break_on_hyphens=False)
234+
suffix = f"{suffix}\n"
235+
lines = [f'"{line}"{suffix}' for line in wrapped]
236+
separator = subsequent_indent * ' '
237+
joined = initial_indent * ' ' + separator.join(lines)
238+
return joined.removesuffix(suffix)
258239

259240

260241
is_legal_c_identifier = re.compile('^[A-Za-z_][A-Za-z0-9_]*$').match

0 commit comments

Comments
 (0)