Skip to content

Commit 6289c57

Browse files
committed
Simplify find_width_of_character_at_span.
1 parent 0a0e045 commit 6289c57

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

compiler/rustc_span/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#![feature(min_specialization)]
2121
#![feature(rustc_attrs)]
2222
#![feature(let_chains)]
23+
#![feature(round_char_boundary)]
2324
#![deny(rustc::untranslatable_diagnostic)]
2425
#![deny(rustc::diagnostic_outside_of_impl)]
2526

compiler/rustc_span/src/source_map.rs

+7-24
Original file line numberDiff line numberDiff line change
@@ -1019,36 +1019,19 @@ impl SourceMap {
10191019

10201020
let src = local_begin.sf.external_src.borrow();
10211021

1022-
// We need to extend the snippet to the end of the src rather than to end_index so when
1023-
// searching forwards for boundaries we've got somewhere to search.
1024-
let snippet = if let Some(ref src) = local_begin.sf.src {
1025-
&src[start_index..]
1022+
let snippet = if let Some(src) = &local_begin.sf.src {
1023+
src
10261024
} else if let Some(src) = src.get_source() {
1027-
&src[start_index..]
1025+
src
10281026
} else {
10291027
return 1;
10301028
};
1031-
debug!("snippet=`{:?}`", snippet);
10321029

1033-
let mut target = if forwards { end_index + 1 } else { end_index - 1 };
1034-
debug!("initial target=`{:?}`", target);
1035-
1036-
while !snippet.is_char_boundary(target - start_index) && target < source_len {
1037-
target = if forwards {
1038-
target + 1
1039-
} else {
1040-
match target.checked_sub(1) {
1041-
Some(target) => target,
1042-
None => {
1043-
break;
1044-
}
1045-
}
1046-
};
1047-
debug!("target=`{:?}`", target);
1030+
if forwards {
1031+
(snippet.ceil_char_boundary(end_index + 1) - end_index) as u32
1032+
} else {
1033+
(end_index - snippet.floor_char_boundary(end_index - 1)) as u32
10481034
}
1049-
debug!("final target=`{:?}`", target);
1050-
1051-
if forwards { (target - end_index) as u32 } else { (end_index - target) as u32 }
10521035
}
10531036

10541037
pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {

0 commit comments

Comments
 (0)