mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
_split_ascii() [method and function]: Don't join the lines just to
split them again. Simply return them as chunk lists. _encode_chunks(): Don't add more folding whitespace than necessary.
This commit is contained in:
parent
faef74a2b0
commit
5b8c69f11e
1 changed files with 11 additions and 10 deletions
|
@ -337,10 +337,9 @@ class Header:
|
||||||
return chunk + self._split(last, charset, self._maxlinelen, splitchars)
|
return chunk + self._split(last, charset, self._maxlinelen, splitchars)
|
||||||
|
|
||||||
def _split_ascii(self, s, charset, firstlen, splitchars):
|
def _split_ascii(self, s, charset, firstlen, splitchars):
|
||||||
line = _split_ascii(s, firstlen, self._maxlinelen,
|
chunks = _split_ascii(s, firstlen, self._maxlinelen,
|
||||||
self._continuation_ws, splitchars)
|
self._continuation_ws, splitchars)
|
||||||
lines = line.splitlines()
|
return zip(chunks, [charset]*len(chunks))
|
||||||
return zip(lines, [charset]*len(lines))
|
|
||||||
|
|
||||||
def _encode_chunks(self, newchunks, maxlinelen):
|
def _encode_chunks(self, newchunks, maxlinelen):
|
||||||
# MIME-encode a header with many different charsets and/or encodings.
|
# MIME-encode a header with many different charsets and/or encodings.
|
||||||
|
@ -360,14 +359,18 @@ class Header:
|
||||||
#
|
#
|
||||||
# =?charset1?q?Mar=EDa_Gonz=E1lez_Alonso?=\n
|
# =?charset1?q?Mar=EDa_Gonz=E1lez_Alonso?=\n
|
||||||
# =?charset2?b?SvxyZ2VuIEL2aW5n?="
|
# =?charset2?b?SvxyZ2VuIEL2aW5n?="
|
||||||
#
|
|
||||||
chunks = []
|
chunks = []
|
||||||
for header, charset in newchunks:
|
for header, charset in newchunks:
|
||||||
if charset is None or charset.header_encoding is None:
|
if charset is None or charset.header_encoding is None:
|
||||||
s = header
|
s = header
|
||||||
else:
|
else:
|
||||||
s = charset.header_encode(header)
|
s = charset.header_encode(header)
|
||||||
_max_append(chunks, s, maxlinelen, ' ')
|
# Don't add more folding whitespace than necessary
|
||||||
|
if chunks and chunks[-1].endswith(' '):
|
||||||
|
extra = ''
|
||||||
|
else:
|
||||||
|
extra = ' '
|
||||||
|
_max_append(chunks, s, maxlinelen, extra)
|
||||||
joiner = NL + self._continuation_ws
|
joiner = NL + self._continuation_ws
|
||||||
return joiner.join(chunks)
|
return joiner.join(chunks)
|
||||||
|
|
||||||
|
@ -412,7 +415,6 @@ class Header:
|
||||||
|
|
||||||
|
|
||||||
def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars):
|
def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars):
|
||||||
linejoiner = '\n' + continuation_ws
|
|
||||||
lines = []
|
lines = []
|
||||||
maxlen = firstlen
|
maxlen = firstlen
|
||||||
for line in s.splitlines():
|
for line in s.splitlines():
|
||||||
|
@ -464,9 +466,8 @@ def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars):
|
||||||
# splitting on whitespace, try to recursively split this line
|
# splitting on whitespace, try to recursively split this line
|
||||||
# on whitespace.
|
# on whitespace.
|
||||||
if partlen > maxlen and ch <> ' ':
|
if partlen > maxlen and ch <> ' ':
|
||||||
subs = _split_ascii(part, maxlen, restlen,
|
subl = _split_ascii(part, maxlen, restlen,
|
||||||
continuation_ws, ' ')
|
continuation_ws, ' ')
|
||||||
subl = re.split(linejoiner, subs)
|
|
||||||
lines.extend(subl[:-1])
|
lines.extend(subl[:-1])
|
||||||
this = [subl[-1]]
|
this = [subl[-1]]
|
||||||
else:
|
else:
|
||||||
|
@ -479,7 +480,7 @@ def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars):
|
||||||
# Put any left over parts on a line by themselves
|
# Put any left over parts on a line by themselves
|
||||||
if this:
|
if this:
|
||||||
lines.append(joiner.join(this))
|
lines.append(joiner.join(this))
|
||||||
return linejoiner.join(lines)
|
return lines
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue