Skip to content

Commit 8d319b4

Browse files
Preserve spaces and remove unnecessary checks
1 parent 7fc34ca commit 8d319b4

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

Lib/test/test_tools/i18n_data/messages.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ msgstr ""
3535

3636
#: messages.py:32
3737
msgid ""
38-
"this is a very very very very very very very very very very very very very"
38+
"this is a very very very very very very very very very very very very very "
3939
"long string!"
4040
msgstr ""
4141

Lib/test/translationdata/argparse/msgids.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ expected one argument
1616
ignored explicit argument %r
1717
invalid %(type)s value: %(value)r
1818
invalid choice: %(value)r (choose from %(choices)s)
19-
invalid choice: %(value)r, maybe you meant %(closest)r? (choose from%(choices)s)
19+
invalid choice: %(value)r, maybe you meant %(closest)r? (choose from %(choices)s)
2020
not allowed with argument %s
2121
one of the arguments %s is required
2222
option '%(option)s' is deprecated

Tools/i18n/pygettext.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
import sys
149149
import time
150150
import tokenize
151+
import re
151152
from dataclasses import dataclass, field
152153
from io import BytesIO
153154
from operator import itemgetter
@@ -220,25 +221,21 @@ def normalize(s, encoding, options):
220221
lines = []
221222
for line in s.splitlines(True):
222223
if len(escape(line, encoding)) > options.width:
223-
words = line.split()
224+
words = re.split(r'(\s+)', line)
224225
words.reverse()
226+
buf = []
227+
size = 2
225228
while words:
226-
buf = []
227-
size = 2
228-
while words:
229-
word = words[-1]
230-
escaped_word = escape(word, encoding)
231-
escaped_word_len = len(escaped_word)
232-
add_space = 1 if buf else 0
233-
new_size = size + escaped_word_len + add_space
234-
if new_size <= options.width:
235-
buf.append(words.pop())
236-
size = new_size
237-
else:
238-
if not buf:
239-
buf = [words.pop()]
240-
break
241-
lines.append(' '.join(buf))
229+
word = words.pop()
230+
escaped_word_len = len(escape(word, encoding))
231+
if size + escaped_word_len <= options.width:
232+
buf.append(word)
233+
size += escaped_word_len
234+
else:
235+
lines.append(''.join(buf))
236+
buf = [word]
237+
size = 2 + escaped_word_len
238+
lines.append(''.join(buf))
242239
else:
243240
lines.append(line)
244241
if len(lines) <= 1:

0 commit comments

Comments
 (0)