My Project
StringExtras.h
Go to the documentation of this file.
1 //===- StringExtras.h - String utilities used by MLIR -----------*- C++ -*-===//
2 //
3 // Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains string utility functions used within MLIR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_SUPPORT_STRINGEXTRAS_H
14 #define MLIR_SUPPORT_STRINGEXTRAS_H
15 
16 #include "llvm/ADT/StringExtras.h"
17 
18 #include <cctype>
19 
20 namespace mlir {
24 inline std::string convertToSnakeCase(llvm::StringRef input) {
25  std::string snakeCase;
26  snakeCase.reserve(input.size());
27  for (auto c : input) {
28  if (std::isupper(c)) {
29  if (!snakeCase.empty() && snakeCase.back() != '_') {
30  snakeCase.push_back('_');
31  }
32  snakeCase.push_back(llvm::toLower(c));
33  } else {
34  snakeCase.push_back(c);
35  }
36  }
37  return snakeCase;
38 }
39 
44 inline std::string convertToCamelCase(llvm::StringRef input,
45  bool capitalizeFirst = false) {
46  if (input.empty()) {
47  return "";
48  }
49  std::string output;
50  output.reserve(input.size());
51  size_t pos = 0;
52  if (capitalizeFirst && std::islower(input[pos])) {
53  output.push_back(llvm::toUpper(input[pos]));
54  pos++;
55  }
56  while (pos < input.size()) {
57  auto cur = input[pos];
58  if (cur == '_') {
59  if (pos && (pos + 1 < input.size())) {
60  if (std::islower(input[pos + 1])) {
61  output.push_back(llvm::toUpper(input[pos + 1]));
62  pos += 2;
63  continue;
64  }
65  }
66  }
67  output.push_back(cur);
68  pos++;
69  }
70  return output;
71 }
72 } // namespace mlir
73 
74 #endif // MLIR_SUPPORT_STRINGEXTRAS_H
Definition: InferTypeOpInterface.cpp:20
std::string convertToCamelCase(llvm::StringRef input, bool capitalizeFirst=false)
Definition: StringExtras.h:44
std::string convertToSnakeCase(llvm::StringRef input)
Definition: StringExtras.h:24