blob: 8da3a88da2937d1cb6d33956be378d927715f0bd [file] [log] [blame]
nnobleebebb7e2014-12-10 16:31:01 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
nnobleebebb7e2014-12-10 16:31:01 -08004 * 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
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H
35#define GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H
nnobleebebb7e2014-12-10 16:31:01 -080036
Yang Gao478568e2015-03-23 22:09:22 -070037#include "src/compiler/config.h"
38
nnobleebebb7e2014-12-10 16:31:01 -080039#include <algorithm>
nnobleebebb7e2014-12-10 16:31:01 -080040#include <sstream>
41#include <vector>
42
43using std::getline;
44using std::transform;
45
46namespace grpc_ruby_generator {
47
48// Split splits a string using char into elems.
Yang Gao478568e2015-03-23 22:09:22 -070049inline std::vector<grpc::string> &Split(const grpc::string &s, char delim,
50 std::vector<grpc::string> *elems) {
Nicolas Noblef5c5d802015-01-15 16:36:13 -080051 std::stringstream ss(s);
Yang Gao478568e2015-03-23 22:09:22 -070052 grpc::string item;
nnobleebebb7e2014-12-10 16:31:01 -080053 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.
Yang Gao478568e2015-03-23 22:09:22 -070060inline std::vector<grpc::string> Split(const grpc::string &s, char delim) {
61 std::vector<grpc::string> elems;
nnobleebebb7e2014-12-10 16:31:01 -080062 Split(s, delim, &elems);
63 return elems;
64}
65
66// Replace replaces from with to in s.
Yang Gao478568e2015-03-23 22:09:22 -070067inline grpc::string Replace(grpc::string s, const grpc::string &from,
68 const grpc::string &to) {
nnobleebebb7e2014-12-10 16:31:01 -080069 size_t start_pos = s.find(from);
Yang Gao478568e2015-03-23 22:09:22 -070070 if (start_pos == grpc::string::npos) {
nnobleebebb7e2014-12-10 16:31:01 -080071 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.
Yang Gao478568e2015-03-23 22:09:22 -070078inline grpc::string ReplaceAll(grpc::string s, const grpc::string &search,
79 const grpc::string &replace) {
nnobleebebb7e2014-12-10 16:31:01 -080080 size_t pos = 0;
Yang Gao478568e2015-03-23 22:09:22 -070081 while ((pos = s.find(search, pos)) != grpc::string::npos) {
nnobleebebb7e2014-12-10 16:31:01 -080082 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.
Yang Gao478568e2015-03-23 22:09:22 -070089inline bool ReplacePrefix(grpc::string *s, const grpc::string &from,
90 const grpc::string &to) {
nnobleebebb7e2014-12-10 16:31:01 -080091 size_t start_pos = s->find(from);
Yang Gao478568e2015-03-23 22:09:22 -070092 if (start_pos == grpc::string::npos || start_pos != 0) {
nnobleebebb7e2014-12-10 16:31:01 -080093 return false;
94 }
95 s->replace(start_pos, from.length(), to);
96 return true;
97}
98
temiola54535552015-01-06 17:50:59 -080099// CapitalizeFirst capitalizes the first char in a string.
Yang Gao478568e2015-03-23 22:09:22 -0700100inline grpc::string CapitalizeFirst(grpc::string s) {
temiola54535552015-01-06 17:50:59 -0800101 if (s.empty()) {
nnobleebebb7e2014-12-10 16:31:01 -0800102 return s;
103 }
nnobleebebb7e2014-12-10 16:31:01 -0800104 s[0] = ::toupper(s[0]);
105 return s;
106}
107
temiola33a21682014-12-11 15:13:40 -0800108// RubyTypeOf updates a proto type to the required ruby equivalent.
Yang Gao478568e2015-03-23 22:09:22 -0700109inline grpc::string RubyTypeOf(const grpc::string &a_type,
110 const grpc::string &package) {
111 grpc::string res(a_type);
temiola33a21682014-12-11 15:13:40 -0800112 ReplacePrefix(&res, package, ""); // remove the leading package if present
Craig Tillerb5dcec52015-01-13 11:13:42 -0800113 ReplacePrefix(&res, ".", ""); // remove the leading . (no package)
Yang Gao478568e2015-03-23 22:09:22 -0700114 if (res.find('.') == grpc::string::npos) {
temiola33a21682014-12-11 15:13:40 -0800115 return res;
116 } else {
Yang Gao478568e2015-03-23 22:09:22 -0700117 std::vector<grpc::string> prefixes_and_type = Split(res, '.');
nnoble72309c62014-12-12 11:42:26 -0800118 for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) {
temiola33a21682014-12-11 15:13:40 -0800119 if (i != 0) {
120 res += "::"; // switch '.' to the ruby module delim
121 }
122 if (i < prefixes_and_type.size() - 1) {
temiola54535552015-01-06 17:50:59 -0800123 res += CapitalizeFirst(prefixes_and_type[i]); // capitalize pkgs
temiola33a21682014-12-11 15:13:40 -0800124 } else {
125 res += prefixes_and_type[i];
126 }
127 }
128 return res;
129 }
130}
131
nnobleebebb7e2014-12-10 16:31:01 -0800132} // namespace grpc_ruby_generator
133
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100134#endif // GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H