-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use u16
instead of usize
for line width
#418
Conversation
1dfc75a
to
0f8be02
Compare
Hi @robinkrahl, @NeverGivinUp, @adetaylor, and @olson-sean-k! You've all been active in #247 and #416 and I would like your feedback on this PR. I believe both issues are caused by the same underlying problem: unintended integer overflows in In this PR, I'm playing with the idea of limiting the maximum line width to something much smaller than So now my question to you is: does it sound like a good idea to limit the maximum line width like this? The size of each fragment (word) is similarly limited to I've added tests to ensure that we can still wrap a string with length larger than |
0f8be02
to
3c7db26
Compare
Using an `usize` for the line width causes problems for the optimal-fit wrapping algorithm. The problem is that the algorithm works with integer values formed by (line_width - text_width)**2 When `line_width` is near `usize::max_value()`, this computation will overflow an `usize`. By limiting the line width to an `u16`, we can do the internal computations with `u64` and avoid the overflows.
3c7db26
to
e2eaa47
Compare
Hi Martin, |
@mgeisler Thanks for working on this issue! Here are my first thoughts. I will give this issue some more thought and maybe do some experiments in the next days. For the In any case, this change would mean that is no longer possible to use a fixed scale factor of e. g. 100. Therefore it would be helpful to have an interface that automatically scales all widths so that the line width maps to |
Thanks for tagging me, but my involvement was just a drive-by comment hoping to help with release vs debug mode behavior, I know nothing about the issue itself :) Good luck on the fix! |
Hey Navid, I just tested with your example, but with the
Thanks! No stress, I'll leave it around for while, probably until after the holidays. It's a pretty major change so I also have to get used to it :-D |
That is a very good point... since we have That would let users typeset text on a piece of A-31 paper 😆 |
When using `u128` internally in `wrap_optimal_fit`, we can let the line width go all the way up to `u32` without risking overflows.
1a7eee7
to
f389353
Compare
Sorry for the confusion... I renamed the branch to better reflect the change. The new PR is up at #420. |
Using an
usize
for the line width causes problems for the optimal-fit wrapping algorithm. The problem is that the algorithm works with integer values formed byWhen
line_width
is nearusize::max_value()
, this computation will overflow anusize
. By limiting the line width to anu16
, we can do the internal computations withu64
and avoid the overflows.