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