[llvm] [llvm][Support] fix convertToSnakeFromCamelCase (PR #68375)

Maksim Levental via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 6 10:17:36 PDT 2023


================
@@ -96,18 +97,13 @@ std::string llvm::convertToSnakeFromCamelCase(StringRef input) {
   if (input.empty())
     return "";
 
-  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() != '_')
-      snakeCase.push_back('_');
-    snakeCase.push_back(llvm::toLower(c));
+  std::string snakeCase = input.str();
+  for (int i = 0; i < 10; ++i) {
+    snakeCase = llvm::Regex("([A-Z]+)([A-Z][a-z])").sub("\\1_\\2", snakeCase);
+    snakeCase = llvm::Regex("([a-z0-9])([A-Z])").sub("\\1_\\2", snakeCase);
----------------
makslevental wrote:

> should probably hoist the `Regex` construction out of the loop

D'oh

https://github.com/llvm/llvm-project/pull/68375


More information about the llvm-commits mailing list