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 "src/compiler/config.h"
23
24 #include <algorithm>
25 #include <sstream>
26 #include <vector>
27
28 using std::getline;
29 using std::transform;
30
31 namespace grpc_ruby_generator {
32
33 // Split splits a string using char into elems.
Split(const grpc::string & s,char delim,std::vector<grpc::string> * elems)34 inline std::vector<grpc::string>& Split(const grpc::string& s, char delim,
35 std::vector<grpc::string>* elems) {
36 std::stringstream ss(s);
37 grpc::string item;
38 while (getline(ss, item, delim)) {
39 elems->push_back(item);
40 }
41 return *elems;
42 }
43
44 // Split splits a string using char, returning the result in a vector.
Split(const grpc::string & s,char delim)45 inline std::vector<grpc::string> Split(const grpc::string& s, char delim) {
46 std::vector<grpc::string> elems;
47 Split(s, delim, &elems);
48 return elems;
49 }
50
51 // Replace replaces from with to in s.
Replace(grpc::string s,const grpc::string & from,const grpc::string & to)52 inline grpc::string Replace(grpc::string s, const grpc::string& from,
53 const grpc::string& to) {
54 size_t start_pos = s.find(from);
55 if (start_pos == grpc::string::npos) {
56 return s;
57 }
58 s.replace(start_pos, from.length(), to);
59 return s;
60 }
61
62 // ReplaceAll replaces all instances of search with replace in s.
ReplaceAll(grpc::string s,const grpc::string & search,const grpc::string & replace)63 inline grpc::string ReplaceAll(grpc::string s, const grpc::string& search,
64 const grpc::string& replace) {
65 size_t pos = 0;
66 while ((pos = s.find(search, pos)) != grpc::string::npos) {
67 s.replace(pos, search.length(), replace);
68 pos += replace.length();
69 }
70 return s;
71 }
72
73 // ReplacePrefix replaces from with to in s if search is a prefix of s.
ReplacePrefix(grpc::string * s,const grpc::string & from,const grpc::string & to)74 inline bool ReplacePrefix(grpc::string* s, const grpc::string& from,
75 const grpc::string& to) {
76 size_t start_pos = s->find(from);
77 if (start_pos == grpc::string::npos || start_pos != 0) {
78 return false;
79 }
80 s->replace(start_pos, from.length(), to);
81 return true;
82 }
83
84 // Modularize converts a string into a ruby module compatible name
Modularize(grpc::string s)85 inline grpc::string Modularize(grpc::string s) {
86 if (s.empty()) {
87 return s;
88 }
89 grpc::string new_string = "";
90 bool was_last_underscore = false;
91 new_string.append(1, ::toupper(s[0]));
92 for (grpc::string::size_type i = 1; i < s.size(); ++i) {
93 if (was_last_underscore && s[i] != '_') {
94 new_string.append(1, ::toupper(s[i]));
95 } else if (s[i] != '_') {
96 new_string.append(1, s[i]);
97 }
98 was_last_underscore = s[i] == '_';
99 }
100 return new_string;
101 }
102
103 // RubyTypeOf updates a proto type to the required ruby equivalent.
RubyTypeOf(const grpc::string & a_type,const grpc::string & package)104 inline grpc::string RubyTypeOf(const grpc::string& a_type,
105 const grpc::string& package) {
106 grpc::string res(a_type);
107 ReplacePrefix(&res, package, ""); // remove the leading package if present
108 ReplacePrefix(&res, ".", ""); // remove the leading . (no package)
109 if (res.find('.') == grpc::string::npos) {
110 return res;
111 } else {
112 std::vector<grpc::string> prefixes_and_type = Split(res, '.');
113 res.clear();
114 for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) {
115 if (i != 0) {
116 res += "::"; // switch '.' to the ruby module delim
117 }
118 if (i < prefixes_and_type.size() - 1) {
119 res += Modularize(prefixes_and_type[i]); // capitalize pkgs
120 } else {
121 res += prefixes_and_type[i];
122 }
123 }
124 return res;
125 }
126 }
127
128 } // namespace grpc_ruby_generator
129
130 #endif // GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H
131