blob: bdd314c16e5c809e8f43f8c22a6ca7948605f4b0 [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
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 Tillerecd49342015-01-18 14:36:47 -080048inline std::vector<std::string> &Split(const std::string &s, char delim,
49 std::vector<std::string> *elems) {
Nicolas Noblef5c5d802015-01-15 16:36:13 -080050 std::stringstream ss(s);
51 std::string item;
nnobleebebb7e2014-12-10 16:31:01 -080052 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 Tillerecd49342015-01-18 14:36:47 -080059inline std::vector<std::string> Split(const std::string &s, char delim) {
Nicolas Noblef5c5d802015-01-15 16:36:13 -080060 std::vector<std::string> elems;
nnobleebebb7e2014-12-10 16:31:01 -080061 Split(s, delim, &elems);
62 return elems;
63}
64
65// Replace replaces from with to in s.
Craig Tillerecd49342015-01-18 14:36:47 -080066inline std::string Replace(std::string s, const std::string &from,
67 const std::string &to) {
nnobleebebb7e2014-12-10 16:31:01 -080068 size_t start_pos = s.find(from);
Nicolas Noblef5c5d802015-01-15 16:36:13 -080069 if (start_pos == std::string::npos) {
nnobleebebb7e2014-12-10 16:31:01 -080070 return s;
71 }
72 s.replace(start_pos, from.length(), to);
73 return s;
74}
75
76// ReplaceAll replaces all instances of search with replace in s.
Craig Tillerecd49342015-01-18 14:36:47 -080077inline std::string ReplaceAll(std::string s, const std::string &search,
78 const std::string &replace) {
nnobleebebb7e2014-12-10 16:31:01 -080079 size_t pos = 0;
Nicolas Noblef5c5d802015-01-15 16:36:13 -080080 while ((pos = s.find(search, pos)) != std::string::npos) {
nnobleebebb7e2014-12-10 16:31:01 -080081 s.replace(pos, search.length(), replace);
82 pos += replace.length();
83 }
84 return s;
85}
86
87// ReplacePrefix replaces from with to in s if search is a prefix of s.
Craig Tillerecd49342015-01-18 14:36:47 -080088inline bool ReplacePrefix(std::string *s, const std::string &from,
89 const std::string &to) {
nnobleebebb7e2014-12-10 16:31:01 -080090 size_t start_pos = s->find(from);
Nicolas Noblef5c5d802015-01-15 16:36:13 -080091 if (start_pos == std::string::npos || start_pos != 0) {
nnobleebebb7e2014-12-10 16:31:01 -080092 return false;
93 }
94 s->replace(start_pos, from.length(), to);
95 return true;
96}
97
temiola54535552015-01-06 17:50:59 -080098// CapitalizeFirst capitalizes the first char in a string.
Nicolas Noblef5c5d802015-01-15 16:36:13 -080099inline std::string CapitalizeFirst(std::string s) {
temiola54535552015-01-06 17:50:59 -0800100 if (s.empty()) {
nnobleebebb7e2014-12-10 16:31:01 -0800101 return s;
102 }
nnobleebebb7e2014-12-10 16:31:01 -0800103 s[0] = ::toupper(s[0]);
104 return s;
105}
106
temiola33a21682014-12-11 15:13:40 -0800107// RubyTypeOf updates a proto type to the required ruby equivalent.
Craig Tillerecd49342015-01-18 14:36:47 -0800108inline std::string RubyTypeOf(const std::string &a_type,
109 const std::string &package) {
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800110 std::string res(a_type);
temiola33a21682014-12-11 15:13:40 -0800111 ReplacePrefix(&res, package, ""); // remove the leading package if present
Craig Tillerb5dcec52015-01-13 11:13:42 -0800112 ReplacePrefix(&res, ".", ""); // remove the leading . (no package)
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800113 if (res.find('.') == std::string::npos) {
temiola33a21682014-12-11 15:13:40 -0800114 return res;
115 } else {
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800116 std::vector<std::string> prefixes_and_type = Split(res, '.');
nnoble72309c62014-12-12 11:42:26 -0800117 for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) {
temiola33a21682014-12-11 15:13:40 -0800118 if (i != 0) {
119 res += "::"; // switch '.' to the ruby module delim
120 }
121 if (i < prefixes_and_type.size() - 1) {
temiola54535552015-01-06 17:50:59 -0800122 res += CapitalizeFirst(prefixes_and_type[i]); // capitalize pkgs
temiola33a21682014-12-11 15:13:40 -0800123 } else {
124 res += prefixes_and_type[i];
125 }
126 }
127 return res;
128 }
129}
130
nnobleebebb7e2014-12-10 16:31:01 -0800131} // namespace grpc_ruby_generator
132
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100133#endif // GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H