Fold string to_encode into lines as encoded word, combining if allowed. Return the new value for last_ew, or None if ew_combine_allowed is False. If there is already an encoded word in the last line of lines (indicated by a non-None value for last_ew) and ew_combine_allowed is true, dec
(to_encode, lines, maxlen, last_ew, ew_combine_allowed, charset, last_word_is_ew)
| 2997 | return policy.linesep.join(lines) + policy.linesep |
| 2998 | |
| 2999 | def _fold_as_ew(to_encode, lines, maxlen, last_ew, ew_combine_allowed, charset, last_word_is_ew): |
| 3000 | """Fold string to_encode into lines as encoded word, combining if allowed. |
| 3001 | Return the new value for last_ew, or None if ew_combine_allowed is False. |
| 3002 | |
| 3003 | If there is already an encoded word in the last line of lines (indicated by |
| 3004 | a non-None value for last_ew) and ew_combine_allowed is true, decode the |
| 3005 | existing ew, combine it with to_encode, and re-encode. Otherwise, encode |
| 3006 | to_encode. In either case, split to_encode as necessary so that the |
| 3007 | encoded segments fit within maxlen. |
| 3008 | |
| 3009 | """ |
| 3010 | if last_ew is not None and ew_combine_allowed: |
| 3011 | to_encode = str( |
| 3012 | get_unstructured(lines[-1][last_ew:] + to_encode)) |
| 3013 | lines[-1] = lines[-1][:last_ew] |
| 3014 | elif last_word_is_ew: |
| 3015 | # If we are following up an encoded word with another encoded word, |
| 3016 | # any white space between the two will be ignored when decoded. |
| 3017 | # Therefore, we encode all to-be-displayed whitespace in the second |
| 3018 | # encoded word. |
| 3019 | len_without_wsp = len(lines[-1].rstrip(_WSP)) |
| 3020 | leading_whitespace = lines[-1][len_without_wsp:] |
| 3021 | lines[-1] = (lines[-1][:len_without_wsp] |
| 3022 | + (' ' if leading_whitespace else '')) |
| 3023 | to_encode = leading_whitespace + to_encode |
| 3024 | elif to_encode[0] in WSP: |
| 3025 | # We're joining this to non-encoded text, so don't encode |
| 3026 | # the leading blank. |
| 3027 | leading_wsp = to_encode[0] |
| 3028 | to_encode = to_encode[1:] |
| 3029 | if (len(lines[-1]) == maxlen): |
| 3030 | lines.append(_steal_trailing_WSP_if_exists(lines)) |
| 3031 | lines[-1] += leading_wsp |
| 3032 | |
| 3033 | trailing_wsp = '' |
| 3034 | if to_encode[-1] in WSP: |
| 3035 | # Likewise for the trailing space. |
| 3036 | trailing_wsp = to_encode[-1] |
| 3037 | to_encode = to_encode[:-1] |
| 3038 | new_last_ew = len(lines[-1]) if last_ew is None else last_ew |
| 3039 | |
| 3040 | encode_as = 'utf-8' if charset == 'us-ascii' else charset |
| 3041 | |
| 3042 | # The RFC2047 chrome takes up 7 characters plus the length |
| 3043 | # of the charset name. |
| 3044 | chrome_len = len(encode_as) + 7 |
| 3045 | |
| 3046 | if (chrome_len + 1) >= maxlen: |
| 3047 | raise errors.HeaderParseError( |
| 3048 | "max_line_length is too small to fit an encoded word") |
| 3049 | |
| 3050 | while to_encode: |
| 3051 | remaining_space = maxlen - len(lines[-1]) |
| 3052 | text_space = remaining_space - chrome_len |
| 3053 | if text_space <= 0: |
| 3054 | newline = _steal_trailing_WSP_if_exists(lines) |
| 3055 | lines.append(newline or ' ') |
| 3056 | new_last_ew = len(lines[-1]) |
no test coverage detected
searching dependent graphs…