Skip to content

Commit 45022be

Browse files
committed
Simplify code
Make use of the fact that folder must never produce a line of only WSP.
1 parent a1cfe3c commit 45022be

File tree

1 file changed

+4
-23
lines changed

1 file changed

+4
-23
lines changed

Lib/email/_header_value_parser.py

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,28 +2837,6 @@ def _steal_trailing_WSP_if_exists(lines):
28372837
return wsp
28382838

28392839

2840-
def _steal_all_trailing_WSP_if_exists(lines):
2841-
lines_popped = False
2842-
wsp_lines = []
2843-
while lines and lines[-1]:
2844-
for i in range(len(lines[-1]), -1, -1):
2845-
if i <= 0 or lines[-1][i - 1] not in WSP:
2846-
break
2847-
wsp_line = lines[-1][i:]
2848-
if not wsp_line:
2849-
break
2850-
wsp_lines.insert(0, wsp_line)
2851-
lines[-1] = lines[-1][:i]
2852-
if not lines[-1]:
2853-
lines_popped = True
2854-
lines.pop()
2855-
else:
2856-
break
2857-
if lines_popped:
2858-
lines.append(' ' if lines else '')
2859-
return ''.join(wsp_lines)
2860-
2861-
28622840
def _last_word_is_sill_ew(_last_word_is_ew, added_str):
28632841
# If the last word is an encoded word, and the added string is all WSP,
28642842
# then (and only then) is the last word is still an encoded word.
@@ -3053,7 +3031,10 @@ def _fold_as_ew(to_encode, lines, maxlen, last_ew, ew_combine_allowed, charset,
30533031
# any white space between the two will be ignored when decoded.
30543032
# Therefore, we encode all to-be-displayed whitespace in the second
30553033
# encoded word.
3056-
leading_whitespace = _steal_all_trailing_WSP_if_exists(lines)
3034+
len_without_wsp = len(lines[-1].rstrip(_WSP))
3035+
leading_whitespace = lines[-1][len_without_wsp:]
3036+
lines[-1] = (lines[-1][:len_without_wsp]
3037+
+ (' ' if leading_whitespace else ''))
30573038
to_encode = leading_whitespace + to_encode
30583039

30593040
trailing_wsp = ''

0 commit comments

Comments
 (0)