]> git.proxmox.com Git - rustc.git/blob - vendor/heck/src/lib.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / vendor / heck / src / lib.rs
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. UpperCamelCase
34 //! 2. lowerCamelCase
35 //! 3. snake_case
36 //! 4. kebab-case
37 //! 5. SHOUTY_SNAKE_CASE
38 //! 6. Title Case
39 //! 7. SHOUTY-KEBAB-CASE
40 #![deny(missing_docs)]
41 #![forbid(unsafe_code)]
42
43 mod kebab;
44 mod lower_camel;
45 mod shouty_kebab;
46 mod shouty_snake;
47 mod snake;
48 mod title;
49 mod upper_camel;
50
51 pub use kebab::{AsKebabCase, ToKebabCase};
52 pub use lower_camel::{AsLowerCamelCase, ToLowerCamelCase};
53 pub use shouty_kebab::{AsShoutyKebabCase, ToShoutyKebabCase};
54 pub use shouty_snake::{
55 AsShoutySnakeCase, AsShoutySnakeCase as AsShoutySnekCase, ToShoutySnakeCase, ToShoutySnekCase,
56 };
57 pub use snake::{AsSnakeCase, AsSnakeCase as AsSnekCase, ToSnakeCase, ToSnekCase};
58 pub use title::{AsTitleCase, ToTitleCase};
59 pub use upper_camel::{
60 AsUpperCamelCase, AsUpperCamelCase as AsPascalCase, ToPascalCase, ToUpperCamelCase,
61 };
62
63 use std::fmt;
64
65 #[cfg(feature = "unicode")]
66 fn get_iterator(s: &str) -> unicode_segmentation::UnicodeWords {
67 use unicode_segmentation::UnicodeSegmentation;
68 s.unicode_words()
69 }
70 #[cfg(not(feature = "unicode"))]
71 fn get_iterator(s: &str) -> impl Iterator<Item = &str> {
72 s.split(|letter: char| !letter.is_ascii_alphanumeric())
73 }
74
75 fn transform<F, G>(
76 s: &str,
77 mut with_word: F,
78 mut boundary: G,
79 f: &mut fmt::Formatter,
80 ) -> fmt::Result
81 where
82 F: FnMut(&str, &mut fmt::Formatter) -> fmt::Result,
83 G: FnMut(&mut fmt::Formatter) -> fmt::Result,
84 {
85 /// Tracks the current 'mode' of the transformation algorithm as it scans
86 /// the input string.
87 ///
88 /// The mode is a tri-state which tracks the case of the last cased
89 /// character of the current word. If there is no cased character
90 /// (either lowercase or uppercase) since the previous word boundary,
91 /// than the mode is `Boundary`. If the last cased character is lowercase,
92 /// then the mode is `Lowercase`. Othertherwise, the mode is
93 /// `Uppercase`.
94 #[derive(Clone, Copy, PartialEq)]
95 enum WordMode {
96 /// There have been no lowercase or uppercase characters in the current
97 /// word.
98 Boundary,
99 /// The previous cased character in the current word is lowercase.
100 Lowercase,
101 /// The previous cased character in the current word is uppercase.
102 Uppercase,
103 }
104
105 let mut first_word = true;
106
107 for word in get_iterator(s) {
108 let mut char_indices = word.char_indices().peekable();
109 let mut init = 0;
110 let mut mode = WordMode::Boundary;
111
112 while let Some((i, c)) = char_indices.next() {
113 // Skip underscore characters
114 if c == '_' {
115 if init == i {
116 init += 1;
117 }
118 continue;
119 }
120
121 if let Some(&(next_i, next)) = char_indices.peek() {
122 // The mode including the current character, assuming the
123 // current character does not result in a word boundary.
124 let next_mode = if c.is_lowercase() {
125 WordMode::Lowercase
126 } else if c.is_uppercase() {
127 WordMode::Uppercase
128 } else {
129 mode
130 };
131
132 // Word boundary after if next is underscore or current is
133 // not uppercase and next is uppercase
134 if next == '_' || (next_mode == WordMode::Lowercase && next.is_uppercase()) {
135 if !first_word {
136 boundary(f)?;
137 }
138 with_word(&word[init..next_i], f)?;
139 first_word = false;
140 init = next_i;
141 mode = WordMode::Boundary;
142
143 // Otherwise if current and previous are uppercase and next
144 // is lowercase, word boundary before
145 } else if mode == WordMode::Uppercase && c.is_uppercase() && next.is_lowercase() {
146 if !first_word {
147 boundary(f)?;
148 } else {
149 first_word = false;
150 }
151 with_word(&word[init..i], f)?;
152 init = i;
153 mode = WordMode::Boundary;
154
155 // Otherwise no word boundary, just update the mode
156 } else {
157 mode = next_mode;
158 }
159 } else {
160 // Collect trailing characters as a word
161 if !first_word {
162 boundary(f)?;
163 } else {
164 first_word = false;
165 }
166 with_word(&word[init..], f)?;
167 break;
168 }
169 }
170 }
171
172 Ok(())
173 }
174
175 fn lowercase(s: &str, f: &mut fmt::Formatter) -> fmt::Result {
176 let mut chars = s.chars().peekable();
177 while let Some(c) = chars.next() {
178 if c == 'Σ' && chars.peek().is_none() {
179 write!(f, "ς")?;
180 } else {
181 write!(f, "{}", c.to_lowercase())?;
182 }
183 }
184
185 Ok(())
186 }
187
188 fn uppercase(s: &str, f: &mut fmt::Formatter) -> fmt::Result {
189 for c in s.chars() {
190 write!(f, "{}", c.to_uppercase())?;
191 }
192
193 Ok(())
194 }
195
196 fn capitalize(s: &str, f: &mut fmt::Formatter) -> fmt::Result {
197 let mut char_indices = s.char_indices();
198 if let Some((_, c)) = char_indices.next() {
199 write!(f, "{}", c.to_uppercase())?;
200 if let Some((i, _)) = char_indices.next() {
201 lowercase(&s[i..], f)?;
202 }
203 }
204
205 Ok(())
206 }