ruby_generator_string-inl.h
Go to the documentation of this file.
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 // Split splits a string using char into elems.
34 inline std::vector<std::string>& Split(const std::string& s, char delim,
35  std::vector<std::string>* elems) {
36  std::stringstream ss(s);
37  std::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.
45 inline std::vector<std::string> Split(const std::string& s, char delim) {
46  std::vector<std::string> elems;
47  Split(s, delim, &elems);
48  return elems;
49 }
50 
51 // Replace replaces from with to in s.
53  const std::string& to) {
54  size_t start_pos = s.find(from);
55  if (start_pos == std::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.
64  const std::string& replace) {
65  size_t pos = 0;
66  while ((pos = s.find(search, pos)) != std::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.
74 inline bool ReplacePrefix(std::string* s, const std::string& from,
75  const std::string& to) {
76  size_t start_pos = s->find(from);
77  if (start_pos == std::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
86  if (s.empty()) {
87  return s;
88  }
89  std::string new_string = "";
90  bool was_last_underscore = false;
91  new_string.append(1, ::toupper(s[0]));
92  for (std::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 // RubyPackage gets the ruby package in either proto or ruby_package format
105  std::string package_name = file->package();
106  if (file->options().has_ruby_package()) {
107  package_name = file->options().ruby_package();
108 
109  // If :: is in the package convert the Ruby formatted name
110  // -> A::B::C
111  // to use the dot seperator notation
112  // -> A.B.C
113  package_name = ReplaceAll(package_name, "::", ".");
114  }
115  return package_name;
116 }
117 
118 // RubyTypeOf updates a proto type to the required ruby equivalent.
120  std::string proto_type = descriptor->full_name();
121  if (descriptor->file()->options().has_ruby_package()) {
122  // remove the leading package if present
123  ReplacePrefix(&proto_type, descriptor->file()->package(), "");
124  ReplacePrefix(&proto_type, ".", ""); // remove the leading . (no package)
125  proto_type = RubyPackage(descriptor->file()) + "." + proto_type;
126  }
127  std::string res("." + proto_type);
128  if (res.find('.') == std::string::npos) {
129  return res;
130  } else {
131  std::vector<std::string> prefixes_and_type = Split(res, '.');
132  res.clear();
133  for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) {
134  if (i != 0) {
135  res += "::"; // switch '.' to the ruby module delim
136  }
137  if (i < prefixes_and_type.size() - 1) {
138  res += Modularize(prefixes_and_type[i]); // capitalize pkgs
139  } else {
140  res += prefixes_and_type[i];
141  }
142  }
143  return res;
144  }
145 }
146 
147 } // namespace grpc_ruby_generator
148 
149 #endif // GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H
grpc_ruby_generator::Modularize
std::string Modularize(std::string s)
Definition: ruby_generator_string-inl.h:85
grpc_ruby_generator::RubyPackage
std::string RubyPackage(const grpc::protobuf::FileDescriptor *file)
Definition: ruby_generator_string-inl.h:104
grpc_ruby_generator::ReplacePrefix
bool ReplacePrefix(std::string *s, const std::string &from, const std::string &to)
Definition: ruby_generator_string-inl.h:74
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
grpc::protobuf::FileDescriptor
GRPC_CUSTOM_FILEDESCRIPTOR FileDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:85
config.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
to
size_t to
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1385
from
size_t from
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1384
search
Definition: search.py:1
grpc_ruby_generator::Replace
std::string Replace(std::string s, const std::string &from, const std::string &to)
Definition: ruby_generator_string-inl.h:52
grpc_ruby_generator
Definition: src/compiler/ruby_generator.cc:38
grpc_ruby_generator::ReplaceAll
std::string ReplaceAll(std::string s, const std::string &search, const std::string &replace)
Definition: ruby_generator_string-inl.h:63
grpc::protobuf::Descriptor
GRPC_CUSTOM_DESCRIPTOR Descriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:81
grpc_ruby_generator::Split
std::vector< std::string > & Split(const std::string &s, char delim, std::vector< std::string > *elems)
Definition: ruby_generator_string-inl.h:34
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_ruby_generator::RubyTypeOf
std::string RubyTypeOf(const grpc::protobuf::Descriptor *descriptor)
Definition: ruby_generator_string-inl.h:119


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:13