Calendar (WIP)
This commit is contained in:
parent
9d3f9e9c60
commit
79a0aeb1b8
24 changed files with 613 additions and 231 deletions
|
|
@ -39,44 +39,3 @@ pub fn get_url_from_host(host: &str) -> String {
|
|||
host
|
||||
)
|
||||
}
|
||||
|
||||
pub fn substitute(str: &str, pattern: &str, replacements: &[&str]) -> String {
|
||||
let mut result = String::with_capacity(
|
||||
(str.len() + replacements.iter().map(|s| s.len()).sum::<usize>())
|
||||
.saturating_sub(pattern.len() * replacements.len()),
|
||||
);
|
||||
|
||||
let mut i = 0;
|
||||
for s in str.split(pattern) {
|
||||
result.push_str(s);
|
||||
if i < replacements.len() {
|
||||
result.push_str(replacements[i]);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if i == 1 {
|
||||
return str.to_string();
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_substitute() {
|
||||
assert_eq!(substitute("", "", &[]), "");
|
||||
assert_eq!(substitute("", "", &[""]), "");
|
||||
assert_eq!(substitute("", "{}", &["a"]), "");
|
||||
assert_eq!(substitute("a", "{}", &["b"]), "a");
|
||||
assert_eq!(substitute("a{}", "{}", &["b"]), "ab");
|
||||
assert_eq!(substitute("{}c", "{}", &["b"]), "bc");
|
||||
assert_eq!(substitute("a{}c", "{}", &["b"]), "abc");
|
||||
assert_eq!(substitute("{}b{}", "{}", &["a", "c"]), "abc");
|
||||
assert_eq!(substitute("{}{}{}", "{}", &["a", "bc", "def"]), "abcdef");
|
||||
assert_eq!(substitute("{}{}{}", "{}", &["a"]), "a");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue