-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.rs
47 lines (40 loc) · 1.11 KB
/
util.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use unic_ucd_category::GeneralCategory;
use unicode_segmentation::UnicodeSegmentation;
pub fn repeat_chars(ch: &str, n: usize) -> String {
std::iter::repeat(ch).take(n).collect()
}
pub fn is_alphabetic(s: &str) -> bool {
s.chars().map(GeneralCategory::of).all(|c| c.is_letter())
}
pub fn is_whitespace(s: &str) -> bool {
s.chars().all(char::is_whitespace)
}
pub fn string_length(word: &str) -> usize {
word.graphemes(true).count()
}
pub fn fmt_token_pointer(token_value: &str, col: usize) -> (String, String) {
(
repeat_chars(" ", col - 1),
repeat_chars("^", string_length(token_value)),
)
}
pub fn fmt_list<'a, T: std::fmt::Display>(elems: &'a [T], sep: &str, linker: &str) -> String {
if let [rest @ .., last] = elems {
if rest.is_empty() {
format!("{}", last)
} else {
format!(
"{}{} {} {}",
rest.iter().map(T::to_string).collect::<Vec<_>>().join(sep),
if rest.len() == 1 { "" } else { "," },
linker,
last
)
}
} else {
"".into()
}
}
pub fn to_string_vec(v: Vec<&str>) -> Vec<String> {
v.into_iter().map(String::from).collect()
}