blob: 66c79a1818431daa0b6719778c259dcd678fca80 [file] [log] [blame]
Julien Boeuf8fd915a2015-08-19 21:18:14 -07001/*
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 Boeuf0382bfa2015-08-24 23:55:43 -070034#include <grpc++/support/string_ref.h>
Julien Boeuf8fd915a2015-08-19 21:18:14 -070035
36#include <string.h>
37
38#include <algorithm>
yang-gdeb3d222015-09-01 12:51:28 -070039#include <iostream>
Julien Boeuf8fd915a2015-08-19 21:18:14 -070040
41namespace grpc {
42
Konstantin Podsvirovdc90c132015-09-01 16:18:57 +030043const size_t string_ref::npos = size_t(-1);
Julien Boeuf8fd915a2015-08-19 21:18:14 -070044
45string_ref& string_ref::operator=(const string_ref& rhs) {
46 data_ = rhs.data_;
47 length_ = rhs.length_;
48 return *this;
49}
50
51string_ref::string_ref(const char* s) : data_(s), length_(strlen(s)) {}
52
53string_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
59int 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
69bool string_ref::starts_with(string_ref x) const {
70 return length_ >= x.length_ && (memcmp(data_, x.data_, x.length_) == 0);
71}
72
73bool 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
78size_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
83size_t string_ref::find(char c) const {
yang-g0d9f81f2015-08-26 22:04:30 -070084 auto it = std::find(cbegin(), cend(), c);
Julien Boeuf8fd915a2015-08-19 21:18:14 -070085 return it == cend() ? npos : std::distance(cbegin(), it);
86}
87
Craig Tiller71a0f9d2015-09-28 17:22:01 -070088bool operator==(string_ref x, string_ref y) { return x.compare(y) == 0; }
Julien Boeuf8fd915a2015-08-19 21:18:14 -070089
Craig Tiller71a0f9d2015-09-28 17:22:01 -070090bool operator!=(string_ref x, string_ref y) { return x.compare(y) != 0; }
Julien Boeuf8fd915a2015-08-19 21:18:14 -070091
Craig Tiller71a0f9d2015-09-28 17:22:01 -070092bool operator<(string_ref x, string_ref y) { return x.compare(y) < 0; }
Julien Boeuf8fd915a2015-08-19 21:18:14 -070093
Craig Tiller71a0f9d2015-09-28 17:22:01 -070094bool operator<=(string_ref x, string_ref y) { return x.compare(y) <= 0; }
Julien Boeuf8fd915a2015-08-19 21:18:14 -070095
Craig Tiller71a0f9d2015-09-28 17:22:01 -070096bool operator>(string_ref x, string_ref y) { return x.compare(y) > 0; }
Julien Boeuf8fd915a2015-08-19 21:18:14 -070097
Craig Tiller71a0f9d2015-09-28 17:22:01 -070098bool operator>=(string_ref x, string_ref y) { return x.compare(y) >= 0; }
Julien Boeuf8fd915a2015-08-19 21:18:14 -070099
Craig Tiller47ec9a42015-08-26 09:47:51 -0700100std::ostream& operator<<(std::ostream& out, const string_ref& string) {
101 return out << grpc::string(string.begin(), string.end());
102}
103
Julien Boeuf8fd915a2015-08-19 21:18:14 -0700104} // namespace grpc