about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAnthonyMikh <anthony.mikh@yandex.ru>2019-09-27 00:58:18 +0300
committerGitHub <noreply@github.com>2019-09-27 00:58:18 +0300
commit75a7c27a54be31d823cf81a26f463428ab9c2d07 (patch)
treefad49d108e0a9a063ab7f1014309a5933638acd4
parent7a0725fdaf964fa1bf60eefb0cc5846b687426bf (diff)
downloadrust-75a7c27a54be31d823cf81a26f463428ab9c2d07.tar.gz
rust-75a7c27a54be31d823cf81a26f463428ab9c2d07.zip
Revert "Simplify Unicode-aware trimming"
`taken` is actually used afterwards
-rw-r--r--src/librustc_errors/emitter.rs27
1 files changed, 12 insertions, 15 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs
index b8b7488d8ae..1aa6b405c8f 100644
--- a/src/librustc_errors/emitter.rs
+++ b/src/librustc_errors/emitter.rs
@@ -530,21 +530,18 @@ 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.
-        // 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();
+        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();
         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.