nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 42 | using std::getline; |
| 43 | using std::transform; |
| 44 | |
| 45 | namespace grpc_ruby_generator { |
| 46 | |
| 47 | // Split splits a string using char into elems. |
| 48 | inline 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. |
| 59 | inline 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. |
| 66 | inline 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. |
| 76 | inline 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. |
| 87 | inline 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 | |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame^] | 96 | // CapitalizeFirst capitalizes the first char in a string. |
| 97 | inline string CapitalizeFirst(string s) { |
| 98 | if (s.empty()) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 99 | return s; |
| 100 | } |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 101 | s[0] = ::toupper(s[0]); |
| 102 | return s; |
| 103 | } |
| 104 | |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 105 | // RubyTypeOf updates a proto type to the required ruby equivalent. |
| 106 | inline string RubyTypeOf(const string& a_type, const string& package) { |
| 107 | string res(a_type); |
| 108 | ReplacePrefix(&res, package, ""); // remove the leading package if present |
| 109 | ReplacePrefix(&res, ".", ""); // remove the leading . (no package) |
| 110 | if (res.find('.') == string::npos) { |
| 111 | return res; |
| 112 | } else { |
| 113 | vector<string> prefixes_and_type = Split(res, '.'); |
nnoble | 72309c6 | 2014-12-12 11:42:26 -0800 | [diff] [blame] | 114 | for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) { |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 115 | if (i != 0) { |
| 116 | res += "::"; // switch '.' to the ruby module delim |
| 117 | } |
| 118 | if (i < prefixes_and_type.size() - 1) { |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame^] | 119 | res += CapitalizeFirst(prefixes_and_type[i]); // capitalize pkgs |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 120 | } else { |
| 121 | res += prefixes_and_type[i]; |
| 122 | } |
| 123 | } |
| 124 | return res; |
| 125 | } |
| 126 | } |
| 127 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 128 | } // namespace grpc_ruby_generator |
| 129 | |
| 130 | #endif // NET_GRPC_COMPILER_RUBY_GENERATOR_STRING_INL_H_ |