grpc 1.26.0 → 1.27.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (198) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1652 -1519
  3. data/etc/roots.pem +44 -0
  4. data/include/grpc/grpc_security.h +37 -15
  5. data/include/grpc/grpc_security_constants.h +27 -0
  6. data/include/grpc/impl/codegen/grpc_types.h +14 -0
  7. data/include/grpc/impl/codegen/port_platform.h +1 -1
  8. data/src/core/ext/filters/client_channel/http_proxy.cc +4 -4
  9. data/src/core/ext/filters/client_channel/lb_policy.cc +4 -3
  10. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +176 -171
  11. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +88 -121
  12. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +28 -57
  13. data/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +8 -9
  14. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +53 -34
  15. data/src/core/ext/filters/client_channel/xds/xds_api.cc +394 -150
  16. data/src/core/ext/filters/client_channel/xds/xds_api.h +75 -35
  17. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc +59 -22
  18. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.h +13 -9
  19. data/src/core/ext/filters/client_channel/xds/xds_channel_secure.cc +8 -6
  20. data/src/core/ext/filters/client_channel/xds/xds_client.cc +456 -175
  21. data/src/core/ext/filters/client_channel/xds/xds_client.h +33 -21
  22. data/src/core/ext/filters/client_channel/xds/xds_client_stats.cc +5 -8
  23. data/src/core/ext/filters/client_channel/xds/xds_client_stats.h +18 -24
  24. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +2 -2
  25. data/src/core/ext/upb-generated/src/proto/grpc/lb/v1/load_balancer.upb.c +13 -5
  26. data/src/core/ext/upb-generated/src/proto/grpc/lb/v1/load_balancer.upb.h +34 -0
  27. data/src/core/lib/channel/channelz.h +11 -1
  28. data/src/core/lib/gpr/time_precise.cc +1 -1
  29. data/src/core/lib/gprpp/optional.h +26 -0
  30. data/src/core/lib/gprpp/string_view.h +14 -10
  31. data/src/core/lib/iomgr/executor.cc +1 -1
  32. data/src/core/lib/iomgr/fork_posix.cc +4 -0
  33. data/src/core/lib/iomgr/poller/eventmanager_libuv.cc +87 -0
  34. data/src/core/lib/iomgr/poller/eventmanager_libuv.h +88 -0
  35. data/src/core/lib/iomgr/socket_utils_common_posix.cc +14 -0
  36. data/src/core/lib/iomgr/socket_utils_posix.h +12 -0
  37. data/src/core/lib/iomgr/tcp_custom.h +3 -0
  38. data/src/core/lib/iomgr/tcp_posix.cc +607 -56
  39. data/src/core/lib/iomgr/tcp_server_custom.cc +15 -2
  40. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +8 -0
  41. data/src/core/lib/json/json.h +11 -1
  42. data/src/core/lib/json/json_reader.cc +206 -28
  43. data/src/core/lib/json/json_writer.cc +111 -24
  44. data/src/core/lib/security/credentials/composite/composite_credentials.cc +7 -0
  45. data/src/core/lib/security/credentials/composite/composite_credentials.h +5 -1
  46. data/src/core/lib/security/credentials/credentials.h +10 -1
  47. data/src/core/lib/security/credentials/fake/fake_credentials.h +2 -1
  48. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +1 -1
  49. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +6 -4
  50. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -1
  51. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.cc +20 -0
  52. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +8 -0
  53. data/src/core/lib/security/credentials/tls/{spiffe_credentials.cc → tls_credentials.cc} +23 -24
  54. data/src/core/lib/security/credentials/tls/{spiffe_credentials.h → tls_credentials.h} +9 -9
  55. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +13 -0
  56. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +22 -2
  57. data/src/core/lib/security/security_connector/load_system_roots_fallback.cc +2 -2
  58. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +2 -2
  59. data/src/core/lib/security/security_connector/local/local_security_connector.cc +30 -3
  60. data/src/core/lib/security/security_connector/ssl_utils.cc +45 -3
  61. data/src/core/lib/security/security_connector/ssl_utils.h +12 -0
  62. data/src/core/lib/security/security_connector/tls/{spiffe_security_connector.cc → tls_security_connector.cc} +82 -69
  63. data/src/core/lib/security/security_connector/tls/{spiffe_security_connector.h → tls_security_connector.h} +17 -18
  64. data/src/core/lib/security/transport/client_auth_filter.cc +33 -0
  65. data/src/core/lib/surface/completion_queue.cc +22 -1
  66. data/src/core/lib/surface/version.cc +1 -1
  67. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +11 -1
  68. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.h +1 -1
  69. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +3 -3
  70. data/src/core/tsi/fake_transport_security.cc +7 -3
  71. data/src/core/tsi/fake_transport_security.h +2 -0
  72. data/src/core/tsi/ssl_transport_security.cc +144 -8
  73. data/src/core/tsi/ssl_transport_security.h +15 -1
  74. data/src/core/tsi/transport_security.cc +13 -0
  75. data/src/core/tsi/transport_security_grpc.cc +2 -2
  76. data/src/core/tsi/transport_security_grpc.h +2 -2
  77. data/src/core/tsi/transport_security_interface.h +12 -0
  78. data/src/ruby/bin/math_pb.rb +5 -5
  79. data/src/ruby/ext/grpc/rb_call_credentials.c +4 -1
  80. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +2 -0
  81. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +4 -1
  82. data/src/ruby/lib/grpc/version.rb +1 -1
  83. data/src/ruby/pb/grpc/health/v1/health_pb.rb +3 -3
  84. data/src/ruby/pb/src/proto/grpc/testing/empty_pb.rb +1 -1
  85. data/src/ruby/pb/src/proto/grpc/testing/messages_pb.rb +23 -13
  86. data/third_party/abseil-cpp/absl/algorithm/algorithm.h +159 -0
  87. data/third_party/abseil-cpp/absl/base/attributes.h +621 -0
  88. data/third_party/abseil-cpp/absl/base/call_once.h +226 -0
  89. data/third_party/abseil-cpp/absl/base/casts.h +184 -0
  90. data/third_party/abseil-cpp/absl/base/config.h +665 -0
  91. data/third_party/abseil-cpp/absl/base/const_init.h +76 -0
  92. data/third_party/abseil-cpp/absl/base/dynamic_annotations.cc +129 -0
  93. data/third_party/abseil-cpp/absl/base/dynamic_annotations.h +389 -0
  94. data/third_party/abseil-cpp/absl/base/internal/atomic_hook.h +179 -0
  95. data/third_party/abseil-cpp/absl/base/internal/bits.h +218 -0
  96. data/third_party/abseil-cpp/absl/base/internal/cycleclock.cc +107 -0
  97. data/third_party/abseil-cpp/absl/base/internal/cycleclock.h +94 -0
  98. data/third_party/abseil-cpp/absl/base/internal/endian.h +266 -0
  99. data/third_party/abseil-cpp/absl/base/internal/hide_ptr.h +51 -0
  100. data/third_party/abseil-cpp/absl/base/internal/identity.h +37 -0
  101. data/third_party/abseil-cpp/absl/base/internal/inline_variable.h +107 -0
  102. data/third_party/abseil-cpp/absl/base/internal/invoke.h +187 -0
  103. data/third_party/abseil-cpp/absl/base/internal/low_level_scheduling.h +107 -0
  104. data/third_party/abseil-cpp/absl/base/internal/per_thread_tls.h +52 -0
  105. data/third_party/abseil-cpp/absl/base/internal/raw_logging.cc +238 -0
  106. data/third_party/abseil-cpp/absl/base/internal/raw_logging.h +182 -0
  107. data/third_party/abseil-cpp/absl/base/internal/scheduling_mode.h +58 -0
  108. data/third_party/abseil-cpp/absl/base/internal/spinlock.cc +233 -0
  109. data/third_party/abseil-cpp/absl/base/internal/spinlock.h +243 -0
  110. data/third_party/abseil-cpp/absl/base/internal/spinlock_akaros.inc +35 -0
  111. data/third_party/abseil-cpp/absl/base/internal/spinlock_linux.inc +67 -0
  112. data/third_party/abseil-cpp/absl/base/internal/spinlock_posix.inc +46 -0
  113. data/third_party/abseil-cpp/absl/base/internal/spinlock_wait.cc +81 -0
  114. data/third_party/abseil-cpp/absl/base/internal/spinlock_wait.h +93 -0
  115. data/third_party/abseil-cpp/absl/base/internal/spinlock_win32.inc +37 -0
  116. data/third_party/abseil-cpp/absl/base/internal/sysinfo.cc +416 -0
  117. data/third_party/abseil-cpp/absl/base/internal/sysinfo.h +66 -0
  118. data/third_party/abseil-cpp/absl/base/internal/thread_annotations.h +271 -0
  119. data/third_party/abseil-cpp/absl/base/internal/thread_identity.cc +152 -0
  120. data/third_party/abseil-cpp/absl/base/internal/thread_identity.h +259 -0
  121. data/third_party/abseil-cpp/absl/base/internal/throw_delegate.cc +108 -0
  122. data/third_party/abseil-cpp/absl/base/internal/throw_delegate.h +75 -0
  123. data/third_party/abseil-cpp/absl/base/internal/tsan_mutex_interface.h +66 -0
  124. data/third_party/abseil-cpp/absl/base/internal/unaligned_access.h +158 -0
  125. data/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.cc +103 -0
  126. data/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.h +124 -0
  127. data/third_party/abseil-cpp/absl/base/log_severity.cc +27 -0
  128. data/third_party/abseil-cpp/absl/base/log_severity.h +121 -0
  129. data/third_party/abseil-cpp/absl/base/macros.h +220 -0
  130. data/third_party/abseil-cpp/absl/base/optimization.h +181 -0
  131. data/third_party/abseil-cpp/absl/base/options.h +219 -0
  132. data/third_party/abseil-cpp/absl/base/policy_checks.h +111 -0
  133. data/third_party/abseil-cpp/absl/base/port.h +26 -0
  134. data/third_party/abseil-cpp/absl/base/thread_annotations.h +280 -0
  135. data/third_party/abseil-cpp/absl/container/inlined_vector.h +848 -0
  136. data/third_party/abseil-cpp/absl/container/internal/compressed_tuple.h +265 -0
  137. data/third_party/abseil-cpp/absl/container/internal/inlined_vector.h +892 -0
  138. data/third_party/abseil-cpp/absl/memory/memory.h +695 -0
  139. data/third_party/abseil-cpp/absl/meta/type_traits.h +759 -0
  140. data/third_party/abseil-cpp/absl/numeric/int128.cc +404 -0
  141. data/third_party/abseil-cpp/absl/numeric/int128.h +1091 -0
  142. data/third_party/abseil-cpp/absl/numeric/int128_have_intrinsic.inc +302 -0
  143. data/third_party/abseil-cpp/absl/numeric/int128_no_intrinsic.inc +308 -0
  144. data/third_party/abseil-cpp/absl/strings/ascii.cc +200 -0
  145. data/third_party/abseil-cpp/absl/strings/ascii.h +242 -0
  146. data/third_party/abseil-cpp/absl/strings/charconv.cc +985 -0
  147. data/third_party/abseil-cpp/absl/strings/charconv.h +119 -0
  148. data/third_party/abseil-cpp/absl/strings/escaping.cc +949 -0
  149. data/third_party/abseil-cpp/absl/strings/escaping.h +164 -0
  150. data/third_party/abseil-cpp/absl/strings/internal/char_map.h +156 -0
  151. data/third_party/abseil-cpp/absl/strings/internal/charconv_bigint.cc +359 -0
  152. data/third_party/abseil-cpp/absl/strings/internal/charconv_bigint.h +423 -0
  153. data/third_party/abseil-cpp/absl/strings/internal/charconv_parse.cc +504 -0
  154. data/third_party/abseil-cpp/absl/strings/internal/charconv_parse.h +99 -0
  155. data/third_party/abseil-cpp/absl/strings/internal/escaping.cc +180 -0
  156. data/third_party/abseil-cpp/absl/strings/internal/escaping.h +58 -0
  157. data/third_party/abseil-cpp/absl/strings/internal/memutil.cc +112 -0
  158. data/third_party/abseil-cpp/absl/strings/internal/memutil.h +148 -0
  159. data/third_party/abseil-cpp/absl/strings/internal/ostringstream.cc +36 -0
  160. data/third_party/abseil-cpp/absl/strings/internal/ostringstream.h +89 -0
  161. data/third_party/abseil-cpp/absl/strings/internal/resize_uninitialized.h +73 -0
  162. data/third_party/abseil-cpp/absl/strings/internal/stl_type_traits.h +248 -0
  163. data/third_party/abseil-cpp/absl/strings/internal/str_join_internal.h +314 -0
  164. data/third_party/abseil-cpp/absl/strings/internal/str_split_internal.h +455 -0
  165. data/third_party/abseil-cpp/absl/strings/internal/utf8.cc +53 -0
  166. data/third_party/abseil-cpp/absl/strings/internal/utf8.h +50 -0
  167. data/third_party/abseil-cpp/absl/strings/match.cc +40 -0
  168. data/third_party/abseil-cpp/absl/strings/match.h +90 -0
  169. data/third_party/abseil-cpp/absl/strings/numbers.cc +965 -0
  170. data/third_party/abseil-cpp/absl/strings/numbers.h +266 -0
  171. data/third_party/abseil-cpp/absl/strings/str_cat.cc +246 -0
  172. data/third_party/abseil-cpp/absl/strings/str_cat.h +408 -0
  173. data/third_party/abseil-cpp/absl/strings/str_join.h +293 -0
  174. data/third_party/abseil-cpp/absl/strings/str_replace.cc +82 -0
  175. data/third_party/abseil-cpp/absl/strings/str_replace.h +219 -0
  176. data/third_party/abseil-cpp/absl/strings/str_split.cc +139 -0
  177. data/third_party/abseil-cpp/absl/strings/str_split.h +513 -0
  178. data/third_party/abseil-cpp/absl/strings/string_view.cc +235 -0
  179. data/third_party/abseil-cpp/absl/strings/string_view.h +614 -0
  180. data/third_party/abseil-cpp/absl/strings/strip.h +91 -0
  181. data/third_party/abseil-cpp/absl/strings/substitute.cc +171 -0
  182. data/third_party/abseil-cpp/absl/strings/substitute.h +693 -0
  183. data/third_party/abseil-cpp/absl/types/bad_optional_access.cc +48 -0
  184. data/third_party/abseil-cpp/absl/types/bad_optional_access.h +78 -0
  185. data/third_party/abseil-cpp/absl/types/internal/optional.h +396 -0
  186. data/third_party/abseil-cpp/absl/types/internal/span.h +128 -0
  187. data/third_party/abseil-cpp/absl/types/optional.h +776 -0
  188. data/third_party/abseil-cpp/absl/types/span.h +713 -0
  189. data/third_party/abseil-cpp/absl/utility/utility.h +350 -0
  190. data/third_party/upb/upb/decode.c +4 -0
  191. data/third_party/upb/upb/port.c +0 -1
  192. data/third_party/upb/upb/port_def.inc +1 -3
  193. data/third_party/upb/upb/table.c +2 -1
  194. metadata +147 -45
  195. data/src/core/lib/json/json_common.h +0 -34
  196. data/src/core/lib/json/json_reader.h +0 -146
  197. data/src/core/lib/json/json_string.cc +0 -367
  198. data/src/core/lib/json/json_writer.h +0 -84
@@ -0,0 +1,235 @@
1
+ // Copyright 2017 The Abseil Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #include "absl/strings/string_view.h"
16
+
17
+ #ifndef ABSL_USES_STD_STRING_VIEW
18
+
19
+ #include <algorithm>
20
+ #include <climits>
21
+ #include <cstring>
22
+ #include <ostream>
23
+
24
+ #include "absl/strings/internal/memutil.h"
25
+
26
+ namespace absl {
27
+ ABSL_NAMESPACE_BEGIN
28
+
29
+ namespace {
30
+ void WritePadding(std::ostream& o, size_t pad) {
31
+ char fill_buf[32];
32
+ memset(fill_buf, o.fill(), sizeof(fill_buf));
33
+ while (pad) {
34
+ size_t n = std::min(pad, sizeof(fill_buf));
35
+ o.write(fill_buf, n);
36
+ pad -= n;
37
+ }
38
+ }
39
+
40
+ class LookupTable {
41
+ public:
42
+ // For each character in wanted, sets the index corresponding
43
+ // to the ASCII code of that character. This is used by
44
+ // the find_.*_of methods below to tell whether or not a character is in
45
+ // the lookup table in constant time.
46
+ explicit LookupTable(string_view wanted) {
47
+ for (char c : wanted) {
48
+ table_[Index(c)] = true;
49
+ }
50
+ }
51
+ bool operator[](char c) const { return table_[Index(c)]; }
52
+
53
+ private:
54
+ static unsigned char Index(char c) { return static_cast<unsigned char>(c); }
55
+ bool table_[UCHAR_MAX + 1] = {};
56
+ };
57
+
58
+ } // namespace
59
+
60
+ std::ostream& operator<<(std::ostream& o, string_view piece) {
61
+ std::ostream::sentry sentry(o);
62
+ if (sentry) {
63
+ size_t lpad = 0;
64
+ size_t rpad = 0;
65
+ if (static_cast<size_t>(o.width()) > piece.size()) {
66
+ size_t pad = o.width() - piece.size();
67
+ if ((o.flags() & o.adjustfield) == o.left) {
68
+ rpad = pad;
69
+ } else {
70
+ lpad = pad;
71
+ }
72
+ }
73
+ if (lpad) WritePadding(o, lpad);
74
+ o.write(piece.data(), piece.size());
75
+ if (rpad) WritePadding(o, rpad);
76
+ o.width(0);
77
+ }
78
+ return o;
79
+ }
80
+
81
+ string_view::size_type string_view::find(string_view s, size_type pos) const
82
+ noexcept {
83
+ if (empty() || pos > length_) {
84
+ if (empty() && pos == 0 && s.empty()) return 0;
85
+ return npos;
86
+ }
87
+ const char* result =
88
+ strings_internal::memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_);
89
+ return result ? result - ptr_ : npos;
90
+ }
91
+
92
+ string_view::size_type string_view::find(char c, size_type pos) const noexcept {
93
+ if (empty() || pos >= length_) {
94
+ return npos;
95
+ }
96
+ const char* result =
97
+ static_cast<const char*>(memchr(ptr_ + pos, c, length_ - pos));
98
+ return result != nullptr ? result - ptr_ : npos;
99
+ }
100
+
101
+ string_view::size_type string_view::rfind(string_view s, size_type pos) const
102
+ noexcept {
103
+ if (length_ < s.length_) return npos;
104
+ if (s.empty()) return std::min(length_, pos);
105
+ const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_;
106
+ const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
107
+ return result != last ? result - ptr_ : npos;
108
+ }
109
+
110
+ // Search range is [0..pos] inclusive. If pos == npos, search everything.
111
+ string_view::size_type string_view::rfind(char c, size_type pos) const
112
+ noexcept {
113
+ // Note: memrchr() is not available on Windows.
114
+ if (empty()) return npos;
115
+ for (size_type i = std::min(pos, length_ - 1);; --i) {
116
+ if (ptr_[i] == c) {
117
+ return i;
118
+ }
119
+ if (i == 0) break;
120
+ }
121
+ return npos;
122
+ }
123
+
124
+ string_view::size_type string_view::find_first_of(string_view s,
125
+ size_type pos) const
126
+ noexcept {
127
+ if (empty() || s.empty()) {
128
+ return npos;
129
+ }
130
+ // Avoid the cost of LookupTable() for a single-character search.
131
+ if (s.length_ == 1) return find_first_of(s.ptr_[0], pos);
132
+ LookupTable tbl(s);
133
+ for (size_type i = pos; i < length_; ++i) {
134
+ if (tbl[ptr_[i]]) {
135
+ return i;
136
+ }
137
+ }
138
+ return npos;
139
+ }
140
+
141
+ string_view::size_type string_view::find_first_not_of(string_view s,
142
+ size_type pos) const
143
+ noexcept {
144
+ if (empty()) return npos;
145
+ // Avoid the cost of LookupTable() for a single-character search.
146
+ if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos);
147
+ LookupTable tbl(s);
148
+ for (size_type i = pos; i < length_; ++i) {
149
+ if (!tbl[ptr_[i]]) {
150
+ return i;
151
+ }
152
+ }
153
+ return npos;
154
+ }
155
+
156
+ string_view::size_type string_view::find_first_not_of(char c,
157
+ size_type pos) const
158
+ noexcept {
159
+ if (empty()) return npos;
160
+ for (; pos < length_; ++pos) {
161
+ if (ptr_[pos] != c) {
162
+ return pos;
163
+ }
164
+ }
165
+ return npos;
166
+ }
167
+
168
+ string_view::size_type string_view::find_last_of(string_view s,
169
+ size_type pos) const noexcept {
170
+ if (empty() || s.empty()) return npos;
171
+ // Avoid the cost of LookupTable() for a single-character search.
172
+ if (s.length_ == 1) return find_last_of(s.ptr_[0], pos);
173
+ LookupTable tbl(s);
174
+ for (size_type i = std::min(pos, length_ - 1);; --i) {
175
+ if (tbl[ptr_[i]]) {
176
+ return i;
177
+ }
178
+ if (i == 0) break;
179
+ }
180
+ return npos;
181
+ }
182
+
183
+ string_view::size_type string_view::find_last_not_of(string_view s,
184
+ size_type pos) const
185
+ noexcept {
186
+ if (empty()) return npos;
187
+ size_type i = std::min(pos, length_ - 1);
188
+ if (s.empty()) return i;
189
+ // Avoid the cost of LookupTable() for a single-character search.
190
+ if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos);
191
+ LookupTable tbl(s);
192
+ for (;; --i) {
193
+ if (!tbl[ptr_[i]]) {
194
+ return i;
195
+ }
196
+ if (i == 0) break;
197
+ }
198
+ return npos;
199
+ }
200
+
201
+ string_view::size_type string_view::find_last_not_of(char c,
202
+ size_type pos) const
203
+ noexcept {
204
+ if (empty()) return npos;
205
+ size_type i = std::min(pos, length_ - 1);
206
+ for (;; --i) {
207
+ if (ptr_[i] != c) {
208
+ return i;
209
+ }
210
+ if (i == 0) break;
211
+ }
212
+ return npos;
213
+ }
214
+
215
+ // MSVC has non-standard behavior that implicitly creates definitions for static
216
+ // const members. These implicit definitions conflict with explicit out-of-class
217
+ // member definitions that are required by the C++ standard, resulting in
218
+ // LNK1169 "multiply defined" errors at link time. __declspec(selectany) asks
219
+ // MSVC to choose only one definition for the symbol it decorates. See details
220
+ // at https://msdn.microsoft.com/en-us/library/34h23df8(v=vs.100).aspx
221
+ #ifdef _MSC_VER
222
+ #define ABSL_STRING_VIEW_SELECTANY __declspec(selectany)
223
+ #else
224
+ #define ABSL_STRING_VIEW_SELECTANY
225
+ #endif
226
+
227
+ ABSL_STRING_VIEW_SELECTANY
228
+ constexpr string_view::size_type string_view::npos;
229
+ ABSL_STRING_VIEW_SELECTANY
230
+ constexpr string_view::size_type string_view::kMaxSize;
231
+
232
+ ABSL_NAMESPACE_END
233
+ } // namespace absl
234
+
235
+ #endif // ABSL_USES_STD_STRING_VIEW
@@ -0,0 +1,614 @@
1
+ //
2
+ // Copyright 2017 The Abseil Authors.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // https://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ //
16
+ // -----------------------------------------------------------------------------
17
+ // File: string_view.h
18
+ // -----------------------------------------------------------------------------
19
+ //
20
+ // This file contains the definition of the `absl::string_view` class. A
21
+ // `string_view` points to a contiguous span of characters, often part or all of
22
+ // another `std::string`, double-quoted string literal, character array, or even
23
+ // another `string_view`.
24
+ //
25
+ // This `absl::string_view` abstraction is designed to be a drop-in
26
+ // replacement for the C++17 `std::string_view` abstraction.
27
+ #ifndef ABSL_STRINGS_STRING_VIEW_H_
28
+ #define ABSL_STRINGS_STRING_VIEW_H_
29
+
30
+ #include <algorithm>
31
+ #include <cassert>
32
+ #include <cstddef>
33
+ #include <cstring>
34
+ #include <iosfwd>
35
+ #include <iterator>
36
+ #include <limits>
37
+ #include <string>
38
+
39
+ #include "absl/base/config.h"
40
+ #include "absl/base/internal/throw_delegate.h"
41
+ #include "absl/base/macros.h"
42
+ #include "absl/base/optimization.h"
43
+ #include "absl/base/port.h"
44
+
45
+ #ifdef ABSL_USES_STD_STRING_VIEW
46
+
47
+ #include <string_view> // IWYU pragma: export
48
+
49
+ namespace absl {
50
+ ABSL_NAMESPACE_BEGIN
51
+ using std::string_view;
52
+ ABSL_NAMESPACE_END
53
+ } // namespace absl
54
+
55
+ #else // ABSL_USES_STD_STRING_VIEW
56
+
57
+ #if ABSL_HAVE_BUILTIN(__builtin_memcmp) || \
58
+ (defined(__GNUC__) && !defined(__clang__))
59
+ #define ABSL_INTERNAL_STRING_VIEW_MEMCMP __builtin_memcmp
60
+ #else // ABSL_HAVE_BUILTIN(__builtin_memcmp)
61
+ #define ABSL_INTERNAL_STRING_VIEW_MEMCMP memcmp
62
+ #endif // ABSL_HAVE_BUILTIN(__builtin_memcmp)
63
+
64
+ namespace absl {
65
+ ABSL_NAMESPACE_BEGIN
66
+
67
+ // absl::string_view
68
+ //
69
+ // A `string_view` provides a lightweight view into the string data provided by
70
+ // a `std::string`, double-quoted string literal, character array, or even
71
+ // another `string_view`. A `string_view` does *not* own the string to which it
72
+ // points, and that data cannot be modified through the view.
73
+ //
74
+ // You can use `string_view` as a function or method parameter anywhere a
75
+ // parameter can receive a double-quoted string literal, `const char*`,
76
+ // `std::string`, or another `absl::string_view` argument with no need to copy
77
+ // the string data. Systematic use of `string_view` within function arguments
78
+ // reduces data copies and `strlen()` calls.
79
+ //
80
+ // Because of its small size, prefer passing `string_view` by value:
81
+ //
82
+ // void MyFunction(absl::string_view arg);
83
+ //
84
+ // If circumstances require, you may also pass one by const reference:
85
+ //
86
+ // void MyFunction(const absl::string_view& arg); // not preferred
87
+ //
88
+ // Passing by value generates slightly smaller code for many architectures.
89
+ //
90
+ // In either case, the source data of the `string_view` must outlive the
91
+ // `string_view` itself.
92
+ //
93
+ // A `string_view` is also suitable for local variables if you know that the
94
+ // lifetime of the underlying object is longer than the lifetime of your
95
+ // `string_view` variable. However, beware of binding a `string_view` to a
96
+ // temporary value:
97
+ //
98
+ // // BAD use of string_view: lifetime problem
99
+ // absl::string_view sv = obj.ReturnAString();
100
+ //
101
+ // // GOOD use of string_view: str outlives sv
102
+ // std::string str = obj.ReturnAString();
103
+ // absl::string_view sv = str;
104
+ //
105
+ // Due to lifetime issues, a `string_view` is sometimes a poor choice for a
106
+ // return value and usually a poor choice for a data member. If you do use a
107
+ // `string_view` this way, it is your responsibility to ensure that the object
108
+ // pointed to by the `string_view` outlives the `string_view`.
109
+ //
110
+ // A `string_view` may represent a whole string or just part of a string. For
111
+ // example, when splitting a string, `std::vector<absl::string_view>` is a
112
+ // natural data type for the output.
113
+ //
114
+ // When constructed from a source which is NUL-terminated, the `string_view`
115
+ // itself will not include the NUL-terminator unless a specific size (including
116
+ // the NUL) is passed to the constructor. As a result, common idioms that work
117
+ // on NUL-terminated strings do not work on `string_view` objects. If you write
118
+ // code that scans a `string_view`, you must check its length rather than test
119
+ // for nul, for example. Note, however, that nuls may still be embedded within
120
+ // a `string_view` explicitly.
121
+ //
122
+ // You may create a null `string_view` in two ways:
123
+ //
124
+ // absl::string_view sv;
125
+ // absl::string_view sv(nullptr, 0);
126
+ //
127
+ // For the above, `sv.data() == nullptr`, `sv.length() == 0`, and
128
+ // `sv.empty() == true`. Also, if you create a `string_view` with a non-null
129
+ // pointer then `sv.data() != nullptr`. Thus, you can use `string_view()` to
130
+ // signal an undefined value that is different from other `string_view` values
131
+ // in a similar fashion to how `const char* p1 = nullptr;` is different from
132
+ // `const char* p2 = "";`. However, in practice, it is not recommended to rely
133
+ // on this behavior.
134
+ //
135
+ // Be careful not to confuse a null `string_view` with an empty one. A null
136
+ // `string_view` is an empty `string_view`, but some empty `string_view`s are
137
+ // not null. Prefer checking for emptiness over checking for null.
138
+ //
139
+ // There are many ways to create an empty string_view:
140
+ //
141
+ // const char* nullcp = nullptr;
142
+ // // string_view.size() will return 0 in all cases.
143
+ // absl::string_view();
144
+ // absl::string_view(nullcp, 0);
145
+ // absl::string_view("");
146
+ // absl::string_view("", 0);
147
+ // absl::string_view("abcdef", 0);
148
+ // absl::string_view("abcdef" + 6, 0);
149
+ //
150
+ // All empty `string_view` objects whether null or not, are equal:
151
+ //
152
+ // absl::string_view() == absl::string_view("", 0)
153
+ // absl::string_view(nullptr, 0) == absl::string_view("abcdef"+6, 0)
154
+ class string_view {
155
+ public:
156
+ using traits_type = std::char_traits<char>;
157
+ using value_type = char;
158
+ using pointer = char*;
159
+ using const_pointer = const char*;
160
+ using reference = char&;
161
+ using const_reference = const char&;
162
+ using const_iterator = const char*;
163
+ using iterator = const_iterator;
164
+ using const_reverse_iterator = std::reverse_iterator<const_iterator>;
165
+ using reverse_iterator = const_reverse_iterator;
166
+ using size_type = size_t;
167
+ using difference_type = std::ptrdiff_t;
168
+
169
+ static constexpr size_type npos = static_cast<size_type>(-1);
170
+
171
+ // Null `string_view` constructor
172
+ constexpr string_view() noexcept : ptr_(nullptr), length_(0) {}
173
+
174
+ // Implicit constructors
175
+
176
+ template <typename Allocator>
177
+ string_view( // NOLINT(runtime/explicit)
178
+ const std::basic_string<char, std::char_traits<char>, Allocator>&
179
+ str) noexcept
180
+ // This is implemented in terms of `string_view(p, n)` so `str.size()`
181
+ // doesn't need to be reevaluated after `ptr_` is set.
182
+ : string_view(str.data(), str.size()) {}
183
+
184
+ // Implicit constructor of a `string_view` from NUL-terminated `str`. When
185
+ // accepting possibly null strings, use `absl::NullSafeStringView(str)`
186
+ // instead (see below).
187
+ constexpr string_view(const char* str) // NOLINT(runtime/explicit)
188
+ : ptr_(str),
189
+ length_(str ? CheckLengthInternal(StrlenInternal(str)) : 0) {}
190
+
191
+ // Implicit constructor of a `string_view` from a `const char*` and length.
192
+ constexpr string_view(const char* data, size_type len)
193
+ : ptr_(data), length_(CheckLengthInternal(len)) {}
194
+
195
+ // NOTE: Harmlessly omitted to work around gdb bug.
196
+ // constexpr string_view(const string_view&) noexcept = default;
197
+ // string_view& operator=(const string_view&) noexcept = default;
198
+
199
+ // Iterators
200
+
201
+ // string_view::begin()
202
+ //
203
+ // Returns an iterator pointing to the first character at the beginning of the
204
+ // `string_view`, or `end()` if the `string_view` is empty.
205
+ constexpr const_iterator begin() const noexcept { return ptr_; }
206
+
207
+ // string_view::end()
208
+ //
209
+ // Returns an iterator pointing just beyond the last character at the end of
210
+ // the `string_view`. This iterator acts as a placeholder; attempting to
211
+ // access it results in undefined behavior.
212
+ constexpr const_iterator end() const noexcept { return ptr_ + length_; }
213
+
214
+ // string_view::cbegin()
215
+ //
216
+ // Returns a const iterator pointing to the first character at the beginning
217
+ // of the `string_view`, or `end()` if the `string_view` is empty.
218
+ constexpr const_iterator cbegin() const noexcept { return begin(); }
219
+
220
+ // string_view::cend()
221
+ //
222
+ // Returns a const iterator pointing just beyond the last character at the end
223
+ // of the `string_view`. This pointer acts as a placeholder; attempting to
224
+ // access its element results in undefined behavior.
225
+ constexpr const_iterator cend() const noexcept { return end(); }
226
+
227
+ // string_view::rbegin()
228
+ //
229
+ // Returns a reverse iterator pointing to the last character at the end of the
230
+ // `string_view`, or `rend()` if the `string_view` is empty.
231
+ const_reverse_iterator rbegin() const noexcept {
232
+ return const_reverse_iterator(end());
233
+ }
234
+
235
+ // string_view::rend()
236
+ //
237
+ // Returns a reverse iterator pointing just before the first character at the
238
+ // beginning of the `string_view`. This pointer acts as a placeholder;
239
+ // attempting to access its element results in undefined behavior.
240
+ const_reverse_iterator rend() const noexcept {
241
+ return const_reverse_iterator(begin());
242
+ }
243
+
244
+ // string_view::crbegin()
245
+ //
246
+ // Returns a const reverse iterator pointing to the last character at the end
247
+ // of the `string_view`, or `crend()` if the `string_view` is empty.
248
+ const_reverse_iterator crbegin() const noexcept { return rbegin(); }
249
+
250
+ // string_view::crend()
251
+ //
252
+ // Returns a const reverse iterator pointing just before the first character
253
+ // at the beginning of the `string_view`. This pointer acts as a placeholder;
254
+ // attempting to access its element results in undefined behavior.
255
+ const_reverse_iterator crend() const noexcept { return rend(); }
256
+
257
+ // Capacity Utilities
258
+
259
+ // string_view::size()
260
+ //
261
+ // Returns the number of characters in the `string_view`.
262
+ constexpr size_type size() const noexcept {
263
+ return length_;
264
+ }
265
+
266
+ // string_view::length()
267
+ //
268
+ // Returns the number of characters in the `string_view`. Alias for `size()`.
269
+ constexpr size_type length() const noexcept { return size(); }
270
+
271
+ // string_view::max_size()
272
+ //
273
+ // Returns the maximum number of characters the `string_view` can hold.
274
+ constexpr size_type max_size() const noexcept { return kMaxSize; }
275
+
276
+ // string_view::empty()
277
+ //
278
+ // Checks if the `string_view` is empty (refers to no characters).
279
+ constexpr bool empty() const noexcept { return length_ == 0; }
280
+
281
+ // string_view::operator[]
282
+ //
283
+ // Returns the ith element of the `string_view` using the array operator.
284
+ // Note that this operator does not perform any bounds checking.
285
+ constexpr const_reference operator[](size_type i) const { return ptr_[i]; }
286
+
287
+ // string_view::at()
288
+ //
289
+ // Returns the ith element of the `string_view`. Bounds checking is performed,
290
+ // and an exception of type `std::out_of_range` will be thrown on invalid
291
+ // access.
292
+ constexpr const_reference at(size_type i) const {
293
+ return ABSL_PREDICT_TRUE(i < size())
294
+ ? ptr_[i]
295
+ : ((void)base_internal::ThrowStdOutOfRange(
296
+ "absl::string_view::at"),
297
+ ptr_[i]);
298
+ }
299
+
300
+ // string_view::front()
301
+ //
302
+ // Returns the first element of a `string_view`.
303
+ constexpr const_reference front() const { return ptr_[0]; }
304
+
305
+ // string_view::back()
306
+ //
307
+ // Returns the last element of a `string_view`.
308
+ constexpr const_reference back() const { return ptr_[size() - 1]; }
309
+
310
+ // string_view::data()
311
+ //
312
+ // Returns a pointer to the underlying character array (which is of course
313
+ // stored elsewhere). Note that `string_view::data()` may contain embedded nul
314
+ // characters, but the returned buffer may or may not be NUL-terminated;
315
+ // therefore, do not pass `data()` to a routine that expects a NUL-terminated
316
+ // std::string.
317
+ constexpr const_pointer data() const noexcept { return ptr_; }
318
+
319
+ // Modifiers
320
+
321
+ // string_view::remove_prefix()
322
+ //
323
+ // Removes the first `n` characters from the `string_view`. Note that the
324
+ // underlying std::string is not changed, only the view.
325
+ void remove_prefix(size_type n) {
326
+ assert(n <= length_);
327
+ ptr_ += n;
328
+ length_ -= n;
329
+ }
330
+
331
+ // string_view::remove_suffix()
332
+ //
333
+ // Removes the last `n` characters from the `string_view`. Note that the
334
+ // underlying std::string is not changed, only the view.
335
+ void remove_suffix(size_type n) {
336
+ assert(n <= length_);
337
+ length_ -= n;
338
+ }
339
+
340
+ // string_view::swap()
341
+ //
342
+ // Swaps this `string_view` with another `string_view`.
343
+ void swap(string_view& s) noexcept {
344
+ auto t = *this;
345
+ *this = s;
346
+ s = t;
347
+ }
348
+
349
+ // Explicit conversion operators
350
+
351
+ // Converts to `std::basic_string`.
352
+ template <typename A>
353
+ explicit operator std::basic_string<char, traits_type, A>() const {
354
+ if (!data()) return {};
355
+ return std::basic_string<char, traits_type, A>(data(), size());
356
+ }
357
+
358
+ // string_view::copy()
359
+ //
360
+ // Copies the contents of the `string_view` at offset `pos` and length `n`
361
+ // into `buf`.
362
+ size_type copy(char* buf, size_type n, size_type pos = 0) const {
363
+ if (ABSL_PREDICT_FALSE(pos > length_)) {
364
+ base_internal::ThrowStdOutOfRange("absl::string_view::copy");
365
+ }
366
+ size_type rlen = (std::min)(length_ - pos, n);
367
+ if (rlen > 0) {
368
+ const char* start = ptr_ + pos;
369
+ traits_type::copy(buf, start, rlen);
370
+ }
371
+ return rlen;
372
+ }
373
+
374
+ // string_view::substr()
375
+ //
376
+ // Returns a "substring" of the `string_view` (at offset `pos` and length
377
+ // `n`) as another string_view. This function throws `std::out_of_bounds` if
378
+ // `pos > size`.
379
+ string_view substr(size_type pos, size_type n = npos) const {
380
+ if (ABSL_PREDICT_FALSE(pos > length_))
381
+ base_internal::ThrowStdOutOfRange("absl::string_view::substr");
382
+ n = (std::min)(n, length_ - pos);
383
+ return string_view(ptr_ + pos, n);
384
+ }
385
+
386
+ // string_view::compare()
387
+ //
388
+ // Performs a lexicographical comparison between the `string_view` and
389
+ // another `absl::string_view`, returning -1 if `this` is less than, 0 if
390
+ // `this` is equal to, and 1 if `this` is greater than the passed std::string
391
+ // view. Note that in the case of data equality, a further comparison is made
392
+ // on the respective sizes of the two `string_view`s to determine which is
393
+ // smaller, equal, or greater.
394
+ constexpr int compare(string_view x) const noexcept {
395
+ return CompareImpl(
396
+ length_, x.length_,
397
+ length_ == 0 || x.length_ == 0
398
+ ? 0
399
+ : ABSL_INTERNAL_STRING_VIEW_MEMCMP(
400
+ ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_));
401
+ }
402
+
403
+ // Overload of `string_view::compare()` for comparing a substring of the
404
+ // 'string_view` and another `absl::string_view`.
405
+ int compare(size_type pos1, size_type count1, string_view v) const {
406
+ return substr(pos1, count1).compare(v);
407
+ }
408
+
409
+ // Overload of `string_view::compare()` for comparing a substring of the
410
+ // `string_view` and a substring of another `absl::string_view`.
411
+ int compare(size_type pos1, size_type count1, string_view v, size_type pos2,
412
+ size_type count2) const {
413
+ return substr(pos1, count1).compare(v.substr(pos2, count2));
414
+ }
415
+
416
+ // Overload of `string_view::compare()` for comparing a `string_view` and a
417
+ // a different C-style std::string `s`.
418
+ int compare(const char* s) const { return compare(string_view(s)); }
419
+
420
+ // Overload of `string_view::compare()` for comparing a substring of the
421
+ // `string_view` and a different std::string C-style std::string `s`.
422
+ int compare(size_type pos1, size_type count1, const char* s) const {
423
+ return substr(pos1, count1).compare(string_view(s));
424
+ }
425
+
426
+ // Overload of `string_view::compare()` for comparing a substring of the
427
+ // `string_view` and a substring of a different C-style std::string `s`.
428
+ int compare(size_type pos1, size_type count1, const char* s,
429
+ size_type count2) const {
430
+ return substr(pos1, count1).compare(string_view(s, count2));
431
+ }
432
+
433
+ // Find Utilities
434
+
435
+ // string_view::find()
436
+ //
437
+ // Finds the first occurrence of the substring `s` within the `string_view`,
438
+ // returning the position of the first character's match, or `npos` if no
439
+ // match was found.
440
+ size_type find(string_view s, size_type pos = 0) const noexcept;
441
+
442
+ // Overload of `string_view::find()` for finding the given character `c`
443
+ // within the `string_view`.
444
+ size_type find(char c, size_type pos = 0) const noexcept;
445
+
446
+ // string_view::rfind()
447
+ //
448
+ // Finds the last occurrence of a substring `s` within the `string_view`,
449
+ // returning the position of the first character's match, or `npos` if no
450
+ // match was found.
451
+ size_type rfind(string_view s, size_type pos = npos) const
452
+ noexcept;
453
+
454
+ // Overload of `string_view::rfind()` for finding the last given character `c`
455
+ // within the `string_view`.
456
+ size_type rfind(char c, size_type pos = npos) const noexcept;
457
+
458
+ // string_view::find_first_of()
459
+ //
460
+ // Finds the first occurrence of any of the characters in `s` within the
461
+ // `string_view`, returning the start position of the match, or `npos` if no
462
+ // match was found.
463
+ size_type find_first_of(string_view s, size_type pos = 0) const
464
+ noexcept;
465
+
466
+ // Overload of `string_view::find_first_of()` for finding a character `c`
467
+ // within the `string_view`.
468
+ size_type find_first_of(char c, size_type pos = 0) const
469
+ noexcept {
470
+ return find(c, pos);
471
+ }
472
+
473
+ // string_view::find_last_of()
474
+ //
475
+ // Finds the last occurrence of any of the characters in `s` within the
476
+ // `string_view`, returning the start position of the match, or `npos` if no
477
+ // match was found.
478
+ size_type find_last_of(string_view s, size_type pos = npos) const
479
+ noexcept;
480
+
481
+ // Overload of `string_view::find_last_of()` for finding a character `c`
482
+ // within the `string_view`.
483
+ size_type find_last_of(char c, size_type pos = npos) const
484
+ noexcept {
485
+ return rfind(c, pos);
486
+ }
487
+
488
+ // string_view::find_first_not_of()
489
+ //
490
+ // Finds the first occurrence of any of the characters not in `s` within the
491
+ // `string_view`, returning the start position of the first non-match, or
492
+ // `npos` if no non-match was found.
493
+ size_type find_first_not_of(string_view s, size_type pos = 0) const noexcept;
494
+
495
+ // Overload of `string_view::find_first_not_of()` for finding a character
496
+ // that is not `c` within the `string_view`.
497
+ size_type find_first_not_of(char c, size_type pos = 0) const noexcept;
498
+
499
+ // string_view::find_last_not_of()
500
+ //
501
+ // Finds the last occurrence of any of the characters not in `s` within the
502
+ // `string_view`, returning the start position of the last non-match, or
503
+ // `npos` if no non-match was found.
504
+ size_type find_last_not_of(string_view s,
505
+ size_type pos = npos) const noexcept;
506
+
507
+ // Overload of `string_view::find_last_not_of()` for finding a character
508
+ // that is not `c` within the `string_view`.
509
+ size_type find_last_not_of(char c, size_type pos = npos) const
510
+ noexcept;
511
+
512
+ private:
513
+ static constexpr size_type kMaxSize =
514
+ (std::numeric_limits<difference_type>::max)();
515
+
516
+ static constexpr size_type CheckLengthInternal(size_type len) {
517
+ return (void)ABSL_ASSERT(len <= kMaxSize), len;
518
+ }
519
+
520
+ static constexpr size_type StrlenInternal(const char* str) {
521
+ #if defined(_MSC_VER) && _MSC_VER >= 1910 && !defined(__clang__)
522
+ // MSVC 2017+ can evaluate this at compile-time.
523
+ const char* begin = str;
524
+ while (*str != '\0') ++str;
525
+ return str - begin;
526
+ #elif ABSL_HAVE_BUILTIN(__builtin_strlen) || \
527
+ (defined(__GNUC__) && !defined(__clang__))
528
+ // GCC has __builtin_strlen according to
529
+ // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html, but
530
+ // ABSL_HAVE_BUILTIN doesn't detect that, so we use the extra checks above.
531
+ // __builtin_strlen is constexpr.
532
+ return __builtin_strlen(str);
533
+ #else
534
+ return str ? strlen(str) : 0;
535
+ #endif
536
+ }
537
+
538
+ static constexpr int CompareImpl(size_type length_a, size_type length_b,
539
+ int compare_result) {
540
+ return compare_result == 0 ? static_cast<int>(length_a > length_b) -
541
+ static_cast<int>(length_a < length_b)
542
+ : static_cast<int>(compare_result > 0) -
543
+ static_cast<int>(compare_result < 0);
544
+ }
545
+
546
+ const char* ptr_;
547
+ size_type length_;
548
+ };
549
+
550
+ // This large function is defined inline so that in a fairly common case where
551
+ // one of the arguments is a literal, the compiler can elide a lot of the
552
+ // following comparisons.
553
+ constexpr bool operator==(string_view x, string_view y) noexcept {
554
+ return x.size() == y.size() &&
555
+ (x.empty() ||
556
+ ABSL_INTERNAL_STRING_VIEW_MEMCMP(x.data(), y.data(), x.size()) == 0);
557
+ }
558
+
559
+ constexpr bool operator!=(string_view x, string_view y) noexcept {
560
+ return !(x == y);
561
+ }
562
+
563
+ constexpr bool operator<(string_view x, string_view y) noexcept {
564
+ return x.compare(y) < 0;
565
+ }
566
+
567
+ constexpr bool operator>(string_view x, string_view y) noexcept {
568
+ return y < x;
569
+ }
570
+
571
+ constexpr bool operator<=(string_view x, string_view y) noexcept {
572
+ return !(y < x);
573
+ }
574
+
575
+ constexpr bool operator>=(string_view x, string_view y) noexcept {
576
+ return !(x < y);
577
+ }
578
+
579
+ // IO Insertion Operator
580
+ std::ostream& operator<<(std::ostream& o, string_view piece);
581
+
582
+ ABSL_NAMESPACE_END
583
+ } // namespace absl
584
+
585
+ #undef ABSL_INTERNAL_STRING_VIEW_MEMCMP
586
+
587
+ #endif // ABSL_USES_STD_STRING_VIEW
588
+
589
+ namespace absl {
590
+ ABSL_NAMESPACE_BEGIN
591
+
592
+ // ClippedSubstr()
593
+ //
594
+ // Like `s.substr(pos, n)`, but clips `pos` to an upper bound of `s.size()`.
595
+ // Provided because std::string_view::substr throws if `pos > size()`
596
+ inline string_view ClippedSubstr(string_view s, size_t pos,
597
+ size_t n = string_view::npos) {
598
+ pos = (std::min)(pos, static_cast<size_t>(s.size()));
599
+ return s.substr(pos, n);
600
+ }
601
+
602
+ // NullSafeStringView()
603
+ //
604
+ // Creates an `absl::string_view` from a pointer `p` even if it's null-valued.
605
+ // This function should be used where an `absl::string_view` can be created from
606
+ // a possibly-null pointer.
607
+ constexpr string_view NullSafeStringView(const char* p) {
608
+ return p ? string_view(p) : string_view();
609
+ }
610
+
611
+ ABSL_NAMESPACE_END
612
+ } // namespace absl
613
+
614
+ #endif // ABSL_STRINGS_STRING_VIEW_H_