string_ref.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_SUPPORT_STRING_REF_H
  19. #define GRPCPP_SUPPORT_STRING_REF_H
  20. #include <string.h>
  21. #include <algorithm>
  22. #include <iosfwd>
  23. #include <iostream>
  24. #include <iterator>
  25. #include <grpcpp/support/config.h>
  26. #include <util/stream/output.h>
  27. namespace grpc {
  28. /// This class is a non owning reference to a string.
  29. ///
  30. /// It should be a strict subset of the upcoming std::string_ref.
  31. ///
  32. /// \see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html
  33. ///
  34. /// The constexpr is dropped or replaced with const for legacy compiler
  35. /// compatibility.
  36. class string_ref {
  37. public:
  38. /// types
  39. typedef const char* const_iterator;
  40. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  41. /// constants
  42. const static size_t npos;
  43. /// construct/copy.
  44. string_ref() : data_(nullptr), length_(0) {}
  45. string_ref(const string_ref& other)
  46. : data_(other.data_), length_(other.length_) {}
  47. // NOLINTNEXTLINE(bugprone-unhandled-self-assignment)
  48. string_ref& operator=(const string_ref& rhs) {
  49. data_ = rhs.data_;
  50. length_ = rhs.length_;
  51. return *this;
  52. }
  53. // NOLINTNEXTLINE(google-explicit-constructor)
  54. string_ref(const char* s) : data_(s), length_(strlen(s)) {}
  55. string_ref(const char* s, size_t l) : data_(s), length_(l) {}
  56. // NOLINTNEXTLINE(google-explicit-constructor)
  57. string_ref(const TString& s) : data_(s.data()), length_(s.length()) {}
  58. /// iterators
  59. const_iterator begin() const { return data_; }
  60. const_iterator end() const { return data_ + length_; }
  61. const_iterator cbegin() const { return data_; }
  62. const_iterator cend() const { return data_ + length_; }
  63. const_reverse_iterator rbegin() const {
  64. return const_reverse_iterator(end());
  65. }
  66. const_reverse_iterator rend() const {
  67. return const_reverse_iterator(begin());
  68. }
  69. const_reverse_iterator crbegin() const {
  70. return const_reverse_iterator(end());
  71. }
  72. const_reverse_iterator crend() const {
  73. return const_reverse_iterator(begin());
  74. }
  75. /// capacity
  76. size_t size() const { return length_; }
  77. size_t length() const { return length_; }
  78. size_t max_size() const { return length_; }
  79. bool empty() const { return length_ == 0; }
  80. /// element access
  81. const char* data() const { return data_; }
  82. /// string operations
  83. int compare(string_ref x) const {
  84. size_t min_size = length_ < x.length_ ? length_ : x.length_;
  85. int r = memcmp(data_, x.data_, min_size);
  86. if (r < 0) return -1;
  87. if (r > 0) return 1;
  88. if (length_ < x.length_) return -1;
  89. if (length_ > x.length_) return 1;
  90. return 0;
  91. }
  92. bool starts_with(string_ref x) const {
  93. return length_ >= x.length_ && (memcmp(data_, x.data_, x.length_) == 0);
  94. }
  95. bool ends_with(string_ref x) const {
  96. return length_ >= x.length_ &&
  97. (memcmp(data_ + (length_ - x.length_), x.data_, x.length_) == 0);
  98. }
  99. size_t find(string_ref s) const {
  100. auto it = std::search(cbegin(), cend(), s.cbegin(), s.cend());
  101. return it == cend() ? npos : std::distance(cbegin(), it);
  102. }
  103. size_t find(char c) const {
  104. auto it = std::find(cbegin(), cend(), c);
  105. return it == cend() ? npos : std::distance(cbegin(), it);
  106. }
  107. string_ref substr(size_t pos, size_t n = npos) const {
  108. if (pos > length_) pos = length_;
  109. if (n > (length_ - pos)) n = length_ - pos;
  110. return string_ref(data_ + pos, n);
  111. }
  112. private:
  113. const char* data_;
  114. size_t length_;
  115. };
  116. /// Comparison operators
  117. inline bool operator==(string_ref x, string_ref y) { return x.compare(y) == 0; }
  118. inline bool operator!=(string_ref x, string_ref y) { return x.compare(y) != 0; }
  119. inline bool operator<(string_ref x, string_ref y) { return x.compare(y) < 0; }
  120. inline bool operator<=(string_ref x, string_ref y) { return x.compare(y) <= 0; }
  121. inline bool operator>(string_ref x, string_ref y) { return x.compare(y) > 0; }
  122. inline bool operator>=(string_ref x, string_ref y) { return x.compare(y) >= 0; }
  123. inline IOutputStream& operator<<(IOutputStream& out, const string_ref& string) {
  124. TString t(string.begin(), string.end());
  125. return out << t;
  126. }
  127. } // namespace grpc
  128. #endif // GRPCPP_SUPPORT_STRING_REF_H