Merge pull request #327 from chrisvest/avoid-grapheme-iteration

Avoid iterating graphemes unless we have to.
This commit is contained in:
Alexandre Bury 2019-03-12 13:55:49 -07:00 committed by GitHub
commit e5ef01c90f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -129,13 +129,17 @@ impl<'a, 'b> Printer<'a, 'b> {
return; return;
} }
let text_width = text.width(); let mut text_width = text.width();
// If we're waaaay too far left, just give up. // If we're waaaay too far left, just give up.
if hidden_part.x > text_width { if hidden_part.x > text_width {
return; return;
} }
let mut text = text;
let mut start = start;
if hidden_part.x > 0 {
// We have to drop hidden_part.x width from the start of the string. // We have to drop hidden_part.x width from the start of the string.
// prefix() may be too short if there's a double-width character. // prefix() may be too short if there's a double-width character.
// So instead, keep the suffix and drop the prefix. // So instead, keep the suffix and drop the prefix.
@ -153,22 +157,27 @@ impl<'a, 'b> Printer<'a, 'b> {
assert!(skipped_width >= hidden_part.x); assert!(skipped_width >= hidden_part.x);
// Drop part of the text, and move the cursor correspondingly. // Drop part of the text, and move the cursor correspondingly.
let text = &text[skipped_len..]; text = &text[skipped_len..];
let start = start + (skipped_width, 0); start = start + (skipped_width, 0);
text_width -= skipped_width;
}
assert!(start.fits(self.content_offset)); assert!(start.fits(self.content_offset));
// What we did before should guarantee that this won't overflow. // What we did before should guarantee that this won't overflow.
let start = start - self.content_offset; start = start - self.content_offset;
// Do we have enough room for the entire line? // Do we have enough room for the entire line?
let room = self.output_size.x - start.x; let room = self.output_size.x - start.x;
if room < text_width {
// Drop the end of the text if it's too long // Drop the end of the text if it's too long
// We want the number of CHARACTERS, not bytes. // We want the number of CHARACTERS, not bytes.
// (Actually we want the "width" of the string, see unicode-width) // (Actually we want the "width" of the string, see unicode-width)
let prefix_len = prefix(text.graphemes(true), room, "").length; let prefix_len = prefix(text.graphemes(true), room, "").length;
let text = &text[..prefix_len]; text = &text[..prefix_len];
assert!(text.width() <= room); assert!(text.width() <= room);
}
let start = start + self.offset; let start = start + self.offset;
self.backend.print_at(start, text); self.backend.print_at(start, text);