summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2020-06-23 22:26:55 -0400
committerDan Davison <dandavison7@gmail.com>2020-06-25 12:35:07 -0400
commitc8caea49a8f5350a5a75e9bd3f761657dfd7e4bb (patch)
treee52253d043503e9b0568e13faa168529ea1275e7
parent06b50bd2f2155ee44d1f6049d4f39b3f06562b2b (diff)
Alternative regex match processing
-rw-r--r--src/features/numbers.rs6
-rw-r--r--src/paint.rs43
2 files changed, 15 insertions, 34 deletions
diff --git a/src/features/numbers.rs b/src/features/numbers.rs
index 401118b1..270bf9f7 100644
--- a/src/features/numbers.rs
+++ b/src/features/numbers.rs
@@ -66,9 +66,9 @@ pub mod tests {
println!("{}", output);
let output = strip_ansi_codes(&output);
let mut lines = output.lines().skip(4);
- assert_eq!(lines.next().unwrap(), " 1 1 ⋮ 1 │a = 1");
- assert_eq!(lines.next().unwrap(), " 2 2 ⋮ │b = 2");
- assert_eq!(lines.next().unwrap(), " ⋮ 2 │bb = 2");
+ assert_eq!(lines.next().unwrap(), " 1 1 ⋮ 1 │a = 1");
+ assert_eq!(lines.next().unwrap(), " 2 2 ⋮ │b = 2");
+ assert_eq!(lines.next().unwrap(), " ⋮ 2 │bb = 2");
}
const TWO_MINUS_LINES_DIFF: &str = "\
diff --git a/src/paint.rs b/src/paint.rs
index dab68901..ed4c9bf0 100644
--- a/src/paint.rs
+++ b/src/paint.rs
@@ -595,8 +595,7 @@ mod superimpose_style_sections {
}
lazy_static! {
- static ref LINE_NUMBER_REGEXP: Regex =
- Regex::new(r"(?P<before>.*?)(?P<ln>(%(lm|lp)))(?P<after>.*)").unwrap();
+ static ref LINE_NUMBER_REGEXP: Regex = Regex::new(r"%(lm|lp)").unwrap();
}
fn format_line_number(line_number: Option<usize>) -> String {
@@ -628,39 +627,21 @@ fn format_number_components<'a>(
) -> Vec<ansi_term::ANSIGenericString<'a, str>> {
let mut formatted_number_strings = Vec::new();
- for cap in LINE_NUMBER_REGEXP.captures_iter(&format_string) {
- let number_placeholder = cap.name("ln");
- let before = cap.name("before");
- let after = cap.name("after");
+ let mut offset = 0;
+ for m in LINE_NUMBER_REGEXP.find_iter(&format_string) {
+ let before = &format_string[offset..m.start()];
+ let number_placeholder = &format_string[m.start()..m.end()];
- match before {
- Some(s) => formatted_number_strings.push(
- number_format_style.paint(s.as_str())
- ),
- _ => (),
- }
+ formatted_number_strings.push(number_format_style.paint(before));
- match number_placeholder {
- Some(s) if Some(s.as_str()) == Some("%lm") => formatted_number_strings.push(
- number_minus_style.paint(format_line_number(minus))
- ),
- Some(s) if Some(s.as_str()) == Some("%lp") => formatted_number_strings.push(
- number_plus_style.paint(format_line_number(plus))
- ),
- Some(s) => formatted_number_strings.push(
- number_format_style.paint(s.as_str())
- ),
- _ => (),
+ if number_placeholder == "%lm" {
+ formatted_number_strings.push(number_minus_style.paint(format_line_number(minus)))
+ } else if number_placeholder == "%lp" {
+ formatted_number_strings.push(number_plus_style.paint(format_line_number(plus)))
}
-
- match after {
- Some(s) => formatted_number_strings.push(
- number_format_style.paint(s.as_str())
- ),
- _ => (),
- }
-
+ offset = m.end();
}
+ formatted_number_strings.push(number_format_style.paint(&format_string[offset..]));
formatted_number_strings
}