Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[llvm][Support] fix convertToSnakeFromCamelCase #68375

Merged
merged 5 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions llvm/lib/Support/StringExtras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,18 @@ std::string llvm::convertToSnakeFromCamelCase(StringRef input) {

std::string snakeCase;
snakeCase.reserve(input.size());
for (char c : input) {
if (!std::isupper(c)) {
snakeCase.push_back(c);
continue;
}

if (!snakeCase.empty() && snakeCase.back() != '_')
auto check = [&input](size_t j, std::function<bool(int)> check) {
makslevental marked this conversation as resolved.
Show resolved Hide resolved
makslevental marked this conversation as resolved.
Show resolved Hide resolved
return j < input.size() ? check(input[j]) : false;
};
for (size_t i = 0; i < input.size(); ++i) {
snakeCase.push_back(input[i]);
if (check(i, isupper) && check(i + 1, isupper) && check(i + 2, islower))
makslevental marked this conversation as resolved.
Show resolved Hide resolved
snakeCase.push_back('_');
if ((check(i, islower) || check(i, isdigit)) && check(i + 1, isupper))
snakeCase.push_back('_');
snakeCase.push_back(llvm::toLower(c));
}
std::transform(snakeCase.begin(), snakeCase.end(), snakeCase.begin(),
[](unsigned char c) { return std::tolower(c); });
makslevental marked this conversation as resolved.
Show resolved Hide resolved
return snakeCase;
}

Expand Down
6 changes: 6 additions & 0 deletions llvm/unittests/ADT/StringExtrasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ TEST(StringExtrasTest, ConvertToSnakeFromCamelCase) {

testConvertToSnakeCase("OpName", "op_name");
testConvertToSnakeCase("opName", "op_name");
testConvertToSnakeCase("OPName", "op_name");
testConvertToSnakeCase("Intel_OCL_BI", "intel_ocl_bi");
testConvertToSnakeCase("opNAME", "op_name");
testConvertToSnakeCase("opNAMe", "op_na_me");
testConvertToSnakeCase("opnameE", "opname_e");
testConvertToSnakeCase("OPNameOPName", "op_name_op_name");
makslevental marked this conversation as resolved.
Show resolved Hide resolved
testConvertToSnakeCase("_OpName", "_op_name");
testConvertToSnakeCase("Op_Name", "op_name");
testConvertToSnakeCase("", "");
Expand Down
Loading