Skip to content

Commit d6327e8

Browse files
authored
Use map + sum instead of fold for computing Unicode width
1 parent 4fc5650 commit d6327e8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/librustc_errors/emitter.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,9 @@ impl EmitterWriter {
594594

595595
let left = margin.left(source_string.len()); // Left trim
596596
// Account for unicode characters of width !=0 that were removed.
597-
let left = source_string.chars().take(left).fold(0, |acc, ch| {
598-
acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1)
599-
});
597+
let left = source_string.chars().take(left)
598+
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
599+
.sum();
600600

601601
self.draw_line(
602602
buffer,
@@ -1512,9 +1512,9 @@ impl EmitterWriter {
15121512
.saturating_sub(part.snippet.trim_start().len());
15131513
// ...or trailing spaces. Account for substitutions containing unicode
15141514
// characters.
1515-
let sub_len = part.snippet.trim().chars().fold(0, |acc, ch| {
1516-
acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1)
1517-
});
1515+
let sub_len = part.snippet.trim().chars()
1516+
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
1517+
.sum();
15181518

15191519
let underline_start = (span_start_pos + start) as isize + offset;
15201520
let underline_end = (span_start_pos + start + sub_len) as isize + offset;
@@ -1535,9 +1535,9 @@ impl EmitterWriter {
15351535
}
15361536

15371537
// length of the code after substitution
1538-
let full_sub_len = part.snippet.chars().fold(0, |acc, ch| {
1539-
acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) as isize
1540-
});
1538+
let full_sub_len = part.snippet.chars()
1539+
.map(|ch| acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
1540+
.sum() as isize;
15411541

15421542
// length of the code to be substituted
15431543
let snippet_len = span_end_pos as isize - span_start_pos as isize;

0 commit comments

Comments
 (0)