1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 15:40:03 +08:00

add split_kv

This commit is contained in:
2020-05-02 12:58:22 +08:00
parent 46528c566c
commit 57c757d770

30
src/util_str.rs Normal file
View File

@@ -0,0 +1,30 @@
pub fn split_kv(s: &str, split: char) -> (String, String) {
let mut k = String::new();
let mut v = String::new();
let mut is_splited = false;
let cs = s.chars();
for c in cs {
if is_splited {
v.push(c);
} else if c == split {
is_splited = true;
} else {
k.push(c);
}
}
(k, v)
}
#[test]
fn test_split_kv() {
assert_eq!(("".to_owned(), "".to_owned()), split_kv("", '='));
assert_eq!(("aaaa".to_owned(), "".to_owned()), split_kv("aaaa", '='));
assert_eq!(("".to_owned(), "aaaa".to_owned()), split_kv("=aaaa", '='));
assert_eq!(("aa".to_owned(), "bb".to_owned()), split_kv("aa=bb", '='));
assert_eq!(("aa".to_owned(), "bb".to_owned()), split_kv("aa:bb", ':'));
}