Debian Codegraph @

helix-core/src/case_conversion.rs

raw symbols SCIP
1 use crate::Tendril;
2
3 // todo: should this be grapheme aware?
4
5 pub fn to_pascal_case(text: impl Iterator<Item = char>) -> Tendril {
6 let mut res = Tendril::new();
7 to_pascal_case_with(text, &mut res);
8 res
9 }
10
11 pub fn to_pascal_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
12 let mut at_word_start = true;
13 for c in text {
14 // we don't count _ as a word char here so case conversions work well
15 if !c.is_alphanumeric() {
16 at_word_start = true;
17 continue;
18 }
19 if at_word_start {
20 at_word_start = false;
21 buf.extend(c.to_uppercase());
22 } else {
23 buf.push(c)
24 }
25 }
26 }
27
28 pub fn to_upper_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
29 for c in text {
30 for c in c.to_uppercase() {
31 buf.push(c)
32 }
33 }
34 }
35
36 pub fn to_lower_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
37 for c in text {
38 for c in c.to_lowercase() {
39 buf.push(c)
40 }
41 }
42 }
43
44 pub fn to_camel_case(text: impl Iterator<Item = char>) -> Tendril {
45 let mut res = Tendril::new();
46 to_camel_case_with(text, &mut res);
47 res
48 }
49 pub fn to_camel_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
50 let mut first = true;
51 let mut at_word_start = false;
52
53 for c in text {
54 if !c.is_alphanumeric() {
55 at_word_start = true;
56 continue;
57 }
58
59 if first {
60 buf.extend(c.to_lowercase());
61 first = false;
62 } else if at_word_start {
63 at_word_start = false;
64 buf.extend(c.to_uppercase());
65 } else {
66 buf.extend(c.to_lowercase());
67 }
68 }
69 }
70
71 #[cfg(test)]
72 mod tests {
73 use super::*;
74
75 #[test]
76 fn test_camel_case_underscore() {
77 let result = to_camel_case("otto_botto".chars());
78 assert_eq!(result.as_ref() as &str, "ottoBotto");
79 }
80
81 #[test]
82 fn test_camel_case_uppercase() {
83 let result = to_camel_case("OTTO_BOTTO".chars());
84 assert_eq!(result.as_ref() as &str, "ottoBotto");
85 }
86
87 #[test]
88 fn test_camel_case_mixed_case() {
89 let result = to_camel_case("OttO_boTTO".chars());
90 assert_eq!(result.as_ref() as &str, "ottoBotto");
91 }
92
93 #[test]
94 fn test_camel_case_includes_nums() {
95 let result = to_camel_case("Ott0_b0TT0".chars());
96 assert_eq!(result.as_ref() as &str, "ott0B0tt0");
97 }
98
99 #[test]
100 fn test_camel_case_one_word_lower() {
101 let result = to_camel_case("otto".chars());
102 assert_eq!(result.as_ref() as &str, "otto");
103 }
104
105 #[test]
106 fn test_camel_case_one_word_upper() {
107 let result = to_camel_case("OTTO".chars());
108 assert_eq!(result.as_ref() as &str, "otto");
109 }
110
111 #[test]
112 fn test_camel_case_one_char_lower() {
113 let result = to_camel_case("o".chars());
114 assert_eq!(result.as_ref() as &str, "o");
115 }
116
117 #[test]
118 fn test_camel_case_one_char_upper() {
119 let result = to_camel_case("O".chars());
120 assert_eq!(result.as_ref() as &str, "o");
121 }
122
123 #[test]
124 fn test_camel_case_many_words_separators() {
125 let result = to_camel_case("otto_botto_the_dog".chars());
126 assert_eq!(result.as_ref() as &str, "ottoBottoTheDog");
127 }
128
129 #[test]
130 fn test_camel_case_empty_string() {
131 let result = to_camel_case("".chars());
132 assert_eq!(result.as_ref() as &str, "");
133 }
134 }