FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

_find_width_index and _handle_long_word change by tiptenbrink · Pull Request #2 · xi/cpython · GitHub

Repository navigation

_find_width_index and _handle_long_word change - #2

Merged
xi merged 2 commits into
xi:textwrap-lenfrom
tiptenbrink:textwrap-len
Nov 11, 2021
Merged

xi merged 2 commits into
xi:textwrap-lenfrom
tiptenbrink:textwrap-len

Conversation

Copy link
Copy Markdown

(the previous PR was made with the incorrect account, apologies)

_find_width_index and _handle_long_word change

bpo-12499: Fix for zero-width characters in custom text_len function for TextWrapper class

See python#28136 (specifically this comment)

Comment thread Lib/textwrap.py Outdated

xi left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Great find and great solution! There are just some minor issues that need to be ironed out.

Comment thread Lib/textwrap.py Outdated
Comment thread Lib/textwrap.py Outdated
Comment thread Lib/textwrap.py Outdated
Comment thread Lib/textwrap.py
# for each character
if self.text_len(text[:width]) == width:
# For character widths greater than one, width can be more than the number of characters
return min(width, len(text))

xi Nov 10, 2021 •
edited
Loading

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I am not sure if this heuristic works. What would happen in the following case:

lengths = {
    'A': 2,
    'B': 0,
}

def text_len(s):
    return sum(lengths.get(c, 1) for c in s)

wrap('AAABBB', 3)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If performance is an issue, a binary search might be an option.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

In that case the width of text[:width] (=['AAA']) is not equal to width (=3), so it will calculate it per character. However, if you'd used len as your text_len, you'd get that the width of ['AAA'] = 3, which is correct to it skips the calculation.

Binary search might work, but it'd require disallowing negative character lengths (which I agree don't make sense but the results of binary search might make the results more different than what you'd expect).

tiptenbrink requested a review from xi November 10, 2021 16:39
tiptenbrink added a commit to tiptenbrink/textwrap that referenced this pull request Nov 10, 2021
xi merged commit b811049 into xi:textwrap-len Nov 11, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL