mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-01-31 20:51:52 +01:00
[llvm][StringExtras] Merge StringExtras from MLIR into LLVM
Summary: This revision adds two utilities currently present in MLIR to LLVM StringExtras: * convertToSnakeFromCamelCase Convert a string from a camel case naming scheme, to a snake case scheme * convertToCamelFromSnakeCase Convert a string from a snake case naming scheme, to a camel case scheme Differential Revision: https://reviews.llvm.org/D78167
This commit is contained in:
parent
ca9019a39c
commit
7df90ad0d7
@ -292,6 +292,18 @@ void printHTMLEscaped(StringRef String, raw_ostream &Out);
|
||||
/// printLowerCase - Print each character as lowercase if it is uppercase.
|
||||
void printLowerCase(StringRef String, raw_ostream &Out);
|
||||
|
||||
/// Converts a string from camel-case to snake-case by replacing all uppercase
|
||||
/// letters with '_' followed by the letter in lowercase, except if the
|
||||
/// uppercase letter is the first character of the string.
|
||||
std::string convertToSnakeFromCamelCase(StringRef input);
|
||||
|
||||
/// Converts a string from snake-case to camel-case by replacing all occurrences
|
||||
/// of '_' followed by a lowercase letter with the letter in uppercase.
|
||||
/// Optionally allow capitalization of the first letter (if it is a lowercase
|
||||
/// letter)
|
||||
std::string convertToCamelFromSnakeCase(StringRef input,
|
||||
bool capitalizeFirst = false);
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename IteratorT>
|
||||
|
@ -90,3 +90,46 @@ void llvm::printLowerCase(StringRef String, raw_ostream &Out) {
|
||||
for (const char C : String)
|
||||
Out << toLower(C);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
return snakeCase;
|
||||
}
|
||||
|
||||
std::string llvm::convertToCamelFromSnakeCase(StringRef input,
|
||||
bool capitalizeFirst) {
|
||||
if (input.empty())
|
||||
return "";
|
||||
|
||||
std::string output;
|
||||
output.reserve(input.size());
|
||||
|
||||
// Push the first character, capatilizing if necessary.
|
||||
if (capitalizeFirst && std::islower(input.front()))
|
||||
output.push_back(llvm::toUpper(input.front()));
|
||||
else
|
||||
output.push_back(input.front());
|
||||
|
||||
// Walk the input converting any `*_[a-z]` snake case into `*[A-Z]` camelCase.
|
||||
for (size_t pos = 1, e = input.size(); pos < e; ++pos) {
|
||||
if (input[pos] == '_' && pos != (e - 1) && std::islower(input[pos + 1]))
|
||||
output.push_back(llvm::toUpper(input[++pos]));
|
||||
else
|
||||
output.push_back(input[pos]);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
@ -118,3 +118,56 @@ TEST(StringExtrasTest, printHTMLEscaped) {
|
||||
printHTMLEscaped("ABCdef123&<>\"'", OS);
|
||||
EXPECT_EQ("ABCdef123&<>"'", OS.str());
|
||||
}
|
||||
|
||||
TEST(StringExtras, ConvertToSnakeFromCamelCase) {
|
||||
auto testConvertToSnakeCase = [](llvm::StringRef input,
|
||||
llvm::StringRef expected) {
|
||||
EXPECT_EQ(convertToSnakeFromCamelCase(input), expected.str());
|
||||
};
|
||||
|
||||
testConvertToSnakeCase("OpName", "op_name");
|
||||
testConvertToSnakeCase("opName", "op_name");
|
||||
testConvertToSnakeCase("_OpName", "_op_name");
|
||||
testConvertToSnakeCase("Op_Name", "op_name");
|
||||
testConvertToSnakeCase("", "");
|
||||
testConvertToSnakeCase("A", "a");
|
||||
testConvertToSnakeCase("_", "_");
|
||||
testConvertToSnakeCase("a", "a");
|
||||
testConvertToSnakeCase("op_name", "op_name");
|
||||
testConvertToSnakeCase("_op_name", "_op_name");
|
||||
testConvertToSnakeCase("__op_name", "__op_name");
|
||||
testConvertToSnakeCase("op__name", "op__name");
|
||||
}
|
||||
|
||||
TEST(StringExtras, ConvertToCamelFromSnakeCase) {
|
||||
auto testConvertToCamelCase = [](bool capitalizeFirst, llvm::StringRef input,
|
||||
llvm::StringRef expected) {
|
||||
EXPECT_EQ(convertToCamelFromSnakeCase(input, capitalizeFirst),
|
||||
expected.str());
|
||||
};
|
||||
|
||||
testConvertToCamelCase(false, "op_name", "opName");
|
||||
testConvertToCamelCase(false, "_op_name", "_opName");
|
||||
testConvertToCamelCase(false, "__op_name", "_OpName");
|
||||
testConvertToCamelCase(false, "op__name", "op_Name");
|
||||
testConvertToCamelCase(false, "", "");
|
||||
testConvertToCamelCase(false, "A", "A");
|
||||
testConvertToCamelCase(false, "_", "_");
|
||||
testConvertToCamelCase(false, "a", "a");
|
||||
testConvertToCamelCase(false, "OpName", "OpName");
|
||||
testConvertToCamelCase(false, "opName", "opName");
|
||||
testConvertToCamelCase(false, "_OpName", "_OpName");
|
||||
testConvertToCamelCase(false, "Op_Name", "Op_Name");
|
||||
testConvertToCamelCase(true, "op_name", "OpName");
|
||||
testConvertToCamelCase(true, "_op_name", "_opName");
|
||||
testConvertToCamelCase(true, "__op_name", "_OpName");
|
||||
testConvertToCamelCase(true, "op__name", "Op_Name");
|
||||
testConvertToCamelCase(true, "", "");
|
||||
testConvertToCamelCase(true, "A", "A");
|
||||
testConvertToCamelCase(true, "_", "_");
|
||||
testConvertToCamelCase(true, "a", "A");
|
||||
testConvertToCamelCase(true, "OpName", "OpName");
|
||||
testConvertToCamelCase(true, "_OpName", "_OpName");
|
||||
testConvertToCamelCase(true, "Op_Name", "Op_Name");
|
||||
testConvertToCamelCase(true, "opName", "OpName");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user