xref: /aosp_15_r20/external/grpc-grpc/src/compiler/ruby_generator_string-inl.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H
20 #define GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H
21 
22 #include <algorithm>
23 #include <sstream>
24 #include <vector>
25 
26 #include "src/compiler/config.h"
27 
28 using std::getline;
29 using std::transform;
30 
31 namespace grpc_ruby_generator {
32 
33 std::string RubifyConstant(const std::string& name);
34 
35 // Split splits a string using char into elems.
Split(const std::string & s,char delim,std::vector<std::string> * elems)36 inline std::vector<std::string>& Split(const std::string& s, char delim,
37                                        std::vector<std::string>* elems) {
38   std::stringstream ss(s);
39   std::string item;
40   while (getline(ss, item, delim)) {
41     elems->push_back(item);
42   }
43   return *elems;
44 }
45 
46 // Split splits a string using char, returning the result in a vector.
Split(const std::string & s,char delim)47 inline std::vector<std::string> Split(const std::string& s, char delim) {
48   std::vector<std::string> elems;
49   Split(s, delim, &elems);
50   return elems;
51 }
52 
53 // Replace replaces from with to in s.
Replace(std::string s,const std::string & from,const std::string & to)54 inline std::string Replace(std::string s, const std::string& from,
55                            const std::string& to) {
56   size_t start_pos = s.find(from);
57   if (start_pos == std::string::npos) {
58     return s;
59   }
60   s.replace(start_pos, from.length(), to);
61   return s;
62 }
63 
64 // ReplaceAll replaces all instances of search with replace in s.
ReplaceAll(std::string s,const std::string & search,const std::string & replace)65 inline std::string ReplaceAll(std::string s, const std::string& search,
66                               const std::string& replace) {
67   size_t pos = 0;
68   while ((pos = s.find(search, pos)) != std::string::npos) {
69     s.replace(pos, search.length(), replace);
70     pos += replace.length();
71   }
72   return s;
73 }
74 
75 // ReplacePrefix replaces from with to in s if search is a prefix of s.
ReplacePrefix(std::string * s,const std::string & from,const std::string & to)76 inline bool ReplacePrefix(std::string* s, const std::string& from,
77                           const std::string& to) {
78   size_t start_pos = s->find(from);
79   if (start_pos == std::string::npos || start_pos != 0) {
80     return false;
81   }
82   s->replace(start_pos, from.length(), to);
83   return true;
84 }
85 
86 // Modularize converts a string into a ruby module compatible name
Modularize(std::string s)87 inline std::string Modularize(std::string s) {
88   if (s.empty()) {
89     return s;
90   }
91   std::string new_string = "";
92   bool was_last_underscore = false;
93   new_string.append(1, ::toupper(s[0]));
94   for (std::string::size_type i = 1; i < s.size(); ++i) {
95     if (was_last_underscore && s[i] != '_') {
96       new_string.append(1, ::toupper(s[i]));
97     } else if (s[i] != '_') {
98       new_string.append(1, s[i]);
99     }
100     was_last_underscore = s[i] == '_';
101   }
102   return new_string;
103 }
104 
105 // RubyPackage gets the ruby package in either proto or ruby_package format
RubyPackage(const grpc::protobuf::FileDescriptor * file)106 inline std::string RubyPackage(const grpc::protobuf::FileDescriptor* file) {
107   std::string package_name = file->package();
108   if (file->options().has_ruby_package()) {
109     package_name = file->options().ruby_package();
110 
111     // If :: is in the package convert the Ruby formatted name
112     //    -> A::B::C
113     // to use the dot seperator notation
114     //    -> A.B.C
115     package_name = ReplaceAll(package_name, "::", ".");
116   }
117   return package_name;
118 }
119 
120 // RubyTypeOf updates a proto type to the required ruby equivalent.
RubyTypeOf(const grpc::protobuf::Descriptor * descriptor)121 inline std::string RubyTypeOf(const grpc::protobuf::Descriptor* descriptor) {
122   std::string proto_type = descriptor->full_name();
123   if (descriptor->file()->options().has_ruby_package()) {
124     // remove the leading package if present
125     ReplacePrefix(&proto_type, descriptor->file()->package(), "");
126     ReplacePrefix(&proto_type, ".", "");  // remove the leading . (no package)
127     proto_type = RubyPackage(descriptor->file()) + "." + proto_type;
128   }
129   std::string res("." + proto_type);
130   if (res.find('.') == std::string::npos) {
131     return res;
132   } else {
133     std::vector<std::string> prefixes_and_type = Split(res, '.');
134     res.clear();
135     for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) {
136       if (i != 0) {
137         res += "::";  // switch '.' to the ruby module delim
138       }
139       if (i < prefixes_and_type.size() - 1) {
140         res += Modularize(prefixes_and_type[i]);  // capitalize pkgs
141       } else {
142         res += RubifyConstant(prefixes_and_type[i]);
143       }
144     }
145     return res;
146   }
147 }
148 
149 }  // namespace grpc_ruby_generator
150 
151 #endif  // GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H
152