blob: f74f2c7355a53a40993eac645de9671e8f050ab2 [file] [log] [blame]
nnobleebebb7e2014-12-10 16:31:01 -08001/*
2 *
3 * Copyright 2014, 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 NET_GRPC_COMPILER_RUBY_GENERATOR_STRING_INL_H_
35#define NET_GRPC_COMPILER_RUBY_GENERATOR_STRING_INL_H_
36
37#include <algorithm>
38#include <string>
39#include <sstream>
40#include <vector>
41
42using std::getline;
43using std::transform;
44
45namespace grpc_ruby_generator {
46
47// Split splits a string using char into elems.
48inline vector<string> &Split(const string &s, char delim,
49 vector<string>* elems) {
50 stringstream ss(s);
51 string item;
52 while (getline(ss, item, delim)) {
53 elems->push_back(item);
54 }
55 return *elems;
56}
57
58// Split splits a string using char, returning the result in a vector.
59inline vector<string> Split(const string &s, char delim) {
60 vector<string> elems;
61 Split(s, delim, &elems);
62 return elems;
63}
64
65// Replace replaces from with to in s.
66inline string Replace(string s, const string& from, const string& to) {
67 size_t start_pos = s.find(from);
68 if (start_pos == string::npos) {
69 return s;
70 }
71 s.replace(start_pos, from.length(), to);
72 return s;
73}
74
75// ReplaceAll replaces all instances of search with replace in s.
76inline string ReplaceAll(string s, const string& search,
77 const string& replace) {
78 size_t pos = 0;
79 while ((pos = s.find(search, pos)) != string::npos) {
80 s.replace(pos, search.length(), replace);
81 pos += replace.length();
82 }
83 return s;
84}
85
86// ReplacePrefix replaces from with to in s if search is a prefix of s.
87inline bool ReplacePrefix(string* s, const string& from, const string& to) {
88 size_t start_pos = s->find(from);
89 if (start_pos == string::npos || start_pos != 0) {
90 return false;
91 }
92 s->replace(start_pos, from.length(), to);
93 return true;
94}
95
nnobleebebb7e2014-12-10 16:31:01 -080096// CapitalizeString capitalizes a string.
97inline string CapitalizeString(string s) {
98 if (!s.empty()) {
99 return s;
100 }
101 transform(s.begin(), s.end(), s.begin(), ::tolower);
102 s[0] = ::toupper(s[0]);
103 return s;
104}
105
temiola33a21682014-12-11 15:13:40 -0800106// RubyTypeOf updates a proto type to the required ruby equivalent.
107inline string RubyTypeOf(const string& a_type, const string& package) {
108 string res(a_type);
109 ReplacePrefix(&res, package, ""); // remove the leading package if present
110 ReplacePrefix(&res, ".", ""); // remove the leading . (no package)
111 if (res.find('.') == string::npos) {
112 return res;
113 } else {
114 vector<string> prefixes_and_type = Split(res, '.');
nnoble72309c62014-12-12 11:42:26 -0800115 for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) {
temiola33a21682014-12-11 15:13:40 -0800116 if (i != 0) {
117 res += "::"; // switch '.' to the ruby module delim
118 }
119 if (i < prefixes_and_type.size() - 1) {
120 res += CapitalizeString(prefixes_and_type[i]); // capitalize pkgs
121 } else {
122 res += prefixes_and_type[i];
123 }
124 }
125 return res;
126 }
127}
128
nnobleebebb7e2014-12-10 16:31:01 -0800129} // namespace grpc_ruby_generator
130
131#endif // NET_GRPC_COMPILER_RUBY_GENERATOR_STRING_INL_H_