gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
ruby_generator_string-inl.h
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H
35 #define GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H
36 
37 #include "src/compiler/config.h"
38 
39 #include <algorithm>
40 #include <sstream>
41 #include <vector>
42 
43 using std::getline;
44 using std::transform;
45 
46 namespace grpc_ruby_generator {
47 
48 // Split splits a string using char into elems.
49 inline std::vector<grpc::string> &Split(const grpc::string &s, char delim,
50  std::vector<grpc::string> *elems) {
51  std::stringstream ss(s);
52  grpc::string item;
53  while (getline(ss, item, delim)) {
54  elems->push_back(item);
55  }
56  return *elems;
57 }
58 
59 // Split splits a string using char, returning the result in a vector.
60 inline std::vector<grpc::string> Split(const grpc::string &s, char delim) {
61  std::vector<grpc::string> elems;
62  Split(s, delim, &elems);
63  return elems;
64 }
65 
66 // Replace replaces from with to in s.
67 inline grpc::string Replace(grpc::string s, const grpc::string &from,
68  const grpc::string &to) {
69  size_t start_pos = s.find(from);
70  if (start_pos == grpc::string::npos) {
71  return s;
72  }
73  s.replace(start_pos, from.length(), to);
74  return s;
75 }
76 
77 // ReplaceAll replaces all instances of search with replace in s.
78 inline grpc::string ReplaceAll(grpc::string s, const grpc::string &search,
79  const grpc::string &replace) {
80  size_t pos = 0;
81  while ((pos = s.find(search, pos)) != grpc::string::npos) {
82  s.replace(pos, search.length(), replace);
83  pos += replace.length();
84  }
85  return s;
86 }
87 
88 // ReplacePrefix replaces from with to in s if search is a prefix of s.
89 inline bool ReplacePrefix(grpc::string *s, const grpc::string &from,
90  const grpc::string &to) {
91  size_t start_pos = s->find(from);
92  if (start_pos == grpc::string::npos || start_pos != 0) {
93  return false;
94  }
95  s->replace(start_pos, from.length(), to);
96  return true;
97 }
98 
99 // CapitalizeFirst capitalizes the first char in a string.
100 inline grpc::string CapitalizeFirst(grpc::string s) {
101  if (s.empty()) {
102  return s;
103  }
104  s[0] = ::toupper(s[0]);
105  return s;
106 }
107 
108 // RubyTypeOf updates a proto type to the required ruby equivalent.
109 inline grpc::string RubyTypeOf(const grpc::string &a_type,
110  const grpc::string &package) {
111  grpc::string res(a_type);
112  ReplacePrefix(&res, package, ""); // remove the leading package if present
113  ReplacePrefix(&res, ".", ""); // remove the leading . (no package)
114  if (res.find('.') == grpc::string::npos) {
115  return res;
116  } else {
117  std::vector<grpc::string> prefixes_and_type = Split(res, '.');
118  for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) {
119  if (i != 0) {
120  res += "::"; // switch '.' to the ruby module delim
121  }
122  if (i < prefixes_and_type.size() - 1) {
123  res += CapitalizeFirst(prefixes_and_type[i]); // capitalize pkgs
124  } else {
125  res += prefixes_and_type[i];
126  }
127  }
128  return res;
129  }
130 }
131 
132 } // namespace grpc_ruby_generator
133 
134 #endif // GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H