Julien Boeuf | 8fd915a | 2015-08-19 21:18:14 -0700 | [diff] [blame] | 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 | |
Julien Boeuf | 0382bfa | 2015-08-24 23:55:43 -0700 | [diff] [blame] | 34 | #include <grpc++/support/string_ref.h> |
Julien Boeuf | 8fd915a | 2015-08-19 21:18:14 -0700 | [diff] [blame] | 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include <algorithm> |
yang-g | deb3d22 | 2015-09-01 12:51:28 -0700 | [diff] [blame^] | 39 | #include <iostream> |
Julien Boeuf | 8fd915a | 2015-08-19 21:18:14 -0700 | [diff] [blame] | 40 | |
| 41 | namespace grpc { |
| 42 | |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 43 | const size_t string_ref::npos; |
Julien Boeuf | 8fd915a | 2015-08-19 21:18:14 -0700 | [diff] [blame] | 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 { |
yang-g | 0d9f81f | 2015-08-26 22:04:30 -0700 | [diff] [blame] | 84 | auto it = std::find(cbegin(), cend(), c); |
Julien Boeuf | 8fd915a | 2015-08-19 21:18:14 -0700 | [diff] [blame] | 85 | return it == cend() ? npos : std::distance(cbegin(), it); |
| 86 | } |
| 87 | |
| 88 | bool operator==(string_ref x, string_ref y) { |
| 89 | return x.compare(y) == 0; |
| 90 | } |
| 91 | |
| 92 | bool operator!=(string_ref x, string_ref y) { |
| 93 | return x.compare(y) != 0; |
| 94 | } |
| 95 | |
| 96 | bool operator<(string_ref x, string_ref y) { |
| 97 | return x.compare(y) < 0; |
| 98 | } |
| 99 | |
| 100 | bool operator<=(string_ref x, string_ref y) { |
| 101 | return x.compare(y) <= 0; |
| 102 | } |
| 103 | |
| 104 | bool operator>(string_ref x, string_ref y) { |
| 105 | return x.compare(y) > 0; |
| 106 | } |
| 107 | |
| 108 | bool operator>=(string_ref x, string_ref y) { |
| 109 | return x.compare(y) >= 0; |
| 110 | } |
| 111 | |
Craig Tiller | 47ec9a4 | 2015-08-26 09:47:51 -0700 | [diff] [blame] | 112 | std::ostream& operator<<(std::ostream& out, const string_ref& string) { |
| 113 | return out << grpc::string(string.begin(), string.end()); |
| 114 | } |
| 115 | |
Julien Boeuf | 8fd915a | 2015-08-19 21:18:14 -0700 | [diff] [blame] | 116 | } // namespace grpc |