-
-
Notifications
You must be signed in to change notification settings - Fork 561
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
Switch from textwrap to bwrap #1765
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the struct bwrap::Wrapper
will use manual memory management and ask to provide the bytes-buffers (which can go wrong easily), but they have the other struct bwrap::EasyWrapper
which uses the standard library for memory management.
With the standard library feature you can actually use the macro bwrap::wrap!
to wrap the text easily without having to change the method signatures since it has similar signature to textwrap::wrap
methods and will save you all the headache from dealing with converting between utf8 and byte-slices because the current implementation will fail with none-ASCII characters.
let title_len = message.subject.len(); | ||
let mut title_buffer = | ||
vec![0; title_len + title_len / width + 1]; | ||
let message_len = | ||
message.body.as_ref().map_or(0usize, String::len); | ||
let mut message_buffer = | ||
vec![0; message_len + message_len / width + 1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Buffers length assumes that all the characters are ASCII so each char will occupy one byte only. But that's not the case and this will fail with non-ASCII chars since chars in UTF8 could occupy ab to 4 bytes. For such cases you could use as_bytes()
method to get the actual number of bytes.
let (title_len, message_len) = | ||
self.data.as_ref().map_or((0usize, 0usize), |data| { | ||
data.message.as_ref().map_or( | ||
(0usize, 0usize), | ||
|message| { | ||
let title_len = message.subject.len(); | ||
let message_len = message | ||
.body | ||
.as_ref() | ||
.map_or(0usize, String::len); | ||
(title_len, message_len) | ||
}, | ||
) | ||
}); | ||
let mut title_buffer = | ||
vec![0u8; title_len + title_len / usize::from(width) + 1]; | ||
let mut message_buffer = | ||
vec![ | ||
0u8; | ||
message_len + message_len / usize::from(width) + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same as the above and it should fail with none-ASCII chars
Closed in favor of #1792 |
This Pull Request fixes/closes #1762.
It changes the following:
I followed the checklist:
I added unittestsThe functionality was verfied by existing unittestsmake check
without errors