nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 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 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H |
| 35 | #define GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 36 | |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 37 | #include "src/compiler/config.h" |
| 38 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 39 | #include <algorithm> |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 40 | #include <sstream> |
| 41 | #include <vector> |
| 42 | |
| 43 | using std::getline; |
| 44 | using std::transform; |
| 45 | |
| 46 | namespace grpc_ruby_generator { |
| 47 | |
| 48 | // Split splits a string using char into elems. |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 49 | inline std::vector<grpc::string> &Split(const grpc::string &s, char delim, |
| 50 | std::vector<grpc::string> *elems) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 51 | std::stringstream ss(s); |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 52 | grpc::string item; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 53 | 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 Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 60 | inline std::vector<grpc::string> Split(const grpc::string &s, char delim) { |
| 61 | std::vector<grpc::string> elems; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 62 | Split(s, delim, &elems); |
| 63 | return elems; |
| 64 | } |
| 65 | |
| 66 | // Replace replaces from with to in s. |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 67 | inline grpc::string Replace(grpc::string s, const grpc::string &from, |
| 68 | const grpc::string &to) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 69 | size_t start_pos = s.find(from); |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 70 | if (start_pos == grpc::string::npos) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 71 | 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 Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 78 | inline grpc::string ReplaceAll(grpc::string s, const grpc::string &search, |
| 79 | const grpc::string &replace) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 80 | size_t pos = 0; |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 81 | while ((pos = s.find(search, pos)) != grpc::string::npos) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 82 | 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 Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 89 | inline bool ReplacePrefix(grpc::string *s, const grpc::string &from, |
| 90 | const grpc::string &to) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 91 | size_t start_pos = s->find(from); |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 92 | if (start_pos == grpc::string::npos || start_pos != 0) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | s->replace(start_pos, from.length(), to); |
| 96 | return true; |
| 97 | } |
| 98 | |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame] | 99 | // CapitalizeFirst capitalizes the first char in a string. |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 100 | inline grpc::string CapitalizeFirst(grpc::string s) { |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame] | 101 | if (s.empty()) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 102 | return s; |
| 103 | } |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 104 | s[0] = ::toupper(s[0]); |
| 105 | return s; |
| 106 | } |
| 107 | |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 108 | // RubyTypeOf updates a proto type to the required ruby equivalent. |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 109 | inline grpc::string RubyTypeOf(const grpc::string &a_type, |
| 110 | const grpc::string &package) { |
| 111 | grpc::string res(a_type); |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 112 | ReplacePrefix(&res, package, ""); // remove the leading package if present |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 113 | ReplacePrefix(&res, ".", ""); // remove the leading . (no package) |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 114 | if (res.find('.') == grpc::string::npos) { |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 115 | return res; |
| 116 | } else { |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 117 | std::vector<grpc::string> prefixes_and_type = Split(res, '.'); |
nnoble | 72309c6 | 2014-12-12 11:42:26 -0800 | [diff] [blame] | 118 | for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) { |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 119 | if (i != 0) { |
| 120 | res += "::"; // switch '.' to the ruby module delim |
| 121 | } |
| 122 | if (i < prefixes_and_type.size() - 1) { |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame] | 123 | res += CapitalizeFirst(prefixes_and_type[i]); // capitalize pkgs |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 124 | } else { |
| 125 | res += prefixes_and_type[i]; |
| 126 | } |
| 127 | } |
| 128 | return res; |
| 129 | } |
| 130 | } |
| 131 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 132 | } // namespace grpc_ruby_generator |
| 133 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 134 | #endif // GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H |