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 | |
| 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. |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 48 | inline std::vector<std::string> &Split(const std::string &s, char delim, |
| 49 | std::vector<std::string> *elems) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 50 | std::stringstream ss(s); |
| 51 | std::string item; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 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 Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 59 | inline std::vector<std::string> Split(const std::string &s, char delim) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 60 | std::vector<std::string> elems; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 61 | Split(s, delim, &elems); |
| 62 | return elems; |
| 63 | } |
| 64 | |
| 65 | // Replace replaces from with to in s. |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 66 | inline std::string Replace(std::string s, const std::string &from, |
| 67 | const std::string &to) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 68 | size_t start_pos = s.find(from); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 69 | if (start_pos == std::string::npos) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 70 | 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 Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 77 | inline std::string ReplaceAll(std::string s, const std::string &search, |
| 78 | const std::string &replace) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 79 | size_t pos = 0; |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 80 | while ((pos = s.find(search, pos)) != std::string::npos) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 81 | 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 Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 88 | inline bool ReplacePrefix(std::string *s, const std::string &from, |
| 89 | const std::string &to) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 90 | size_t start_pos = s->find(from); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 91 | if (start_pos == std::string::npos || start_pos != 0) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | s->replace(start_pos, from.length(), to); |
| 95 | return true; |
| 96 | } |
| 97 | |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame] | 98 | // CapitalizeFirst capitalizes the first char in a string. |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 99 | inline std::string CapitalizeFirst(std::string s) { |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame] | 100 | if (s.empty()) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 101 | return s; |
| 102 | } |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 103 | s[0] = ::toupper(s[0]); |
| 104 | return s; |
| 105 | } |
| 106 | |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 107 | // RubyTypeOf updates a proto type to the required ruby equivalent. |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 108 | inline std::string RubyTypeOf(const std::string &a_type, |
| 109 | const std::string &package) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 110 | std::string res(a_type); |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 111 | ReplacePrefix(&res, package, ""); // remove the leading package if present |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 112 | ReplacePrefix(&res, ".", ""); // remove the leading . (no package) |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 113 | if (res.find('.') == std::string::npos) { |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 114 | return res; |
| 115 | } else { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 116 | std::vector<std::string> prefixes_and_type = Split(res, '.'); |
nnoble | 72309c6 | 2014-12-12 11:42:26 -0800 | [diff] [blame] | 117 | for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) { |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 118 | if (i != 0) { |
| 119 | res += "::"; // switch '.' to the ruby module delim |
| 120 | } |
| 121 | if (i < prefixes_and_type.size() - 1) { |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame] | 122 | res += CapitalizeFirst(prefixes_and_type[i]); // capitalize pkgs |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 123 | } else { |
| 124 | res += prefixes_and_type[i]; |
| 125 | } |
| 126 | } |
| 127 | return res; |
| 128 | } |
| 129 | } |
| 130 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 131 | } // namespace grpc_ruby_generator |
| 132 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame^] | 133 | #endif // GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_STRING_INL_H |