GRPC Core  9.0.0
string_view.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2019 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 #ifndef GRPC_CORE_LIB_GPRPP_STRING_VIEW_H
19 #define GRPC_CORE_LIB_GPRPP_STRING_VIEW_H
20 
22 
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 
27 #include <algorithm>
28 #include <cstdint>
29 #include <cstring>
30 #include <limits>
31 #include <string>
32 
36 
37 #if GRPC_USE_ABSL
38 #include "absl/strings/string_view.h"
39 #endif
40 
41 namespace grpc_core {
42 
43 #if GRPC_USE_ABSL
44 
45 using StringView = absl::string_view;
46 
47 #else
48 
49 // Provides a light-weight view over a char array or a slice, similar but not
50 // identical to absl::string_view.
51 //
52 // Any method that has the same name as absl::string_view MUST HAVE identical
53 // semantics to what absl::string_view provides.
54 //
55 // Methods that are not part of absl::string_view API, must be clearly
56 // annotated.
57 //
58 // StringView does not own the buffers that back the view. Callers must ensure
59 // the buffer stays around while the StringView is accessible.
60 //
61 // Pass StringView by value in functions, since it is exactly two pointers in
62 // size.
63 //
64 // The interface used here is not identical to absl::string_view. Notably, we
65 // need to support slices while we cannot support std::string, and gpr string
66 // style functions such as strdup() and cmp(). Once we switch to
67 // absl::string_view this class will inherit from absl::string_view and add the
68 // gRPC-specific APIs.
69 class StringView final {
70  public:
71  static constexpr size_t npos = std::numeric_limits<size_t>::max();
72 
73  constexpr StringView(const char* ptr, size_t size) : ptr_(ptr), size_(size) {}
74  constexpr StringView(const char* ptr)
75  : StringView(ptr, ptr == nullptr ? 0 : strlen(ptr)) {}
76  constexpr StringView() : StringView(nullptr, 0) {}
77 
78  constexpr const char* data() const { return ptr_; }
79  constexpr size_t size() const { return size_; }
80  constexpr bool empty() const { return size_ == 0; }
81 
82  StringView substr(size_t start, size_t size = npos) {
83  GPR_DEBUG_ASSERT(start + size <= size_);
84  return StringView(ptr_ + start, std::min(size, size_ - start));
85  }
86 
87  constexpr const char& operator[](size_t i) const { return ptr_[i]; }
88 
89  const char& front() const { return ptr_[0]; }
90  const char& back() const { return ptr_[size_ - 1]; }
91 
92  void remove_prefix(size_t n) {
93  GPR_DEBUG_ASSERT(n <= size_);
94  ptr_ += n;
95  size_ -= n;
96  }
97 
98  void remove_suffix(size_t n) {
99  GPR_DEBUG_ASSERT(n <= size_);
100  size_ -= n;
101  }
102 
103  size_t find(char c, size_t pos = 0) const {
104  if (empty() || pos >= size_) return npos;
105  const char* result =
106  static_cast<const char*>(memchr(ptr_ + pos, c, size_ - pos));
107  return result != nullptr ? result - ptr_ : npos;
108  }
109 
110  void clear() {
111  ptr_ = nullptr;
112  size_ = 0;
113  }
114 
115  // Converts to `std::basic_string`.
116  template <typename Allocator>
117  explicit operator std::basic_string<char, std::char_traits<char>, Allocator>()
118  const {
119  if (data() == nullptr) return {};
120  return std::basic_string<char, std::char_traits<char>, Allocator>(data(),
121  size());
122  }
123 
124  private:
125  const char* ptr_;
126  size_t size_;
127 };
128 
129 inline bool operator==(StringView lhs, StringView rhs) {
130  return lhs.size() == rhs.size() &&
131  strncmp(lhs.data(), rhs.data(), lhs.size()) == 0;
132 }
133 
134 inline bool operator!=(StringView lhs, StringView rhs) { return !(lhs == rhs); }
135 
136 #endif // GRPC_USE_ABSL
137 
138 // Converts grpc_slice to StringView.
140  return StringView(reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(slice)),
141  GRPC_SLICE_LENGTH(slice));
142 }
143 
144 // Creates a dup of the string viewed by this class.
145 // Return value is null-terminated and never nullptr.
147  char* str = static_cast<char*>(gpr_malloc(sv.size() + 1));
148  if (sv.size() > 0) memcpy(str, sv.data(), sv.size());
149  str[sv.size()] = '\0';
150  return grpc_core::UniquePtr<char>(str);
151 }
152 
153 // Compares lhs and rhs.
154 inline int StringViewCmp(const StringView lhs, const StringView rhs) {
155  const size_t len = GPR_MIN(lhs.size(), rhs.size());
156  const int ret = strncmp(lhs.data(), rhs.data(), len);
157  if (ret != 0) return ret;
158  if (lhs.size() == rhs.size()) return 0;
159  if (lhs.size() < rhs.size()) return -1;
160  return 1;
161 }
162 
163 } // namespace grpc_core
164 
165 #endif /* GRPC_CORE_LIB_GPRPP_STRING_VIEW_H */
Definition: string_view.h:69
constexpr StringView(const char *ptr, size_t size)
Definition: string_view.h:73
const char & back() const
Definition: string_view.h:90
constexpr const char & operator[](size_t i) const
Definition: string_view.h:87
const char & front() const
Definition: string_view.h:89
void remove_suffix(size_t n)
Definition: string_view.h:98
constexpr size_t size() const
Definition: string_view.h:79
constexpr StringView()
Definition: string_view.h:76
size_t find(char c, size_t pos=0) const
Definition: string_view.h:103
constexpr StringView(const char *ptr)
Definition: string_view.h:74
static constexpr size_t npos
Definition: string_view.h:71
void remove_prefix(size_t n)
Definition: string_view.h:92
StringView substr(size_t start, size_t size=npos)
Definition: string_view.h:82
constexpr const char * data() const
Definition: string_view.h:78
void clear()
Definition: string_view.h:110
constexpr bool empty() const
Definition: string_view.h:80
#define GPR_DEBUG_ASSERT(x)
Definition: log.h:103
#define GRPC_SLICE_START_PTR(slice)
Definition: slice.h:96
#define GRPC_SLICE_LENGTH(slice)
Definition: slice.h:99
GPRAPI void * gpr_malloc(size_t size)
malloc.
Definition: alloc.cc:28
Round Robin Policy.
Definition: backend_metric.cc:24
bool operator!=(StringView lhs, StringView rhs)
Definition: string_view.h:134
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: memory.h:45
StringView StringViewFromSlice(const grpc_slice &slice)
Definition: string_view.h:139
bool operator==(StringView lhs, StringView rhs)
Definition: string_view.h:129
grpc_core::UniquePtr< char > StringViewToCString(const StringView sv)
Definition: string_view.h:146
int StringViewCmp(const StringView lhs, const StringView rhs)
Definition: string_view.h:154
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice.h:60
#define GPR_MIN(a, b)
useful macros that don't belong anywhere else
Definition: useful.h:24