1 //! **heck** is a case conversion library.
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.
7 //! ## Definition of a word boundary
9 //! Word boundaries are defined as the "unicode words" defined in the
10 //! `unicode_segmentation` library, as well as within those words in this
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).
20 //! That is, "HelloWorld" is segmented `Hello|World` whereas "XMLHttpRequest" is
21 //! segmented `XML|Http|Request`.
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
31 //! ### Cases contained in this library:
37 //! 5. SHOUTY_SNAKE_CASE
39 //! 7. SHOUTY-KEBAB-CASE
40 #![deny(missing_docs)]
41 #![forbid(unsafe_code)]
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
,
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
,
65 #[cfg(feature = "unicode")]
66 fn get_iterator(s
: &str) -> unicode_segmentation
::UnicodeWords
{
67 use unicode_segmentation
::UnicodeSegmentation
;
70 #[cfg(not(feature = "unicode"))]
71 fn get_iterator(s
: &str) -> impl Iterator
<Item
= &str> {
72 s
.split(|letter
: char| !letter
.is_ascii_alphanumeric())
79 f
: &mut fmt
::Formatter
,
82 F
: FnMut(&str, &mut fmt
::Formatter
) -> fmt
::Result
,
83 G
: FnMut(&mut fmt
::Formatter
) -> fmt
::Result
,
85 /// Tracks the current 'mode' of the transformation algorithm as it scans
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
94 #[derive(Clone, Copy, PartialEq)]
96 /// There have been no lowercase or uppercase characters in the current
99 /// The previous cased character in the current word is lowercase.
101 /// The previous cased character in the current word is uppercase.
105 let mut first_word
= true;
107 for word
in get_iterator(s
) {
108 let mut char_indices
= word
.char_indices().peekable();
110 let mut mode
= WordMode
::Boundary
;
112 while let Some((i
, c
)) = char_indices
.next() {
113 // Skip underscore characters
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() {
126 } else if c
.is_uppercase() {
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()) {
138 with_word(&word
[init
..next_i
], f
)?
;
141 mode
= WordMode
::Boundary
;
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() {
151 with_word(&word
[init
..i
], f
)?
;
153 mode
= WordMode
::Boundary
;
155 // Otherwise no word boundary, just update the mode
160 // Collect trailing characters as a word
166 with_word(&word
[init
..], f
)?
;
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() {
181 write
!(f
, "{}", c
.to_lowercase())?
;
188 fn uppercase(s
: &str, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
190 write
!(f
, "{}", c
.to_uppercase())?
;
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
)?
;