2018-01-06 05:03:30 +08:00
|
|
|
use itertools::Itertools;
|
2018-12-13 07:44:15 +08:00
|
|
|
use regex::Regex;
|
2019-06-21 09:53:24 +08:00
|
|
|
use std::ops::Bound::{Excluded, Included, Unbounded};
|
|
|
|
use std::ops::RangeBounds;
|
2018-01-06 05:03:30 +08:00
|
|
|
|
|
|
|
/// Take a range of lines from a string.
|
2019-06-21 09:53:24 +08:00
|
|
|
pub fn take_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String {
|
|
|
|
let start = match range.start_bound() {
|
|
|
|
Excluded(&n) => n + 1,
|
|
|
|
Included(&n) => n,
|
|
|
|
Unbounded => 0,
|
|
|
|
};
|
2018-01-06 05:03:30 +08:00
|
|
|
let mut lines = s.lines().skip(start);
|
2019-06-21 09:53:24 +08:00
|
|
|
match range.end_bound() {
|
|
|
|
Excluded(end) => lines.take(end.saturating_sub(start)).join("\n"),
|
|
|
|
Included(end) => lines.take((end + 1).saturating_sub(start)).join("\n"),
|
|
|
|
Unbounded => lines.join("\n"),
|
2018-01-06 05:03:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 07:44:15 +08:00
|
|
|
/// Take anchored lines from a string.
|
|
|
|
/// Lines containing anchor are ignored.
|
|
|
|
pub fn take_anchored_lines(s: &str, anchor: &str) -> String {
|
|
|
|
lazy_static! {
|
|
|
|
static ref RE_START: Regex = Regex::new(r"ANCHOR:\s*(?P<anchor_name>[\w_-]+)").unwrap();
|
|
|
|
static ref RE_END: Regex = Regex::new(r"ANCHOR_END:\s*(?P<anchor_name>[\w_-]+)").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut retained = Vec::<&str>::new();
|
|
|
|
let mut anchor_found = false;
|
|
|
|
|
|
|
|
for l in s.lines() {
|
|
|
|
if anchor_found {
|
|
|
|
match RE_END.captures(l) {
|
|
|
|
Some(cap) => {
|
|
|
|
if &cap["anchor_name"] == anchor {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
if !RE_START.is_match(l) {
|
|
|
|
retained.push(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let Some(cap) = RE_START.captures(l) {
|
|
|
|
if &cap["anchor_name"] == anchor {
|
|
|
|
anchor_found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retained.join("\n")
|
|
|
|
}
|
|
|
|
|
2018-01-06 05:03:30 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-12-13 07:44:15 +08:00
|
|
|
use super::{take_anchored_lines, take_lines};
|
2018-01-06 05:03:30 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn take_lines_test() {
|
|
|
|
let s = "Lorem\nipsum\ndolor\nsit\namet";
|
2018-01-31 18:57:47 +08:00
|
|
|
assert_eq!(take_lines(s, 1..3), "ipsum\ndolor");
|
2018-01-06 05:03:30 +08:00
|
|
|
assert_eq!(take_lines(s, 3..), "sit\namet");
|
|
|
|
assert_eq!(take_lines(s, ..3), "Lorem\nipsum\ndolor");
|
|
|
|
assert_eq!(take_lines(s, ..), s);
|
2018-01-31 18:57:47 +08:00
|
|
|
// corner cases
|
|
|
|
assert_eq!(take_lines(s, 4..3), "");
|
|
|
|
assert_eq!(take_lines(s, ..100), s);
|
2018-01-06 05:03:30 +08:00
|
|
|
}
|
2018-12-13 07:44:15 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn take_anchored_lines_test() {
|
|
|
|
let s = "Lorem\nipsum\ndolor\nsit\namet";
|
|
|
|
assert_eq!(take_anchored_lines(s, "test"), "");
|
|
|
|
|
|
|
|
let s = "Lorem\nipsum\ndolor\nANCHOR_END: test\nsit\namet";
|
|
|
|
assert_eq!(take_anchored_lines(s, "test"), "");
|
|
|
|
|
|
|
|
let s = "Lorem\nipsum\nANCHOR: test\ndolor\nsit\namet";
|
|
|
|
assert_eq!(take_anchored_lines(s, "test"), "dolor\nsit\namet");
|
|
|
|
assert_eq!(take_anchored_lines(s, "something"), "");
|
|
|
|
|
|
|
|
let s = "Lorem\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nipsum";
|
|
|
|
assert_eq!(take_anchored_lines(s, "test"), "dolor\nsit\namet");
|
|
|
|
assert_eq!(take_anchored_lines(s, "something"), "");
|
|
|
|
|
|
|
|
let s = "Lorem\nANCHOR: test\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nipsum";
|
|
|
|
assert_eq!(take_anchored_lines(s, "test"), "ipsum\ndolor\nsit\namet");
|
|
|
|
assert_eq!(take_anchored_lines(s, "something"), "");
|
|
|
|
|
|
|
|
let s = "Lorem\nANCHOR: test2\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nANCHOR_END:test2\nipsum";
|
|
|
|
assert_eq!(
|
|
|
|
take_anchored_lines(s, "test2"),
|
|
|
|
"ipsum\ndolor\nsit\namet\nlorem"
|
|
|
|
);
|
|
|
|
assert_eq!(take_anchored_lines(s, "test"), "dolor\nsit\namet");
|
|
|
|
assert_eq!(take_anchored_lines(s, "something"), "");
|
|
|
|
}
|
2018-01-06 05:03:30 +08:00
|
|
|
}
|