OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, 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 #include <grpc++/support/string_ref.h> |
| 35 |
| 36 #include <string.h> |
| 37 |
| 38 #include <algorithm> |
| 39 #include <iostream> |
| 40 |
| 41 namespace grpc { |
| 42 |
| 43 const size_t string_ref::npos = size_t(-1); |
| 44 |
| 45 string_ref& string_ref::operator=(const string_ref& rhs) { |
| 46 data_ = rhs.data_; |
| 47 length_ = rhs.length_; |
| 48 return *this; |
| 49 } |
| 50 |
| 51 string_ref::string_ref(const char* s) : data_(s), length_(strlen(s)) {} |
| 52 |
| 53 string_ref string_ref::substr(size_t pos, size_t n) const { |
| 54 if (pos > length_) pos = length_; |
| 55 if (n > (length_ - pos)) n = length_ - pos; |
| 56 return string_ref(data_ + pos, n); |
| 57 } |
| 58 |
| 59 int string_ref::compare(string_ref x) const { |
| 60 size_t min_size = length_ < x.length_ ? length_ : x.length_; |
| 61 int r = memcmp(data_, x.data_, min_size); |
| 62 if (r < 0) return -1; |
| 63 if (r > 0) return 1; |
| 64 if (length_ < x.length_) return -1; |
| 65 if (length_ > x.length_) return 1; |
| 66 return 0; |
| 67 } |
| 68 |
| 69 bool string_ref::starts_with(string_ref x) const { |
| 70 return length_ >= x.length_ && (memcmp(data_, x.data_, x.length_) == 0); |
| 71 } |
| 72 |
| 73 bool string_ref::ends_with(string_ref x) const { |
| 74 return length_ >= x.length_ && |
| 75 (memcmp(data_ + (length_ - x.length_), x.data_, x.length_) == 0); |
| 76 } |
| 77 |
| 78 size_t string_ref::find(string_ref s) const { |
| 79 auto it = std::search(cbegin(), cend(), s.cbegin(), s.cend()); |
| 80 return it == cend() ? npos : std::distance(cbegin(), it); |
| 81 } |
| 82 |
| 83 size_t string_ref::find(char c) const { |
| 84 auto it = std::find(cbegin(), cend(), c); |
| 85 return it == cend() ? npos : std::distance(cbegin(), it); |
| 86 } |
| 87 |
| 88 bool operator==(string_ref x, string_ref y) { return x.compare(y) == 0; } |
| 89 |
| 90 bool operator!=(string_ref x, string_ref y) { return x.compare(y) != 0; } |
| 91 |
| 92 bool operator<(string_ref x, string_ref y) { return x.compare(y) < 0; } |
| 93 |
| 94 bool operator<=(string_ref x, string_ref y) { return x.compare(y) <= 0; } |
| 95 |
| 96 bool operator>(string_ref x, string_ref y) { return x.compare(y) > 0; } |
| 97 |
| 98 bool operator>=(string_ref x, string_ref y) { return x.compare(y) >= 0; } |
| 99 |
| 100 std::ostream& operator<<(std::ostream& out, const string_ref& string) { |
| 101 return out << grpc::string(string.begin(), string.end()); |
| 102 } |
| 103 |
| 104 } // namespace grpc |
OLD | NEW |