Skip to content

Conversation

@tiptenbrink
Copy link

(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)

Copy link
Owner

@xi xi left a comment

Choose a reason for hiding this comment

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

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

# 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))
Copy link
Owner

@xi xi Nov 10, 2021

Choose a reason for hiding this comment

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

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
Owner

Choose a reason for hiding this comment

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

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

Copy link
Author

Choose a reason for hiding this comment

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

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 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 xi merged commit b811049 into xi:textwrap-len Nov 11, 2021
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