File tree 2 files changed +16
-8
lines changed
2 files changed +16
-8
lines changed Original file line number Diff line number Diff line change @@ -98,15 +98,16 @@ std::string llvm::convertToSnakeFromCamelCase(StringRef input) {
98
98
99
99
std::string snakeCase;
100
100
snakeCase.reserve (input.size ());
101
- for (char c : input) {
102
- if (!std::isupper (c)) {
103
- snakeCase.push_back (c);
104
- continue ;
105
- }
106
-
107
- if (!snakeCase.empty () && snakeCase.back () != ' _' )
101
+ auto check = [&input](size_t j, function_ref<bool (int )> predicate) {
102
+ return j < input.size () && predicate (input[j]);
103
+ };
104
+ for (size_t i = 0 ; i < input.size (); ++i) {
105
+ snakeCase.push_back (tolower (input[i]));
106
+ // Handles "runs" of capitals, such as in OPName -> op_name.
107
+ if (check (i, isupper) && check (i + 1 , isupper) && check (i + 2 , islower))
108
+ snakeCase.push_back (' _' );
109
+ if ((check (i, islower) || check (i, isdigit)) && check (i + 1 , isupper))
108
110
snakeCase.push_back (' _' );
109
- snakeCase.push_back (llvm::toLower (c));
110
111
}
111
112
return snakeCase;
112
113
}
Original file line number Diff line number Diff line change @@ -184,6 +184,13 @@ TEST(StringExtrasTest, ConvertToSnakeFromCamelCase) {
184
184
185
185
testConvertToSnakeCase (" OpName" , " op_name" );
186
186
testConvertToSnakeCase (" opName" , " op_name" );
187
+ testConvertToSnakeCase (" OPName" , " op_name" );
188
+ testConvertToSnakeCase (" Intel_OCL_BI" , " intel_ocl_bi" );
189
+ testConvertToSnakeCase (" I32Attr" , " i32_attr" );
190
+ testConvertToSnakeCase (" opNAME" , " op_name" );
191
+ testConvertToSnakeCase (" opNAMe" , " op_na_me" );
192
+ testConvertToSnakeCase (" opnameE" , " opname_e" );
193
+ testConvertToSnakeCase (" OPNameOPName" , " op_name_op_name" );
187
194
testConvertToSnakeCase (" _OpName" , " _op_name" );
188
195
testConvertToSnakeCase (" Op_Name" , " op_name" );
189
196
testConvertToSnakeCase (" " , " " );
You can’t perform that action at this time.
0 commit comments