diff options
| author | AnthonyMikh <anthony.mikh@yandex.ru> | 2019-09-25 23:13:19 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-25 23:13:19 +0300 |
| commit | 4fc5650d17794c00fbf03597b35979de7094d386 (patch) | |
| tree | 5015e0d778ed327130e826cc02d83a900a7c8c92 /src | |
| parent | e9a93be53a6ccec5ba76f17333acf3c198313c61 (diff) | |
| download | rust-4fc5650d17794c00fbf03597b35979de7094d386.tar.gz rust-4fc5650d17794c00fbf03597b35979de7094d386.zip | |
Simplify Unicode-aware trimming
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc_errors/emitter.rs | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 45c3dd8f4d8..151c06ddbbd 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -529,18 +529,21 @@ impl EmitterWriter { let left = margin.left(line_len); let right = margin.right(line_len); // On long lines, we strip the source line, accounting for unicode. - let mut taken = 0; - let code: String = source_string.chars().skip(left).take_while(|ch| { - // Make sure that the trimming on the right will fall within the terminal width. - // FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char` is. - // For now, just accept that sometimes the code line will be longer than desired. - let next = unicode_width::UnicodeWidthChar::width(*ch).unwrap_or(1); - if taken + next > right - left { - return false; - } - taken += next; - true - }).collect(); + // Make sure that the trimming on the right will fall within the terminal width. + // FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char` is. + // For now, just accept that sometimes the code line will be longer than desired. + let code: String = source_string.chars().skip(left) + .map(|ch| { + let width = unicode_width::UnicodeWidthChar::width(*ch).unwrap_or(1); + (width, ch) + }) + .scan(0, |len, (width, ch)| { + *len += width; + Some(*len, ch) + }) + .take_while(|&(prefix_len, _ch)| prefix_len <= right - left) + .map(|(_prefix_len, ch)| ch) + .collect(); buffer.puts(line_offset, code_offset, &code, Style::Quotation); if margin.was_cut_left() { // We have stripped some code/whitespace from the beginning, make it clear. |
