grpc 1.26.0 → 1.27.0.pre1

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