@@ -47,93 +47,50 @@ pub trait ToSanitizedUpperCase {
47
47
}
48
48
49
49
pub trait ToSanitizedSnakeCase {
50
- fn to_sanitized_snake_case ( & self ) -> Cow < str > ;
50
+ fn to_sanitized_not_keyword_snake_case ( & self ) -> Cow < str > ;
51
+ fn to_sanitized_snake_case ( & self ) -> Cow < str > {
52
+ let s = self . to_sanitized_not_keyword_snake_case ( ) ;
53
+ sanitize_keyword ( s)
54
+ }
51
55
}
52
56
53
57
impl ToSanitizedSnakeCase for str {
54
- fn to_sanitized_snake_case ( & self ) -> Cow < str > {
55
- macro_rules! keywords {
56
- ( $s: expr, $( $kw: ident) ,+, ) => {
57
- Cow :: from( match & $s. to_lowercase( ) [ ..] {
58
- $( stringify!( $kw) => concat!( stringify!( $kw) , "_" ) ) ,+,
59
- _ => return Cow :: from( $s. to_snake_case( ) )
60
- } )
61
- }
62
- }
58
+ fn to_sanitized_not_keyword_snake_case ( & self ) -> Cow < str > {
59
+ const INTERNALS : [ & str ; 4 ] = [ "set_bit" , "clear_bit" , "bit" , "bits" ] ;
63
60
64
61
let s = self . replace ( BLACKLIST_CHARS , "" ) ;
65
-
66
62
match s. chars ( ) . next ( ) . unwrap_or ( '\0' ) {
67
63
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
68
- Cow :: from ( format ! ( "_{}" , s. to_snake_case( ) ) )
64
+ format ! ( "_{}" , s. to_snake_case( ) ) . into ( )
69
65
}
70
66
_ => {
71
- keywords ! {
72
- s,
73
- abstract,
74
- alignof,
75
- as ,
76
- async ,
77
- await ,
78
- become,
79
- box,
80
- break ,
81
- const ,
82
- continue ,
83
- crate ,
84
- do,
85
- else,
86
- enum ,
87
- extern,
88
- false ,
89
- final,
90
- fn ,
91
- for ,
92
- if ,
93
- impl ,
94
- in,
95
- let ,
96
- loop ,
97
- macro,
98
- match ,
99
- mod ,
100
- move,
101
- mut ,
102
- offsetof,
103
- override,
104
- priv,
105
- proc,
106
- pub ,
107
- pure,
108
- ref,
109
- return ,
110
- self ,
111
- sizeof,
112
- static ,
113
- struct ,
114
- super ,
115
- trait ,
116
- true ,
117
- try,
118
- type ,
119
- typeof,
120
- unsafe ,
121
- unsized,
122
- use ,
123
- virtual,
124
- where ,
125
- while ,
126
- yield,
127
- set_bit,
128
- clear_bit,
129
- bit,
130
- bits,
67
+ let s = Cow :: from ( s. to_snake_case ( ) ) ;
68
+ if INTERNALS . contains ( & s. as_ref ( ) ) {
69
+ s + "_"
70
+ } else {
71
+ s
131
72
}
132
73
}
133
74
}
134
75
}
135
76
}
136
77
78
+ pub fn sanitize_keyword ( sc : Cow < str > ) -> Cow < str > {
79
+ const KEYWORDS : [ & str ; 54 ] = [
80
+ "abstract" , "alignof" , "as" , "async" , "await" , "become" , "box" , "break" , "const" ,
81
+ "continue" , "crate" , "do" , "else" , "enum" , "extern" , "false" , "final" , "fn" , "for" , "if" ,
82
+ "impl" , "in" , "let" , "loop" , "macro" , "match" , "mod" , "move" , "mut" , "offsetof" ,
83
+ "override" , "priv" , "proc" , "pub" , "pure" , "ref" , "return" , "self" , "sizeof" , "static" ,
84
+ "struct" , "super" , "trait" , "true" , "try" , "type" , "typeof" , "unsafe" , "unsized" , "use" ,
85
+ "virtual" , "where" , "while" , "yield" ,
86
+ ] ;
87
+ if KEYWORDS . contains ( & sc. as_ref ( ) ) {
88
+ sc + "_"
89
+ } else {
90
+ sc
91
+ }
92
+ }
93
+
137
94
impl ToSanitizedUpperCase for str {
138
95
fn to_sanitized_upper_case ( & self ) -> Cow < str > {
139
96
let s = self . replace ( BLACKLIST_CHARS , "" ) ;
0 commit comments