Do not split UTF-8 characters between "Q"-encoded-words#41
Conversation
|
This does not enforce 76 character limit strictly and only splits newlines after overflow, but is an improvement over current state anyway. The test does not rely on exact line lengths, so this can be improved later and the test will ensure that there is no regression wrt. fix for character boundaries. |
2dac05c to
4eb118e
Compare
| bytes_written += prefix.len(); | ||
|
|
||
| for (pos, &ch) in self.text.as_bytes().iter().enumerate() { | ||
| if bytes_written >= 76 && self.text.is_char_boundary(pos) { |
There was a problem hiding this comment.
I think it would be more idiomatic (and likely more performant) to replace the current self.text.as_bytes().iter().enumerate() + text.is_char_boundary() combination with self.text.char_indices().
char_indices() allows iterating over valid UTF-8 character boundaries and avoids the need to manually check boundaries using is_char_boundary on each iteration.
There was a problem hiding this comment.
I thought about it, but I want to iterate over bytes rather than characters. Internally char_indices() just uses chars() iterator.
Regarding performance, checking for UTF-8 boundary is very cheap, it just looks at the byte value internally using this private function:
https://github.com/rust-lang/rust/blob/8708f3cd1f96d226f6420a58ebdd61aa0bc08b0a/library/core/src/num/mod.rs#L1073-L1077
Iterating over bytes might actually be more performant than iterating to the end of char and then rewinding back and looping over all char bytes again, but in any case I think it's better to not do such low-level optimizations without measurements.
There was a problem hiding this comment.
In that case you can replace:
self.text.is_char_boundary(pos)
with
pos == 0 || ch.is_utf8_char_boundary()
this avoids the extra array access done internally by self.text.is_char_boundary(pos).
There was a problem hiding this comment.
is_utf8_char_boundary is private. And is_char_boundary is inlined, so compiler should optimize array access.
See also comments in the standard library:
https://github.com/rust-lang/rust/blob/8708f3cd1f96d226f6420a58ebdd61aa0bc08b0a/library/core/src/str/mod.rs#L360-L383
Anyway, I don't see much point in optimizing the code without measurements. I will be likely changed in the future anyway to have strict 76 character boundary, which I don't try to fix. My only goal is to fix the bug with UTF-8 characters being broken between encoded-words.
There was a problem hiding this comment.
Yes, is_utf8_char_boundary is inlined but self.text.is_char_boundary(pos) is accessing the array by pos on each iteration and you already have ch with the value needed.
If is_utf8_char_boundary is private then an alternative would be pos == 0 || (ch as i8) >= -0x40.
4eb118e to
dbb451d
Compare
dbb451d to
8825292
Compare
|
Thanks! |
Partially addresses #40.
Multiple commits, don't squash.