Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster few span methods #81735

Merged
merged 1 commit into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#![feature(nll)]
#![feature(min_specialization)]
#![feature(option_expect_none)]
#![feature(str_split_once)]

#[macro_use]
extern crate rustc_macros;
Expand Down
17 changes: 9 additions & 8 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ impl SourceMap {

pub fn is_line_before_span_empty(&self, sp: Span) -> bool {
match self.span_to_prev_source(sp) {
Ok(s) => s.split('\n').last().map_or(false, |l| l.trim_start().is_empty()),
Ok(s) => s.rsplit_once('\n').unwrap_or(("", &s)).1.trim_start().is_empty(),
Err(_) => false,
}
}
Expand Down Expand Up @@ -632,10 +632,11 @@ impl SourceMap {
pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
match self.span_to_prev_source(sp) {
Err(_) => None,
Ok(source) => source
.split('\n')
.last()
.map(|last_line| last_line.len() - last_line.trim_start().len()),
Ok(source) => {
let last_line = source.rsplit_once('\n').unwrap_or(("", &source)).1;

Some(last_line.len() - last_line.trim_start().len())
varkor marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand All @@ -651,7 +652,7 @@ impl SourceMap {
pub fn span_extend_to_prev_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
if let Ok(prev_source) = self.span_to_prev_source(sp) {
let prev_source = prev_source.rsplit(c).next().unwrap_or("");
if !prev_source.is_empty() && (!prev_source.contains('\n') || accept_newlines) {
if !prev_source.is_empty() && (accept_newlines || !prev_source.contains('\n')) {
return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
}
}
Expand All @@ -673,7 +674,7 @@ impl SourceMap {
let prev_source = prev_source.rsplit(&pat).next().unwrap_or("").trim_start();
if prev_source.is_empty() && sp.lo().0 != 0 {
return sp.with_lo(BytePos(sp.lo().0 - 1));
} else if !prev_source.contains('\n') || accept_newlines {
} else if accept_newlines || !prev_source.contains('\n') {
return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
}
}
Expand All @@ -693,7 +694,7 @@ impl SourceMap {
pub fn span_extend_to_next_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
if let Ok(next_source) = self.span_to_next_source(sp) {
let next_source = next_source.split(c).next().unwrap_or("");
if !next_source.is_empty() && (!next_source.contains('\n') || accept_newlines) {
if !next_source.is_empty() && (accept_newlines || !next_source.contains('\n')) {
return sp.with_hi(BytePos(sp.hi().0 + next_source.len() as u32));
}
}
Expand Down