1 //! **heck** is a case conversion library.
2 //!
3 //! This library exists to provide case conversion between common cases like
4 //! CamelCase and snake_case. It is intended to be unicode aware, internally,
5 //! consistent, and reasonably well performing.
6 //!
7 //! ## Definition of a word boundary
8 //!
9 //! Word boundaries are defined as the "unicode words" defined in the
10 //! `unicode_segmentation` library, as well as within those words in this
11 //! manner:
12 //!
13 //! 1. All underscore characters are considered word boundaries.
14 //! 2. If an uppercase character is followed by lowercase letters, a word
15 //! boundary is considered to be just prior to that uppercase character.
16 //! 3. If multiple uppercase characters are consecutive, they are considered to
17 //! be within a single word, except that the last will be part of the next word
18 //! if it is followed by lowercase characters (see rule 2).
19 //!
20 //! That is, "HelloWorld" is segmented `Hello|World` whereas "XMLHttpRequest" is
21 //! segmented `XML|Http|Request`.
22 //!
23 //! Characters not within words (such as spaces, punctuations, and underscores)
24 //! are not included in the output string except as they are a part of the case
25 //! being converted to. Multiple adjacent word boundaries (such as a series of
26 //! underscores) are folded into one. ("hello__world" in snake case is therefore
27 //! "hello_world", not the exact same string). Leading or trailing word boundary
28 //! indicators are dropped, except insofar as CamelCase capitalizes the first
29 //! word.
30 //!
31 //! ### Cases contained in this library:
32 //!
33 //! 1. CamelCase
34 //! 2. snake_case
35 //! 3. kebab-case
36 //! 4. SHOUTY_SNAKE_CASE
37 //! 5. mixedCase
38 //! 6. Title Case
39 //! 7. SHOUTY-KEBAB-CASE
40 #![deny(missing_docs)]
41
42 mod camel;
43 mod kebab;
44 mod mixed;
45 mod shouty_kebab;
46 mod shouty_snake;
47 mod snake;
48 mod title;
49
50 pub use camel::CamelCase;
51 pub use kebab::KebabCase;
52 pub use mixed::MixedCase;
53 pub use shouty_kebab::ShoutyKebabCase;
54 pub use shouty_snake::{ShoutySnakeCase, ShoutySnekCase};
55 pub use snake::{SnakeCase, SnekCase};
56 pub use title::TitleCase;
57
58 use unicode_segmentation::UnicodeSegmentation;
59
transform<F, G>(s: &str, with_word: F, boundary: G) -> String where F: Fn(&str, &mut String), G: Fn(&mut String),60 fn transform<F, G>(s: &str, with_word: F, boundary: G) -> String
61 where
62 F: Fn(&str, &mut String),
63 G: Fn(&mut String),
64 {
65 /// Tracks the current 'mode' of the transformation algorithm as it scans
66 /// the input string.
67 ///
68 /// The mode is a tri-state which tracks the case of the last cased
69 /// character of the current word. If there is no cased character
70 /// (either lowercase or uppercase) since the previous word boundary,
71 /// than the mode is `Boundary`. If the last cased character is lowercase,
72 /// then the mode is `Lowercase`. Othertherwise, the mode is
73 /// `Uppercase`.
74 #[derive(Clone, Copy, PartialEq)]
75 enum WordMode {
76 /// There have been no lowercase or uppercase characters in the current
77 /// word.
78 Boundary,
79 /// The previous cased character in the current word is lowercase.
80 Lowercase,
81 /// The previous cased character in the current word is uppercase.
82 Uppercase,
83 }
84
85 let mut out = String::new();
86 let mut first_word = true;
87
88 for word in s.unicode_words() {
89 let mut char_indices = word.char_indices().peekable();
90 let mut init = 0;
91 let mut mode = WordMode::Boundary;
92
93 while let Some((i, c)) = char_indices.next() {
94 // Skip underscore characters
95 if c == '_' {
96 if init == i {
97 init += 1;
98 }
99 continue;
100 }
101
102 if let Some(&(next_i, next)) = char_indices.peek() {
103 // The mode including the current character, assuming the
104 // current character does not result in a word boundary.
105 let next_mode = if c.is_lowercase() {
106 WordMode::Lowercase
107 } else if c.is_uppercase() {
108 WordMode::Uppercase
109 } else {
110 mode
111 };
112
113 // Word boundary after if next is underscore or current is
114 // not uppercase and next is uppercase
115 if next == '_' || (next_mode == WordMode::Lowercase && next.is_uppercase()) {
116 if !first_word {
117 boundary(&mut out);
118 }
119 with_word(&word[init..next_i], &mut out);
120 first_word = false;
121 init = next_i;
122 mode = WordMode::Boundary;
123
124 // Otherwise if current and previous are uppercase and next
125 // is lowercase, word boundary before
126 } else if mode == WordMode::Uppercase && c.is_uppercase() && next.is_lowercase() {
127 if !first_word {
128 boundary(&mut out);
129 } else {
130 first_word = false;
131 }
132 with_word(&word[init..i], &mut out);
133 init = i;
134 mode = WordMode::Boundary;
135
136 // Otherwise no word boundary, just update the mode
137 } else {
138 mode = next_mode;
139 }
140 } else {
141 // Collect trailing characters as a word
142 if !first_word {
143 boundary(&mut out);
144 } else {
145 first_word = false;
146 }
147 with_word(&word[init..], &mut out);
148 break;
149 }
150 }
151 }
152
153 out
154 }
155
lowercase(s: &str, out: &mut String)156 fn lowercase(s: &str, out: &mut String) {
157 let mut chars = s.chars().peekable();
158 while let Some(c) = chars.next() {
159 if c == 'Σ' && chars.peek().is_none() {
160 out.push('ς');
161 } else {
162 out.extend(c.to_lowercase());
163 }
164 }
165 }
166
uppercase(s: &str, out: &mut String)167 fn uppercase(s: &str, out: &mut String) {
168 for c in s.chars() {
169 out.extend(c.to_uppercase())
170 }
171 }
172
capitalize(s: &str, out: &mut String)173 fn capitalize(s: &str, out: &mut String) {
174 let mut char_indices = s.char_indices();
175 if let Some((_, c)) = char_indices.next() {
176 out.extend(c.to_uppercase());
177 if let Some((i, _)) = char_indices.next() {
178 lowercase(&s[i..], out);
179 }
180 }
181 }
182