From d82d9fc808bfa3f99844ed40f622c8a53e2d267e Mon Sep 17 00:00:00 2001 From: Kamal Marhubi Date: Mon, 7 Mar 2016 13:41:32 -0500 Subject: utils: Add CodeMapSpanUtils trait for span_* methods This commit adds a CodeMapSpanUtils extension trait on CodeMap, and moves some functions to methods there: - span_after - span_after_last - span_before This better reflects them being lookup methods on the codemap. --- src/utils.rs | 62 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 27 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 3b03ede2be3..5c2bca73432 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -20,42 +20,50 @@ use rewrite::{Rewrite, RewriteContext}; use SKIP_ANNOTATION; -// Computes the length of a string's last line, minus offset. -#[inline] -pub fn extra_offset(text: &str, offset: Indent) -> usize { - match text.rfind('\n') { - // 1 for newline character - Some(idx) => text.len() - idx - 1 - offset.width(), - None => text.len(), - } +pub trait CodeMapSpanUtils { + fn span_after(&self, original: Span, needle: &str) -> BytePos; + fn span_after_last(&self, original: Span, needle: &str) -> BytePos; + fn span_before(&self, original: Span, needle: &str) -> BytePos; } -#[inline] -pub fn span_after(original: Span, needle: &str, codemap: &CodeMap) -> BytePos { - let snippet = codemap.span_to_snippet(original).unwrap(); - let offset = snippet.find_uncommented(needle).unwrap() + needle.len(); +impl CodeMapSpanUtils for CodeMap { + #[inline] + fn span_after(&self, original: Span, needle: &str) -> BytePos { + let snippet = self.span_to_snippet(original).unwrap(); + let offset = snippet.find_uncommented(needle).unwrap() + needle.len(); - original.lo + BytePos(offset as u32) -} + original.lo + BytePos(offset as u32) + } -#[inline] -pub fn span_before(original: Span, needle: &str, codemap: &CodeMap) -> BytePos { - let snippet = codemap.span_to_snippet(original).unwrap(); - let offset = snippet.find_uncommented(needle).unwrap(); + #[inline] + fn span_after_last(&self, original: Span, needle: &str) -> BytePos { + let snippet = self.span_to_snippet(original).unwrap(); + let mut offset = 0; - original.lo + BytePos(offset as u32) -} + while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) { + offset += additional_offset + needle.len(); + } -#[inline] -pub fn span_after_last(original: Span, needle: &str, codemap: &CodeMap) -> BytePos { - let snippet = codemap.span_to_snippet(original).unwrap(); - let mut offset = 0; + original.lo + BytePos(offset as u32) + } + + #[inline] + fn span_before(&self, original: Span, needle: &str) -> BytePos { + let snippet = self.span_to_snippet(original).unwrap(); + let offset = snippet.find_uncommented(needle).unwrap(); - while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) { - offset += additional_offset + needle.len(); + original.lo + BytePos(offset as u32) } +} - original.lo + BytePos(offset as u32) +// Computes the length of a string's last line, minus offset. +#[inline] +pub fn extra_offset(text: &str, offset: Indent) -> usize { + match text.rfind('\n') { + // 1 for newline character + Some(idx) => text.len() - idx - 1 - offset.width(), + None => text.len(), + } } #[inline] -- cgit 1.4.1-3-g733a5