62 lines
1.5 KiB
Rust
62 lines
1.5 KiB
Rust
// ----------------------------------------------------------------
|
|
// IMPORTS
|
|
// ----------------------------------------------------------------
|
|
|
|
extern crate regex;
|
|
|
|
use self::regex::Regex;
|
|
|
|
// ----------------------------------------------------------------
|
|
// METHODS get regex
|
|
// ----------------------------------------------------------------
|
|
|
|
/// Constructs RegEx and panics if error.
|
|
#[allow(dead_code)]
|
|
pub fn construct_regex(pattern: &str) -> Regex {
|
|
return Regex::new(pattern)
|
|
.expect("Invalid regex construction!");
|
|
}
|
|
|
|
// ----------------------------------------------------------------
|
|
// METHODS values
|
|
// ----------------------------------------------------------------
|
|
|
|
pub fn min<T>(x: T, y: T) -> T
|
|
where T: PartialOrd
|
|
{
|
|
return if y < x { y } else { x };
|
|
}
|
|
|
|
// ----------------------------------------------------------------
|
|
// METHODS Vectors
|
|
// ----------------------------------------------------------------
|
|
|
|
pub fn restrict<T>(x: &Vec<T>, i: usize, j: usize) -> Vec<T>
|
|
where T: Clone
|
|
{
|
|
return x[i..j].iter()
|
|
.cloned()
|
|
.collect::<Vec<T>>();
|
|
}
|
|
|
|
pub fn remove_last<T>(x: &Vec<T>) -> Vec<T>
|
|
where T: Clone
|
|
{
|
|
let n = x.len();
|
|
if n == 0 {
|
|
return Vec::<T>::new();
|
|
}
|
|
return restrict::<T>(x, 0, n-1);
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn remove_first<T>(x: &Vec<T>) -> Vec<T>
|
|
where T: Clone
|
|
{
|
|
let n = x.len();
|
|
if n == 0 {
|
|
return Vec::<T>::new();
|
|
}
|
|
return restrict::<T>(x, 0, n);
|
|
}
|