You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

string_ref.h 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. *
  3. * Copyright 2015 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 GRPCPP_IMPL_CODEGEN_STRING_REF_H
  19. #define GRPCPP_IMPL_CODEGEN_STRING_REF_H
  20. // IWYU pragma: private, include <grpcpp/support/string_ref.h>
  21. #include <string.h>
  22. #include <algorithm>
  23. #include <iosfwd>
  24. #include <iostream>
  25. #include <iterator>
  26. #include <grpcpp/impl/codegen/config.h>
  27. namespace grpc
  28. {
  29. /// This class is a non owning reference to a string.
  30. ///
  31. /// It should be a strict subset of the upcoming std::string_ref.
  32. ///
  33. /// \see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html
  34. ///
  35. /// The constexpr is dropped or replaced with const for legacy compiler
  36. /// compatibility.
  37. class string_ref
  38. {
  39. public:
  40. /// types
  41. typedef const char* const_iterator;
  42. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  43. /// constants
  44. const static size_t npos;
  45. /// construct/copy.
  46. string_ref() :
  47. data_(nullptr),
  48. length_(0)
  49. {
  50. }
  51. string_ref(const string_ref& other) :
  52. data_(other.data_),
  53. length_(other.length_)
  54. {
  55. }
  56. // NOLINTNEXTLINE(bugprone-unhandled-self-assignment)
  57. string_ref& operator=(const string_ref& rhs)
  58. {
  59. data_ = rhs.data_;
  60. length_ = rhs.length_;
  61. return *this;
  62. }
  63. /* NOLINTNEXTLINE(google-explicit-constructor) */
  64. string_ref(const char* s) :
  65. data_(s),
  66. length_(strlen(s))
  67. {
  68. }
  69. string_ref(const char* s, size_t l) :
  70. data_(s),
  71. length_(l)
  72. {
  73. }
  74. /* NOLINTNEXTLINE(google-explicit-constructor) */
  75. string_ref(const std::string& s) :
  76. data_(s.data()),
  77. length_(s.length())
  78. {
  79. }
  80. /// iterators
  81. const_iterator begin() const
  82. {
  83. return data_;
  84. }
  85. const_iterator end() const
  86. {
  87. return data_ + length_;
  88. }
  89. const_iterator cbegin() const
  90. {
  91. return data_;
  92. }
  93. const_iterator cend() const
  94. {
  95. return data_ + length_;
  96. }
  97. const_reverse_iterator rbegin() const
  98. {
  99. return const_reverse_iterator(end());
  100. }
  101. const_reverse_iterator rend() const
  102. {
  103. return const_reverse_iterator(begin());
  104. }
  105. const_reverse_iterator crbegin() const
  106. {
  107. return const_reverse_iterator(end());
  108. }
  109. const_reverse_iterator crend() const
  110. {
  111. return const_reverse_iterator(begin());
  112. }
  113. /// capacity
  114. size_t size() const
  115. {
  116. return length_;
  117. }
  118. size_t length() const
  119. {
  120. return length_;
  121. }
  122. size_t max_size() const
  123. {
  124. return length_;
  125. }
  126. bool empty() const
  127. {
  128. return length_ == 0;
  129. }
  130. /// element access
  131. const char* data() const
  132. {
  133. return data_;
  134. }
  135. /// string operations
  136. int compare(string_ref x) const
  137. {
  138. size_t min_size = length_ < x.length_ ? length_ : x.length_;
  139. int r = memcmp(data_, x.data_, min_size);
  140. if (r < 0)
  141. return -1;
  142. if (r > 0)
  143. return 1;
  144. if (length_ < x.length_)
  145. return -1;
  146. if (length_ > x.length_)
  147. return 1;
  148. return 0;
  149. }
  150. bool starts_with(string_ref x) const
  151. {
  152. return length_ >= x.length_ && (memcmp(data_, x.data_, x.length_) == 0);
  153. }
  154. bool ends_with(string_ref x) const
  155. {
  156. return length_ >= x.length_ &&
  157. (memcmp(data_ + (length_ - x.length_), x.data_, x.length_) == 0);
  158. }
  159. size_t find(string_ref s) const
  160. {
  161. auto it = std::search(cbegin(), cend(), s.cbegin(), s.cend());
  162. return it == cend() ? npos : std::distance(cbegin(), it);
  163. }
  164. size_t find(char c) const
  165. {
  166. auto it = std::find(cbegin(), cend(), c);
  167. return it == cend() ? npos : std::distance(cbegin(), it);
  168. }
  169. string_ref substr(size_t pos, size_t n = npos) const
  170. {
  171. if (pos > length_)
  172. pos = length_;
  173. if (n > (length_ - pos))
  174. n = length_ - pos;
  175. return string_ref(data_ + pos, n);
  176. }
  177. private:
  178. const char* data_;
  179. size_t length_;
  180. };
  181. /// Comparison operators
  182. inline bool operator==(string_ref x, string_ref y)
  183. {
  184. return x.compare(y) == 0;
  185. }
  186. inline bool operator!=(string_ref x, string_ref y)
  187. {
  188. return x.compare(y) != 0;
  189. }
  190. inline bool operator<(string_ref x, string_ref y)
  191. {
  192. return x.compare(y) < 0;
  193. }
  194. inline bool operator<=(string_ref x, string_ref y)
  195. {
  196. return x.compare(y) <= 0;
  197. }
  198. inline bool operator>(string_ref x, string_ref y)
  199. {
  200. return x.compare(y) > 0;
  201. }
  202. inline bool operator>=(string_ref x, string_ref y)
  203. {
  204. return x.compare(y) >= 0;
  205. }
  206. inline std::ostream& operator<<(std::ostream& out, const string_ref& string)
  207. {
  208. return out << std::string(string.begin(), string.end());
  209. }
  210. } // namespace grpc
  211. #endif // GRPCPP_IMPL_CODEGEN_STRING_REF_H