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

Maksim Levental via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 6 08:16:19 PDT 2023


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

>From 84ac205bfabea45eafb72e0efe01e8840b023aa6 Mon Sep 17 00:00:00 2001
From: max <maksim.levental at gmail.com>
Date: Thu, 5 Oct 2023 21:00:37 -0500
Subject: [PATCH 1/3] [llvm][Support] fix convertToSnakeFromCamelCase

---
 llvm/lib/Support/StringExtras.cpp       | 18 +++++++-----------
 llvm/unittests/ADT/StringExtrasTest.cpp |  5 +++++
 2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/llvm/lib/Support/StringExtras.cpp b/llvm/lib/Support/StringExtras.cpp
index 5683d7005584eb2..fd5a34fb3d6e82c 100644
--- a/llvm/lib/Support/StringExtras.cpp
+++ b/llvm/lib/Support/StringExtras.cpp
@@ -12,6 +12,7 @@
 
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/Regex.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cctype>
 
@@ -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);
   }
+  std::transform(snakeCase.begin(), snakeCase.end(), snakeCase.begin(),
+                 [](unsigned char c) { return std::tolower(c); });
   return snakeCase;
 }
 
diff --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp
index 3f69c91b270a355..fab562f1ed0d594 100644
--- a/llvm/unittests/ADT/StringExtrasTest.cpp
+++ b/llvm/unittests/ADT/StringExtrasTest.cpp
@@ -184,6 +184,11 @@ TEST(StringExtrasTest, ConvertToSnakeFromCamelCase) {
 
   testConvertToSnakeCase("OpName", "op_name");
   testConvertToSnakeCase("opName", "op_name");
+  testConvertToSnakeCase("OPName", "op_name");
+  testConvertToSnakeCase("opNAME", "op_name");
+  testConvertToSnakeCase("opNAMe", "op_na_me");
+  testConvertToSnakeCase("opnameE", "opname_e");
+  testConvertToSnakeCase("OPNameOPName", "op_name_op_name");
   testConvertToSnakeCase("_OpName", "_op_name");
   testConvertToSnakeCase("Op_Name", "op_name");
   testConvertToSnakeCase("", "");

>From d9a6b83d3650baecd70326e33fe6518e18fa34ff Mon Sep 17 00:00:00 2001
From: max <maksim.levental at gmail.com>
Date: Fri, 6 Oct 2023 10:11:46 -0500
Subject: [PATCH 2/3] kick CI

---
 llvm/lib/Support/StringExtras.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/lib/Support/StringExtras.cpp b/llvm/lib/Support/StringExtras.cpp
index fd5a34fb3d6e82c..019ba266afe607a 100644
--- a/llvm/lib/Support/StringExtras.cpp
+++ b/llvm/lib/Support/StringExtras.cpp
@@ -97,6 +97,7 @@ std::string llvm::convertToSnakeFromCamelCase(StringRef input) {
   if (input.empty())
     return "";
 
+
   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);

>From f2b18a6e6d506e48562fff950ae8ebca4b82e8f3 Mon Sep 17 00:00:00 2001
From: max <maksim.levental at gmail.com>
Date: Fri, 6 Oct 2023 10:16:02 -0500
Subject: [PATCH 3/3] Revert "kick CI"

This reverts commit d9a6b83d3650baecd70326e33fe6518e18fa34ff.
---
 llvm/lib/Support/StringExtras.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/lib/Support/StringExtras.cpp b/llvm/lib/Support/StringExtras.cpp
index 019ba266afe607a..fd5a34fb3d6e82c 100644
--- a/llvm/lib/Support/StringExtras.cpp
+++ b/llvm/lib/Support/StringExtras.cpp
@@ -97,7 +97,6 @@ std::string llvm::convertToSnakeFromCamelCase(StringRef input) {
   if (input.empty())
     return "";
 
-
   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);



More information about the llvm-commits mailing list