blob: 44e17a202a767012ac8ec7869e8907b009fdf724 [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.
Craig Tillerb5dcec52015-01-13 11:13:42 -080048inline vector<string>& Split(const string& s, char delim,
nnobleebebb7e2014-12-10 16:31:01 -080049 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.
Craig Tillerb5dcec52015-01-13 11:13:42 -080059inline vector<string> Split(const string& s, char delim) {
nnobleebebb7e2014-12-10 16:31:01 -080060 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
temiola54535552015-01-06 17:50:59 -080096// CapitalizeFirst capitalizes the first char in a string.
97inline string CapitalizeFirst(string s) {
98 if (s.empty()) {
nnobleebebb7e2014-12-10 16:31:01 -080099 return s;
100 }
nnobleebebb7e2014-12-10 16:31:01 -0800101 s[0] = ::toupper(s[0]);
102 return s;
103}
104
temiola33a21682014-12-11 15:13:40 -0800105// RubyTypeOf updates a proto type to the required ruby equivalent.
106inline string RubyTypeOf(const string& a_type, const string& package) {
107 string res(a_type);
108 ReplacePrefix(&res, package, ""); // remove the leading package if present
Craig Tillerb5dcec52015-01-13 11:13:42 -0800109 ReplacePrefix(&res, ".", ""); // remove the leading . (no package)
temiola33a21682014-12-11 15:13:40 -0800110 if (res.find('.') == string::npos) {
111 return res;
112 } else {
113 vector<string> prefixes_and_type = Split(res, '.');
nnoble72309c62014-12-12 11:42:26 -0800114 for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) {
temiola33a21682014-12-11 15:13:40 -0800115 if (i != 0) {
116 res += "::"; // switch '.' to the ruby module delim
117 }
118 if (i < prefixes_and_type.size() - 1) {
temiola54535552015-01-06 17:50:59 -0800119 res += CapitalizeFirst(prefixes_and_type[i]); // capitalize pkgs
temiola33a21682014-12-11 15:13:40 -0800120 } else {
121 res += prefixes_and_type[i];
122 }
123 }
124 return res;
125 }
126}
127
nnobleebebb7e2014-12-10 16:31:01 -0800128} // namespace grpc_ruby_generator
129
130#endif // NET_GRPC_COMPILER_RUBY_GENERATOR_STRING_INL_H_