Last active
August 26, 2017 01:16
-
-
Save Songbird0/a93e739c8220682a7f0545574febc704 to your computer and use it in GitHub Desktop.
(personal draft) tag replacing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const INLINE_TAG: &'static str = "[inline]"; | |
const CLOSED_INLINE_TAG: &'static str = "[/inline]"; | |
fn main() { | |
let raw_input = "`hello`"; | |
let couple = (INLINE_TAG, CLOSED_INLINE_TAG); | |
let delimiter = "`"; | |
let wrapped_content = wrap_with_couple_of_tag(raw_input.to_owned(), couple, delimiter); | |
assert_eq!(wrapped_content, "[inline]hello[/inline]"); | |
} | |
fn wrap_with_couple_of_tag(sentence: String, couple: (&str, &str), delimiter: &str) -> String { | |
let mut _sentence = sentence; | |
if _sentence.len() < 2 { | |
eprintln!("the sentence length must be (equal or) greater than 2."); | |
} | |
let first_idx: usize = _sentence.find(delimiter).unwrap_or_else(|| panic!("first element not found")); | |
_sentence.remove(first_idx); | |
// last removed element | |
_sentence.pop(); | |
let mut wrapped_foo = String::new(); | |
let (opened_tag, closed_tag) = couple; | |
wrapped_foo.push_str(opened_tag); | |
wrapped_foo.push_str(_sentence.as_str()); | |
wrapped_foo.push_str(closed_tag); | |
std::mem::drop(_sentence); | |
wrapped_foo | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment