Skip to content

Commit 60cca83

Browse files
committed
faster spans
1 parent 6ad11e2 commit 60cca83

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

compiler/rustc_span/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#![feature(nll)]
2323
#![feature(min_specialization)]
2424
#![feature(option_expect_none)]
25+
#![feature(str_split_once)]
2526

2627
#[macro_use]
2728
extern crate rustc_macros;

compiler/rustc_span/src/source_map.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ impl SourceMap {
539539

540540
pub fn is_line_before_span_empty(&self, sp: Span) -> bool {
541541
match self.span_to_prev_source(sp) {
542-
Ok(s) => s.split('\n').last().map_or(false, |l| l.trim_start().is_empty()),
542+
Ok(s) => s.rsplit_once('\n').unwrap_or(("", &s)).1.trim_start().is_empty(),
543543
Err(_) => false,
544544
}
545545
}
@@ -632,10 +632,11 @@ impl SourceMap {
632632
pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
633633
match self.span_to_prev_source(sp) {
634634
Err(_) => None,
635-
Ok(source) => source
636-
.split('\n')
637-
.last()
638-
.map(|last_line| last_line.len() - last_line.trim_start().len()),
635+
Ok(source) => {
636+
let last_line = source.rsplit_once('\n').unwrap_or(("", &source)).1;
637+
638+
Some(last_line.len() - last_line.trim_start().len())
639+
}
639640
}
640641
}
641642

@@ -651,7 +652,7 @@ impl SourceMap {
651652
pub fn span_extend_to_prev_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
652653
if let Ok(prev_source) = self.span_to_prev_source(sp) {
653654
let prev_source = prev_source.rsplit(c).next().unwrap_or("");
654-
if !prev_source.is_empty() && (!prev_source.contains('\n') || accept_newlines) {
655+
if !prev_source.is_empty() && (accept_newlines || !prev_source.contains('\n')) {
655656
return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
656657
}
657658
}
@@ -673,7 +674,7 @@ impl SourceMap {
673674
let prev_source = prev_source.rsplit(&pat).next().unwrap_or("").trim_start();
674675
if prev_source.is_empty() && sp.lo().0 != 0 {
675676
return sp.with_lo(BytePos(sp.lo().0 - 1));
676-
} else if !prev_source.contains('\n') || accept_newlines {
677+
} else if accept_newlines || !prev_source.contains('\n') {
677678
return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
678679
}
679680
}
@@ -693,7 +694,7 @@ impl SourceMap {
693694
pub fn span_extend_to_next_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
694695
if let Ok(next_source) = self.span_to_next_source(sp) {
695696
let next_source = next_source.split(c).next().unwrap_or("");
696-
if !next_source.is_empty() && (!next_source.contains('\n') || accept_newlines) {
697+
if !next_source.is_empty() && (accept_newlines || !next_source.contains('\n')) {
697698
return sp.with_hi(BytePos(sp.hi().0 + next_source.len() as u32));
698699
}
699700
}

0 commit comments

Comments
 (0)