Skip to content

Commit 4790f32

Browse files
committed
Use fold instead of flat_map
1 parent 6d9ee6e commit 4790f32

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -3867,7 +3867,6 @@ version = "0.0.0"
38673867
dependencies = [
38683868
"annotate-snippets 0.10.2",
38693869
"derive_setters",
3870-
"either",
38713870
"rustc_ast",
38723871
"rustc_ast_pretty",
38733872
"rustc_data_structures",

compiler/rustc_errors/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ edition = "2021"
77
# tidy-alphabetical-start
88
annotate-snippets = "0.10"
99
derive_setters = "0.1.6"
10-
either = "1.5.0"
1110
rustc_ast = { path = "../rustc_ast" }
1211
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1312
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_errors/src/emitter.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use std::iter;
1616
use std::path::Path;
1717

1818
use derive_setters::Setters;
19-
use either::Either;
2019
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
2120
use rustc_data_structures::sync::{DynSend, IntoDynSyncSend, Lrc};
2221
use rustc_error_messages::{FluentArgs, SpanLabel};
@@ -2609,16 +2608,21 @@ const OUTPUT_REPLACEMENTS: &[(char, &str)] = &[
26092608
('\u{2069}', "�"),
26102609
];
26112610

2612-
fn normalize_whitespace(str: &str) -> String {
2611+
fn normalize_whitespace(s: &str) -> String {
26132612
// Scan the input string for a character in the ordered table above. If it's present, replace
26142613
// it with it's alternative string (it can be more than 1 char!). Otherwise, retain the input
26152614
// char. At the end, allocate all chars into a string in one operation.
2616-
str.chars()
2617-
.flat_map(|c| match OUTPUT_REPLACEMENTS.binary_search_by_key(&c, |(k, _)| *k) {
2618-
Ok(i) => Either::Left(OUTPUT_REPLACEMENTS[i].1.chars()),
2619-
_ => Either::Right([c].into_iter()),
2620-
})
2621-
.collect()
2615+
s.chars().fold(String::with_capacity(s.len()), |mut s, c| {
2616+
match OUTPUT_REPLACEMENTS.binary_search_by_key(&c, |(k, _)| *k) {
2617+
Ok(i) => {
2618+
for c in OUTPUT_REPLACEMENTS[i].1.chars() {
2619+
s.push(c);
2620+
}
2621+
}
2622+
_ => s.push(c),
2623+
}
2624+
s
2625+
})
26222626
}
26232627

26242628
fn draw_col_separator(buffer: &mut StyledBuffer, line: usize, col: usize) {

0 commit comments

Comments
 (0)