• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 by non-alphanumeric characters, as well as
10 //! within those words in this manner:
11 //!
12 //! 1. If an uppercase character is followed by lowercase letters, a word
13 //! boundary is considered to be just prior to that uppercase character.
14 //! 2. If multiple uppercase characters are consecutive, they are considered to
15 //! be within a single word, except that the last will be part of the next word
16 //! if it is followed by lowercase characters (see rule 1).
17 //!
18 //! That is, "HelloWorld" is segmented `Hello|World` whereas "XMLHttpRequest" is
19 //! segmented `XML|Http|Request`.
20 //!
21 //! Characters not within words (such as spaces, punctuations, and underscores)
22 //! are not included in the output string except as they are a part of the case
23 //! being converted to. Multiple adjacent word boundaries (such as a series of
24 //! underscores) are folded into one. ("hello__world" in snake case is therefore
25 //! "hello_world", not the exact same string). Leading or trailing word boundary
26 //! indicators are dropped, except insofar as CamelCase capitalizes the first
27 //! word.
28 //!
29 //! ### Cases contained in this library:
30 //!
31 //! 1. UpperCamelCase
32 //! 2. lowerCamelCase
33 //! 3. snake_case
34 //! 4. kebab-case
35 //! 5. SHOUTY_SNAKE_CASE
36 //! 6. Title Case
37 //! 7. SHOUTY-KEBAB-CASE
38 //! 8. Train-Case
39 #![deny(missing_docs)]
40 #![forbid(unsafe_code)]
41 #![no_std]
42 
43 extern crate alloc;
44 
45 mod kebab;
46 mod lower_camel;
47 mod shouty_kebab;
48 mod shouty_snake;
49 mod snake;
50 mod title;
51 mod train;
52 mod upper_camel;
53 
54 pub use kebab::{AsKebabCase, ToKebabCase};
55 pub use lower_camel::{AsLowerCamelCase, ToLowerCamelCase};
56 pub use shouty_kebab::{AsShoutyKebabCase, ToShoutyKebabCase};
57 pub use shouty_snake::{
58     AsShoutySnakeCase, AsShoutySnakeCase as AsShoutySnekCase, ToShoutySnakeCase, ToShoutySnekCase,
59 };
60 pub use snake::{AsSnakeCase, AsSnakeCase as AsSnekCase, ToSnakeCase, ToSnekCase};
61 pub use title::{AsTitleCase, ToTitleCase};
62 pub use train::{AsTrainCase, ToTrainCase};
63 pub use upper_camel::{
64     AsUpperCamelCase, AsUpperCamelCase as AsPascalCase, ToPascalCase, ToUpperCamelCase,
65 };
66 
67 use core::fmt;
68 
transform<F, G>( s: &str, mut with_word: F, mut boundary: G, f: &mut fmt::Formatter, ) -> fmt::Result where F: FnMut(&str, &mut fmt::Formatter) -> fmt::Result, G: FnMut(&mut fmt::Formatter) -> fmt::Result,69 fn transform<F, G>(
70     s: &str,
71     mut with_word: F,
72     mut boundary: G,
73     f: &mut fmt::Formatter,
74 ) -> fmt::Result
75 where
76     F: FnMut(&str, &mut fmt::Formatter) -> fmt::Result,
77     G: FnMut(&mut fmt::Formatter) -> fmt::Result,
78 {
79     /// Tracks the current 'mode' of the transformation algorithm as it scans
80     /// the input string.
81     ///
82     /// The mode is a tri-state which tracks the case of the last cased
83     /// character of the current word. If there is no cased character
84     /// (either lowercase or uppercase) since the previous word boundary,
85     /// than the mode is `Boundary`. If the last cased character is lowercase,
86     /// then the mode is `Lowercase`. Othertherwise, the mode is
87     /// `Uppercase`.
88     #[derive(Clone, Copy, PartialEq)]
89     enum WordMode {
90         /// There have been no lowercase or uppercase characters in the current
91         /// word.
92         Boundary,
93         /// The previous cased character in the current word is lowercase.
94         Lowercase,
95         /// The previous cased character in the current word is uppercase.
96         Uppercase,
97     }
98 
99     let mut first_word = true;
100 
101     for word in s.split(|c: char| !c.is_alphanumeric()) {
102         let mut char_indices = word.char_indices().peekable();
103         let mut init = 0;
104         let mut mode = WordMode::Boundary;
105 
106         while let Some((i, c)) = char_indices.next() {
107             if let Some(&(next_i, next)) = char_indices.peek() {
108                 // The mode including the current character, assuming the
109                 // current character does not result in a word boundary.
110                 let next_mode = if c.is_lowercase() {
111                     WordMode::Lowercase
112                 } else if c.is_uppercase() {
113                     WordMode::Uppercase
114                 } else {
115                     mode
116                 };
117 
118                 // Word boundary after if current is not uppercase and next
119                 // is uppercase
120                 if next_mode == WordMode::Lowercase && next.is_uppercase() {
121                     if !first_word {
122                         boundary(f)?;
123                     }
124                     with_word(&word[init..next_i], f)?;
125                     first_word = false;
126                     init = next_i;
127                     mode = WordMode::Boundary;
128 
129                 // Otherwise if current and previous are uppercase and next
130                 // is lowercase, word boundary before
131                 } else if mode == WordMode::Uppercase && c.is_uppercase() && next.is_lowercase() {
132                     if !first_word {
133                         boundary(f)?;
134                     } else {
135                         first_word = false;
136                     }
137                     with_word(&word[init..i], f)?;
138                     init = i;
139                     mode = WordMode::Boundary;
140 
141                 // Otherwise no word boundary, just update the mode
142                 } else {
143                     mode = next_mode;
144                 }
145             } else {
146                 // Collect trailing characters as a word
147                 if !first_word {
148                     boundary(f)?;
149                 } else {
150                     first_word = false;
151                 }
152                 with_word(&word[init..], f)?;
153                 break;
154             }
155         }
156     }
157 
158     Ok(())
159 }
160 
lowercase(s: &str, f: &mut fmt::Formatter) -> fmt::Result161 fn lowercase(s: &str, f: &mut fmt::Formatter) -> fmt::Result {
162     let mut chars = s.chars().peekable();
163     while let Some(c) = chars.next() {
164         if c == 'Σ' && chars.peek().is_none() {
165             write!(f, "ς")?;
166         } else {
167             write!(f, "{}", c.to_lowercase())?;
168         }
169     }
170 
171     Ok(())
172 }
173 
uppercase(s: &str, f: &mut fmt::Formatter) -> fmt::Result174 fn uppercase(s: &str, f: &mut fmt::Formatter) -> fmt::Result {
175     for c in s.chars() {
176         write!(f, "{}", c.to_uppercase())?;
177     }
178 
179     Ok(())
180 }
181 
capitalize(s: &str, f: &mut fmt::Formatter) -> fmt::Result182 fn capitalize(s: &str, f: &mut fmt::Formatter) -> fmt::Result {
183     let mut char_indices = s.char_indices();
184     if let Some((_, c)) = char_indices.next() {
185         write!(f, "{}", c.to_uppercase())?;
186         if let Some((i, _)) = char_indices.next() {
187             lowercase(&s[i..], f)?;
188         }
189     }
190 
191     Ok(())
192 }
193