Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit37551c9

Browse files
authored
gh-107369: optimize textwrap.indent() (#107374)
1 parentf2d07d3 commit37551c9

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

‎Doc/whatsnew/3.13.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ typing
150150
Optimizations
151151
=============
152152

153-
153+
*:func:`textwrap.indent` is now ~30% faster than before for large input.
154+
(Contributed by Inada Naoki in:gh:`107369`.)
154155

155156

156157
Deprecated

‎Lib/textwrap.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,19 @@ def indent(text, prefix, predicate=None):
476476
consist solely of whitespace characters.
477477
"""
478478
ifpredicateisNone:
479-
defpredicate(line):
480-
returnline.strip()
481-
482-
defprefixed_lines():
483-
forlineintext.splitlines(True):
484-
yield (prefix+lineifpredicate(line)elseline)
485-
return''.join(prefixed_lines())
479+
# str.splitlines(True) doesn't produce empty string.
480+
# ''.splitlines(True) => []
481+
# 'foo\n'.splitlines(True) => ['foo\n']
482+
# So we can use just `not s.isspace()` here.
483+
predicate=lambdas:nots.isspace()
484+
485+
prefixed_lines= []
486+
forlineintext.splitlines(True):
487+
ifpredicate(line):
488+
prefixed_lines.append(prefix)
489+
prefixed_lines.append(line)
490+
491+
return''.join(prefixed_lines)
486492

487493

488494
if__name__=="__main__":
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Optimize:func:`textwrap.indent`. It is ~30% faster for large input. Patch
2+
by Inada Naoki.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp