|
29 | 29 |
|
30 | 30 | USASCII = Charset('us-ascii') |
31 | 31 | UTF8 = Charset('utf-8') |
32 | | -TRANSITIONAL_SPACE = object() |
33 | 32 |
|
34 | 33 | # Match encoded-word strings in the form =?charset?q?Hello_World?= |
35 | 34 | ecre = re.compile(r''' |
@@ -309,6 +308,7 @@ def encode(self, splitchars=';, \t', maxlinelen=None): |
309 | 308 | formatter.feed(line, charset) |
310 | 309 | if len(lines) > 1: |
311 | 310 | formatter.newline() |
| 311 | + formatter.add_transition() |
312 | 312 | return str(formatter) |
313 | 313 |
|
314 | 314 | def _normalize(self): |
@@ -341,19 +341,20 @@ def __init__(self, headerlen, maxlen, continuation_ws, splitchars): |
341 | 341 | self._current_line = _Accumulator(headerlen) |
342 | 342 |
|
343 | 343 | def __str__(self): |
344 | | - # Remove any trailing TRANSITIONAL_SPACE |
345 | | - if len(self._current_line) > 0: |
346 | | - last_line = self._current_line.pop() |
347 | | - if last_line is not TRANSITIONAL_SPACE: |
348 | | - self._current_line.push(last_line) |
349 | 344 | self.newline() |
350 | 345 | return NL.join(self._lines) |
351 | 346 |
|
352 | 347 | def newline(self): |
| 348 | + end_of_line = self._current_line.pop() |
| 349 | + if end_of_line is not None: |
| 350 | + self._current_line.push(end_of_line) |
353 | 351 | if len(self._current_line) > 0: |
354 | 352 | self._lines.append(str(self._current_line)) |
355 | 353 | self._current_line.reset() |
356 | 354 |
|
| 355 | + def add_transition(self): |
| 356 | + self._current_line.push(None) |
| 357 | + |
357 | 358 | def feed(self, string, charset): |
358 | 359 | # If the string itself fits on the current line in its encoded format, |
359 | 360 | # then add it now and be done with it. |
@@ -408,7 +409,6 @@ def feed(self, string, charset): |
408 | 409 | # There was only one line. |
409 | 410 | return |
410 | 411 | self._current_line.push(last_line) |
411 | | - self._current_line.push(TRANSITIONAL_SPACE) |
412 | 412 | # Everything else are full lines in themselves. |
413 | 413 | for line in encoded_lines: |
414 | 414 | self._lines.append(self._continuation_ws + line) |
@@ -554,18 +554,20 @@ def push(self, string): |
554 | 554 | self._current.append(string) |
555 | 555 |
|
556 | 556 | def pop(self): |
| 557 | + if not self._current: |
| 558 | + return None |
557 | 559 | return self._current.pop() |
558 | 560 |
|
559 | 561 | def __len__(self): |
560 | | - return sum((len(string) |
561 | | - for string in self._current |
562 | | - if string is not TRANSITIONAL_SPACE), |
| 562 | + return sum(((1 if string is None else len(string)) |
| 563 | + for string in self._current), |
563 | 564 | self._initial_size) |
564 | 565 |
|
565 | 566 | def __str__(self): |
566 | | - return EMPTYSTRING.join( |
567 | | - (' ' if string is TRANSITIONAL_SPACE else string) |
568 | | - for string in self._current) |
| 567 | + if self._current and self._current[-1] is None: |
| 568 | + self._current.pop() |
| 569 | + return EMPTYSTRING.join((' ' if string is None else string) |
| 570 | + for string in self._current) |
569 | 571 |
|
570 | 572 | def reset(self, string=None): |
571 | 573 | self._current = [] |
|
0 commit comments