| 510 | self._append_chunk(fws, part) |
| 511 | |
| 512 | def _append_chunk(self, fws, string): |
| 513 | self._current_line.push(fws, string) |
| 514 | if len(self._current_line) > self._maxlen: |
| 515 | # Find the best split point, working backward from the end. |
| 516 | # There might be none, on a long first line. |
| 517 | for ch in self._splitchars: |
| 518 | for i in range(self._current_line.part_count()-1, 0, -1): |
| 519 | if ch.isspace(): |
| 520 | fws = self._current_line[i][0] |
| 521 | if fws and fws[0]==ch: |
| 522 | break |
| 523 | prevpart = self._current_line[i-1][1] |
| 524 | if prevpart and prevpart[-1]==ch: |
| 525 | break |
| 526 | else: |
| 527 | continue |
| 528 | break |
| 529 | else: |
| 530 | fws, part = self._current_line.pop() |
| 531 | if self._current_line._initial_size > 0: |
| 532 | # There will be a header, so leave it on a line by itself. |
| 533 | self.newline() |
| 534 | if not fws: |
| 535 | # We don't use continuation_ws here because the whitespace |
| 536 | # after a header should always be a space. |
| 537 | fws = ' ' |
| 538 | self._current_line.push(fws, part) |
| 539 | return |
| 540 | remainder = self._current_line.pop_from(i) |
| 541 | self._lines.append(str(self._current_line)) |
| 542 | self._current_line.reset(remainder) |
| 543 | |
| 544 | |
| 545 | class _Accumulator(list): |