1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#![deny(missing_docs)]
extern crate unicode_segmentation;
mod camel;
mod kebab;
mod mixed;
mod shouty_snake;
mod snake;
mod title;
pub use camel::CamelCase;
pub use kebab::KebabCase;
pub use mixed::MixedCase;
pub use shouty_snake::{ShoutySnakeCase, ShoutySnekCase};
pub use snake::{SnakeCase, SnekCase};
pub use title::TitleCase;
use unicode_segmentation::UnicodeSegmentation;
fn transform<F, G>(s: &str, word_boundary: F, not_word_boundary: G) -> String
where
F: Fn(char, &mut String),
G: Fn(char, &mut String),
{
let mut out = String::new();
let mut after_word_boundary = false;
for word in s.unicode_words() {
if out.len() != 0 { after_word_boundary = true; }
let mut last_c_was_uppercase = false;
let mut multiple_uppercase = false;
for c in word.chars() {
if c == '_' {
after_word_boundary = true;
continue
}
if c.is_uppercase() {
if out.len() != 0 && !last_c_was_uppercase {
after_word_boundary = true;
}
if last_c_was_uppercase {
multiple_uppercase = true;
}
last_c_was_uppercase = true;
} else {
if multiple_uppercase && !after_word_boundary {
after_word_boundary = true;
}
multiple_uppercase = false;
last_c_was_uppercase = false;
}
if after_word_boundary {
word_boundary(c, &mut out);
} else {
not_word_boundary(c, &mut out);
}
after_word_boundary = false;
}
}
out
}