fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. #include <vector>
  5.  
  6. typedef std::vector<std::string> DType;
  7.  
  8. DType MakeVector() {
  9. return { "snake_case","ODAI00_99_TEST","x_0_x","UpperCamelCase" };
  10. }
  11. int SelectChar(int Ch) {
  12. if (std::isdigit(Ch)) {
  13. return 1;//number
  14. }
  15. if (std::isalpha(Ch)) {//alphabet
  16. return 2;//upper
  17. }
  18.  
  19. return 0;//graph
  20. }
  21. DType Split(const std::string& D){
  22. std::string S;
  23. DType R;
  24. int St = SelectChar(D.front());
  25. bool F = true;
  26. for(std::size_t i=0;i<D.size();i++){
  27. int V = SelectChar(D[i]);
  28. if (St == V) {
  29. if (F) {
  30. S += std::toupper(D[i]);
  31. F = false;
  32. }
  33. else {
  34. S += std::tolower(D[i]);
  35.  
  36. }
  37. }
  38. else {
  39. St = V;
  40. R.push_back(S);
  41. S.clear();
  42. i--;
  43. F = true;
  44. }
  45. }
  46. if (S.size()) R.push_back(S);
  47. return R;
  48. }
  49.  
  50. std::string MakeHoge(const std::string& S) {
  51. DType D = Split(S);
  52. std::string R;
  53.  
  54. for (std::size_t i = 0; i < D.size(); i++) {
  55. if (D[i][0] == '_') {
  56. if (static_cast<long long int>(i) - 1 >= 0) {
  57. if (!std::isdigit(D[i - 1].back())) { continue; }
  58. if (static_cast<long long int>(i) + 1 < D.size() && std::isdigit(D[i + 1].front())) { R += '_'; }
  59. }
  60. continue;
  61. }
  62. R += D[i];
  63. }
  64. return R;
  65. }
  66. int main() {
  67. DType D = MakeVector();
  68.  
  69. for (auto& o : D) {
  70. auto R = MakeHoge(o);
  71. std::cout << o << " => " << R << std::endl;
  72. }
  73.  
  74. return 0;
  75. }
Success #stdin #stdout 0s 4404KB
stdin
Standard input is empty
stdout
snake_case => SnakeCase
ODAI00_99_TEST => Odai00_99Test
x_0_x => X0X
UpperCamelCase => Uppercamelcase