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,82 @@
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/str_replace.h"
16
+
17
+ #include "absl/strings/str_cat.h"
18
+
19
+ namespace absl {
20
+ ABSL_NAMESPACE_BEGIN
21
+ namespace strings_internal {
22
+
23
+ using FixedMapping =
24
+ std::initializer_list<std::pair<absl::string_view, absl::string_view>>;
25
+
26
+ // Applies the ViableSubstitutions in subs_ptr to the absl::string_view s, and
27
+ // stores the result in *result_ptr. Returns the number of substitutions that
28
+ // occurred.
29
+ int ApplySubstitutions(
30
+ absl::string_view s,
31
+ std::vector<strings_internal::ViableSubstitution>* subs_ptr,
32
+ std::string* result_ptr) {
33
+ auto& subs = *subs_ptr;
34
+ int substitutions = 0;
35
+ size_t pos = 0;
36
+ while (!subs.empty()) {
37
+ auto& sub = subs.back();
38
+ if (sub.offset >= pos) {
39
+ if (pos <= s.size()) {
40
+ StrAppend(result_ptr, s.substr(pos, sub.offset - pos), sub.replacement);
41
+ }
42
+ pos = sub.offset + sub.old.size();
43
+ substitutions += 1;
44
+ }
45
+ sub.offset = s.find(sub.old, pos);
46
+ if (sub.offset == s.npos) {
47
+ subs.pop_back();
48
+ } else {
49
+ // Insertion sort to ensure the last ViableSubstitution continues to be
50
+ // before all the others.
51
+ size_t index = subs.size();
52
+ while (--index && subs[index - 1].OccursBefore(subs[index])) {
53
+ std::swap(subs[index], subs[index - 1]);
54
+ }
55
+ }
56
+ }
57
+ result_ptr->append(s.data() + pos, s.size() - pos);
58
+ return substitutions;
59
+ }
60
+
61
+ } // namespace strings_internal
62
+
63
+ // We can implement this in terms of the generic StrReplaceAll, but
64
+ // we must specify the template overload because C++ cannot deduce the type
65
+ // of an initializer_list parameter to a function, and also if we don't specify
66
+ // the type, we just call ourselves.
67
+ //
68
+ // Note that we implement them here, rather than in the header, so that they
69
+ // aren't inlined.
70
+
71
+ std::string StrReplaceAll(absl::string_view s,
72
+ strings_internal::FixedMapping replacements) {
73
+ return StrReplaceAll<strings_internal::FixedMapping>(s, replacements);
74
+ }
75
+
76
+ int StrReplaceAll(strings_internal::FixedMapping replacements,
77
+ std::string* target) {
78
+ return StrReplaceAll<strings_internal::FixedMapping>(replacements, target);
79
+ }
80
+
81
+ ABSL_NAMESPACE_END
82
+ } // namespace absl
@@ -0,0 +1,219 @@
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: str_replace.h
18
+ // -----------------------------------------------------------------------------
19
+ //
20
+ // This file defines `absl::StrReplaceAll()`, a general-purpose string
21
+ // replacement function designed for large, arbitrary text substitutions,
22
+ // especially on strings which you are receiving from some other system for
23
+ // further processing (e.g. processing regular expressions, escaping HTML
24
+ // entities, etc.). `StrReplaceAll` is designed to be efficient even when only
25
+ // one substitution is being performed, or when substitution is rare.
26
+ //
27
+ // If the string being modified is known at compile-time, and the substitutions
28
+ // vary, `absl::Substitute()` may be a better choice.
29
+ //
30
+ // Example:
31
+ //
32
+ // std::string html_escaped = absl::StrReplaceAll(user_input, {
33
+ // {"&", "&amp;"},
34
+ // {"<", "&lt;"},
35
+ // {">", "&gt;"},
36
+ // {"\"", "&quot;"},
37
+ // {"'", "&#39;"}});
38
+ #ifndef ABSL_STRINGS_STR_REPLACE_H_
39
+ #define ABSL_STRINGS_STR_REPLACE_H_
40
+
41
+ #include <string>
42
+ #include <utility>
43
+ #include <vector>
44
+
45
+ #include "absl/base/attributes.h"
46
+ #include "absl/strings/string_view.h"
47
+
48
+ namespace absl {
49
+ ABSL_NAMESPACE_BEGIN
50
+
51
+ // StrReplaceAll()
52
+ //
53
+ // Replaces character sequences within a given string with replacements provided
54
+ // within an initializer list of key/value pairs. Candidate replacements are
55
+ // considered in order as they occur within the string, with earlier matches
56
+ // taking precedence, and longer matches taking precedence for candidates
57
+ // starting at the same position in the string. Once a substitution is made, the
58
+ // replaced text is not considered for any further substitutions.
59
+ //
60
+ // Example:
61
+ //
62
+ // std::string s = absl::StrReplaceAll(
63
+ // "$who bought $count #Noun. Thanks $who!",
64
+ // {{"$count", absl::StrCat(5)},
65
+ // {"$who", "Bob"},
66
+ // {"#Noun", "Apples"}});
67
+ // EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
68
+ ABSL_MUST_USE_RESULT std::string StrReplaceAll(
69
+ absl::string_view s,
70
+ std::initializer_list<std::pair<absl::string_view, absl::string_view>>
71
+ replacements);
72
+
73
+ // Overload of `StrReplaceAll()` to accept a container of key/value replacement
74
+ // pairs (typically either an associative map or a `std::vector` of `std::pair`
75
+ // elements). A vector of pairs is generally more efficient.
76
+ //
77
+ // Examples:
78
+ //
79
+ // std::map<const absl::string_view, const absl::string_view> replacements;
80
+ // replacements["$who"] = "Bob";
81
+ // replacements["$count"] = "5";
82
+ // replacements["#Noun"] = "Apples";
83
+ // std::string s = absl::StrReplaceAll(
84
+ // "$who bought $count #Noun. Thanks $who!",
85
+ // replacements);
86
+ // EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
87
+ //
88
+ // // A std::vector of std::pair elements can be more efficient.
89
+ // std::vector<std::pair<const absl::string_view, std::string>> replacements;
90
+ // replacements.push_back({"&", "&amp;"});
91
+ // replacements.push_back({"<", "&lt;"});
92
+ // replacements.push_back({">", "&gt;"});
93
+ // std::string s = absl::StrReplaceAll("if (ptr < &foo)",
94
+ // replacements);
95
+ // EXPECT_EQ("if (ptr &lt; &amp;foo)", s);
96
+ template <typename StrToStrMapping>
97
+ std::string StrReplaceAll(absl::string_view s,
98
+ const StrToStrMapping& replacements);
99
+
100
+ // Overload of `StrReplaceAll()` to replace character sequences within a given
101
+ // output string *in place* with replacements provided within an initializer
102
+ // list of key/value pairs, returning the number of substitutions that occurred.
103
+ //
104
+ // Example:
105
+ //
106
+ // std::string s = std::string("$who bought $count #Noun. Thanks $who!");
107
+ // int count;
108
+ // count = absl::StrReplaceAll({{"$count", absl::StrCat(5)},
109
+ // {"$who", "Bob"},
110
+ // {"#Noun", "Apples"}}, &s);
111
+ // EXPECT_EQ(count, 4);
112
+ // EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
113
+ int StrReplaceAll(
114
+ std::initializer_list<std::pair<absl::string_view, absl::string_view>>
115
+ replacements,
116
+ std::string* target);
117
+
118
+ // Overload of `StrReplaceAll()` to replace patterns within a given output
119
+ // string *in place* with replacements provided within a container of key/value
120
+ // pairs.
121
+ //
122
+ // Example:
123
+ //
124
+ // std::string s = std::string("if (ptr < &foo)");
125
+ // int count = absl::StrReplaceAll({{"&", "&amp;"},
126
+ // {"<", "&lt;"},
127
+ // {">", "&gt;"}}, &s);
128
+ // EXPECT_EQ(count, 2);
129
+ // EXPECT_EQ("if (ptr &lt; &amp;foo)", s);
130
+ template <typename StrToStrMapping>
131
+ int StrReplaceAll(const StrToStrMapping& replacements, std::string* target);
132
+
133
+ // Implementation details only, past this point.
134
+ namespace strings_internal {
135
+
136
+ struct ViableSubstitution {
137
+ absl::string_view old;
138
+ absl::string_view replacement;
139
+ size_t offset;
140
+
141
+ ViableSubstitution(absl::string_view old_str,
142
+ absl::string_view replacement_str, size_t offset_val)
143
+ : old(old_str), replacement(replacement_str), offset(offset_val) {}
144
+
145
+ // One substitution occurs "before" another (takes priority) if either
146
+ // it has the lowest offset, or it has the same offset but a larger size.
147
+ bool OccursBefore(const ViableSubstitution& y) const {
148
+ if (offset != y.offset) return offset < y.offset;
149
+ return old.size() > y.old.size();
150
+ }
151
+ };
152
+
153
+ // Build a vector of ViableSubstitutions based on the given list of
154
+ // replacements. subs can be implemented as a priority_queue. However, it turns
155
+ // out that most callers have small enough a list of substitutions that the
156
+ // overhead of such a queue isn't worth it.
157
+ template <typename StrToStrMapping>
158
+ std::vector<ViableSubstitution> FindSubstitutions(
159
+ absl::string_view s, const StrToStrMapping& replacements) {
160
+ std::vector<ViableSubstitution> subs;
161
+ subs.reserve(replacements.size());
162
+
163
+ for (const auto& rep : replacements) {
164
+ using std::get;
165
+ absl::string_view old(get<0>(rep));
166
+
167
+ size_t pos = s.find(old);
168
+ if (pos == s.npos) continue;
169
+
170
+ // Ignore attempts to replace "". This condition is almost never true,
171
+ // but above condition is frequently true. That's why we test for this
172
+ // now and not before.
173
+ if (old.empty()) continue;
174
+
175
+ subs.emplace_back(old, get<1>(rep), pos);
176
+
177
+ // Insertion sort to ensure the last ViableSubstitution comes before
178
+ // all the others.
179
+ size_t index = subs.size();
180
+ while (--index && subs[index - 1].OccursBefore(subs[index])) {
181
+ std::swap(subs[index], subs[index - 1]);
182
+ }
183
+ }
184
+ return subs;
185
+ }
186
+
187
+ int ApplySubstitutions(absl::string_view s,
188
+ std::vector<ViableSubstitution>* subs_ptr,
189
+ std::string* result_ptr);
190
+
191
+ } // namespace strings_internal
192
+
193
+ template <typename StrToStrMapping>
194
+ std::string StrReplaceAll(absl::string_view s,
195
+ const StrToStrMapping& replacements) {
196
+ auto subs = strings_internal::FindSubstitutions(s, replacements);
197
+ std::string result;
198
+ result.reserve(s.size());
199
+ strings_internal::ApplySubstitutions(s, &subs, &result);
200
+ return result;
201
+ }
202
+
203
+ template <typename StrToStrMapping>
204
+ int StrReplaceAll(const StrToStrMapping& replacements, std::string* target) {
205
+ auto subs = strings_internal::FindSubstitutions(*target, replacements);
206
+ if (subs.empty()) return 0;
207
+
208
+ std::string result;
209
+ result.reserve(target->size());
210
+ int substitutions =
211
+ strings_internal::ApplySubstitutions(*target, &subs, &result);
212
+ target->swap(result);
213
+ return substitutions;
214
+ }
215
+
216
+ ABSL_NAMESPACE_END
217
+ } // namespace absl
218
+
219
+ #endif // ABSL_STRINGS_STR_REPLACE_H_
@@ -0,0 +1,139 @@
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/str_split.h"
16
+
17
+ #include <algorithm>
18
+ #include <cassert>
19
+ #include <cstdint>
20
+ #include <cstdlib>
21
+ #include <cstring>
22
+ #include <iterator>
23
+ #include <limits>
24
+ #include <memory>
25
+
26
+ #include "absl/base/internal/raw_logging.h"
27
+ #include "absl/strings/ascii.h"
28
+
29
+ namespace absl {
30
+ ABSL_NAMESPACE_BEGIN
31
+
32
+ namespace {
33
+
34
+ // This GenericFind() template function encapsulates the finding algorithm
35
+ // shared between the ByString and ByAnyChar delimiters. The FindPolicy
36
+ // template parameter allows each delimiter to customize the actual find
37
+ // function to use and the length of the found delimiter. For example, the
38
+ // Literal delimiter will ultimately use absl::string_view::find(), and the
39
+ // AnyOf delimiter will use absl::string_view::find_first_of().
40
+ template <typename FindPolicy>
41
+ absl::string_view GenericFind(absl::string_view text,
42
+ absl::string_view delimiter, size_t pos,
43
+ FindPolicy find_policy) {
44
+ if (delimiter.empty() && text.length() > 0) {
45
+ // Special case for empty std::string delimiters: always return a zero-length
46
+ // absl::string_view referring to the item at position 1 past pos.
47
+ return absl::string_view(text.data() + pos + 1, 0);
48
+ }
49
+ size_t found_pos = absl::string_view::npos;
50
+ absl::string_view found(text.data() + text.size(),
51
+ 0); // By default, not found
52
+ found_pos = find_policy.Find(text, delimiter, pos);
53
+ if (found_pos != absl::string_view::npos) {
54
+ found = absl::string_view(text.data() + found_pos,
55
+ find_policy.Length(delimiter));
56
+ }
57
+ return found;
58
+ }
59
+
60
+ // Finds using absl::string_view::find(), therefore the length of the found
61
+ // delimiter is delimiter.length().
62
+ struct LiteralPolicy {
63
+ size_t Find(absl::string_view text, absl::string_view delimiter, size_t pos) {
64
+ return text.find(delimiter, pos);
65
+ }
66
+ size_t Length(absl::string_view delimiter) { return delimiter.length(); }
67
+ };
68
+
69
+ // Finds using absl::string_view::find_first_of(), therefore the length of the
70
+ // found delimiter is 1.
71
+ struct AnyOfPolicy {
72
+ size_t Find(absl::string_view text, absl::string_view delimiter, size_t pos) {
73
+ return text.find_first_of(delimiter, pos);
74
+ }
75
+ size_t Length(absl::string_view /* delimiter */) { return 1; }
76
+ };
77
+
78
+ } // namespace
79
+
80
+ //
81
+ // ByString
82
+ //
83
+
84
+ ByString::ByString(absl::string_view sp) : delimiter_(sp) {}
85
+
86
+ absl::string_view ByString::Find(absl::string_view text, size_t pos) const {
87
+ if (delimiter_.length() == 1) {
88
+ // Much faster to call find on a single character than on an
89
+ // absl::string_view.
90
+ size_t found_pos = text.find(delimiter_[0], pos);
91
+ if (found_pos == absl::string_view::npos)
92
+ return absl::string_view(text.data() + text.size(), 0);
93
+ return text.substr(found_pos, 1);
94
+ }
95
+ return GenericFind(text, delimiter_, pos, LiteralPolicy());
96
+ }
97
+
98
+ //
99
+ // ByChar
100
+ //
101
+
102
+ absl::string_view ByChar::Find(absl::string_view text, size_t pos) const {
103
+ size_t found_pos = text.find(c_, pos);
104
+ if (found_pos == absl::string_view::npos)
105
+ return absl::string_view(text.data() + text.size(), 0);
106
+ return text.substr(found_pos, 1);
107
+ }
108
+
109
+ //
110
+ // ByAnyChar
111
+ //
112
+
113
+ ByAnyChar::ByAnyChar(absl::string_view sp) : delimiters_(sp) {}
114
+
115
+ absl::string_view ByAnyChar::Find(absl::string_view text, size_t pos) const {
116
+ return GenericFind(text, delimiters_, pos, AnyOfPolicy());
117
+ }
118
+
119
+ //
120
+ // ByLength
121
+ //
122
+ ByLength::ByLength(ptrdiff_t length) : length_(length) {
123
+ ABSL_RAW_CHECK(length > 0, "");
124
+ }
125
+
126
+ absl::string_view ByLength::Find(absl::string_view text,
127
+ size_t pos) const {
128
+ pos = std::min(pos, text.size()); // truncate `pos`
129
+ absl::string_view substr = text.substr(pos);
130
+ // If the std::string is shorter than the chunk size we say we
131
+ // "can't find the delimiter" so this will be the last chunk.
132
+ if (substr.length() <= static_cast<size_t>(length_))
133
+ return absl::string_view(text.data() + text.size(), 0);
134
+
135
+ return absl::string_view(substr.data() + length_, 0);
136
+ }
137
+
138
+ ABSL_NAMESPACE_END
139
+ } // namespace absl
@@ -0,0 +1,513 @@
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: str_split.h
18
+ // -----------------------------------------------------------------------------
19
+ //
20
+ // This file contains functions for splitting strings. It defines the main
21
+ // `StrSplit()` function, several delimiters for determining the boundaries on
22
+ // which to split the string, and predicates for filtering delimited results.
23
+ // `StrSplit()` adapts the returned collection to the type specified by the
24
+ // caller.
25
+ //
26
+ // Example:
27
+ //
28
+ // // Splits the given string on commas. Returns the results in a
29
+ // // vector of strings.
30
+ // std::vector<std::string> v = absl::StrSplit("a,b,c", ',');
31
+ // // Can also use ","
32
+ // // v[0] == "a", v[1] == "b", v[2] == "c"
33
+ //
34
+ // See StrSplit() below for more information.
35
+ #ifndef ABSL_STRINGS_STR_SPLIT_H_
36
+ #define ABSL_STRINGS_STR_SPLIT_H_
37
+
38
+ #include <algorithm>
39
+ #include <cstddef>
40
+ #include <map>
41
+ #include <set>
42
+ #include <string>
43
+ #include <utility>
44
+ #include <vector>
45
+
46
+ #include "absl/base/internal/raw_logging.h"
47
+ #include "absl/strings/internal/str_split_internal.h"
48
+ #include "absl/strings/string_view.h"
49
+ #include "absl/strings/strip.h"
50
+
51
+ namespace absl {
52
+ ABSL_NAMESPACE_BEGIN
53
+
54
+ //------------------------------------------------------------------------------
55
+ // Delimiters
56
+ //------------------------------------------------------------------------------
57
+ //
58
+ // `StrSplit()` uses delimiters to define the boundaries between elements in the
59
+ // provided input. Several `Delimiter` types are defined below. If a string
60
+ // (`const char*`, `std::string`, or `absl::string_view`) is passed in place of
61
+ // an explicit `Delimiter` object, `StrSplit()` treats it the same way as if it
62
+ // were passed a `ByString` delimiter.
63
+ //
64
+ // A `Delimiter` is an object with a `Find()` function that knows how to find
65
+ // the first occurrence of itself in a given `absl::string_view`.
66
+ //
67
+ // The following `Delimiter` types are available for use within `StrSplit()`:
68
+ //
69
+ // - `ByString` (default for string arguments)
70
+ // - `ByChar` (default for a char argument)
71
+ // - `ByAnyChar`
72
+ // - `ByLength`
73
+ // - `MaxSplits`
74
+ //
75
+ // A Delimiter's `Find()` member function will be passed an input `text` that is
76
+ // to be split and a position (`pos`) to begin searching for the next delimiter
77
+ // in `text`. The returned absl::string_view should refer to the next occurrence
78
+ // (after `pos`) of the represented delimiter; this returned absl::string_view
79
+ // represents the next location where the input `text` should be broken.
80
+ //
81
+ // The returned absl::string_view may be zero-length if the Delimiter does not
82
+ // represent a part of the string (e.g., a fixed-length delimiter). If no
83
+ // delimiter is found in the input `text`, a zero-length absl::string_view
84
+ // referring to `text.end()` should be returned (e.g.,
85
+ // `text.substr(text.size())`). It is important that the returned
86
+ // absl::string_view always be within the bounds of the input `text` given as an
87
+ // argument--it must not refer to a string that is physically located outside of
88
+ // the given string.
89
+ //
90
+ // The following example is a simple Delimiter object that is created with a
91
+ // single char and will look for that char in the text passed to the `Find()`
92
+ // function:
93
+ //
94
+ // struct SimpleDelimiter {
95
+ // const char c_;
96
+ // explicit SimpleDelimiter(char c) : c_(c) {}
97
+ // absl::string_view Find(absl::string_view text, size_t pos) {
98
+ // auto found = text.find(c_, pos);
99
+ // if (found == absl::string_view::npos)
100
+ // return text.substr(text.size());
101
+ //
102
+ // return text.substr(found, 1);
103
+ // }
104
+ // };
105
+
106
+ // ByString
107
+ //
108
+ // A sub-string delimiter. If `StrSplit()` is passed a string in place of a
109
+ // `Delimiter` object, the string will be implicitly converted into a
110
+ // `ByString` delimiter.
111
+ //
112
+ // Example:
113
+ //
114
+ // // Because a string literal is converted to an `absl::ByString`,
115
+ // // the following two splits are equivalent.
116
+ //
117
+ // std::vector<std::string> v1 = absl::StrSplit("a, b, c", ", ");
118
+ //
119
+ // using absl::ByString;
120
+ // std::vector<std::string> v2 = absl::StrSplit("a, b, c",
121
+ // ByString(", "));
122
+ // // v[0] == "a", v[1] == "b", v[2] == "c"
123
+ class ByString {
124
+ public:
125
+ explicit ByString(absl::string_view sp);
126
+ absl::string_view Find(absl::string_view text, size_t pos) const;
127
+
128
+ private:
129
+ const std::string delimiter_;
130
+ };
131
+
132
+ // ByChar
133
+ //
134
+ // A single character delimiter. `ByChar` is functionally equivalent to a
135
+ // 1-char string within a `ByString` delimiter, but slightly more efficient.
136
+ //
137
+ // Example:
138
+ //
139
+ // // Because a char literal is converted to a absl::ByChar,
140
+ // // the following two splits are equivalent.
141
+ // std::vector<std::string> v1 = absl::StrSplit("a,b,c", ',');
142
+ // using absl::ByChar;
143
+ // std::vector<std::string> v2 = absl::StrSplit("a,b,c", ByChar(','));
144
+ // // v[0] == "a", v[1] == "b", v[2] == "c"
145
+ //
146
+ // `ByChar` is also the default delimiter if a single character is given
147
+ // as the delimiter to `StrSplit()`. For example, the following calls are
148
+ // equivalent:
149
+ //
150
+ // std::vector<std::string> v = absl::StrSplit("a-b", '-');
151
+ //
152
+ // using absl::ByChar;
153
+ // std::vector<std::string> v = absl::StrSplit("a-b", ByChar('-'));
154
+ //
155
+ class ByChar {
156
+ public:
157
+ explicit ByChar(char c) : c_(c) {}
158
+ absl::string_view Find(absl::string_view text, size_t pos) const;
159
+
160
+ private:
161
+ char c_;
162
+ };
163
+
164
+ // ByAnyChar
165
+ //
166
+ // A delimiter that will match any of the given byte-sized characters within
167
+ // its provided string.
168
+ //
169
+ // Note: this delimiter works with single-byte string data, but does not work
170
+ // with variable-width encodings, such as UTF-8.
171
+ //
172
+ // Example:
173
+ //
174
+ // using absl::ByAnyChar;
175
+ // std::vector<std::string> v = absl::StrSplit("a,b=c", ByAnyChar(",="));
176
+ // // v[0] == "a", v[1] == "b", v[2] == "c"
177
+ //
178
+ // If `ByAnyChar` is given the empty string, it behaves exactly like
179
+ // `ByString` and matches each individual character in the input string.
180
+ //
181
+ class ByAnyChar {
182
+ public:
183
+ explicit ByAnyChar(absl::string_view sp);
184
+ absl::string_view Find(absl::string_view text, size_t pos) const;
185
+
186
+ private:
187
+ const std::string delimiters_;
188
+ };
189
+
190
+ // ByLength
191
+ //
192
+ // A delimiter for splitting into equal-length strings. The length argument to
193
+ // the constructor must be greater than 0.
194
+ //
195
+ // Note: this delimiter works with single-byte string data, but does not work
196
+ // with variable-width encodings, such as UTF-8.
197
+ //
198
+ // Example:
199
+ //
200
+ // using absl::ByLength;
201
+ // std::vector<std::string> v = absl::StrSplit("123456789", ByLength(3));
202
+
203
+ // // v[0] == "123", v[1] == "456", v[2] == "789"
204
+ //
205
+ // Note that the string does not have to be a multiple of the fixed split
206
+ // length. In such a case, the last substring will be shorter.
207
+ //
208
+ // using absl::ByLength;
209
+ // std::vector<std::string> v = absl::StrSplit("12345", ByLength(2));
210
+ //
211
+ // // v[0] == "12", v[1] == "34", v[2] == "5"
212
+ class ByLength {
213
+ public:
214
+ explicit ByLength(ptrdiff_t length);
215
+ absl::string_view Find(absl::string_view text, size_t pos) const;
216
+
217
+ private:
218
+ const ptrdiff_t length_;
219
+ };
220
+
221
+ namespace strings_internal {
222
+
223
+ // A traits-like metafunction for selecting the default Delimiter object type
224
+ // for a particular Delimiter type. The base case simply exposes type Delimiter
225
+ // itself as the delimiter's Type. However, there are specializations for
226
+ // string-like objects that map them to the ByString delimiter object.
227
+ // This allows functions like absl::StrSplit() and absl::MaxSplits() to accept
228
+ // string-like objects (e.g., ',') as delimiter arguments but they will be
229
+ // treated as if a ByString delimiter was given.
230
+ template <typename Delimiter>
231
+ struct SelectDelimiter {
232
+ using type = Delimiter;
233
+ };
234
+
235
+ template <>
236
+ struct SelectDelimiter<char> {
237
+ using type = ByChar;
238
+ };
239
+ template <>
240
+ struct SelectDelimiter<char*> {
241
+ using type = ByString;
242
+ };
243
+ template <>
244
+ struct SelectDelimiter<const char*> {
245
+ using type = ByString;
246
+ };
247
+ template <>
248
+ struct SelectDelimiter<absl::string_view> {
249
+ using type = ByString;
250
+ };
251
+ template <>
252
+ struct SelectDelimiter<std::string> {
253
+ using type = ByString;
254
+ };
255
+
256
+ // Wraps another delimiter and sets a max number of matches for that delimiter.
257
+ template <typename Delimiter>
258
+ class MaxSplitsImpl {
259
+ public:
260
+ MaxSplitsImpl(Delimiter delimiter, int limit)
261
+ : delimiter_(delimiter), limit_(limit), count_(0) {}
262
+ absl::string_view Find(absl::string_view text, size_t pos) {
263
+ if (count_++ == limit_) {
264
+ return absl::string_view(text.data() + text.size(),
265
+ 0); // No more matches.
266
+ }
267
+ return delimiter_.Find(text, pos);
268
+ }
269
+
270
+ private:
271
+ Delimiter delimiter_;
272
+ const int limit_;
273
+ int count_;
274
+ };
275
+
276
+ } // namespace strings_internal
277
+
278
+ // MaxSplits()
279
+ //
280
+ // A delimiter that limits the number of matches which can occur to the passed
281
+ // `limit`. The last element in the returned collection will contain all
282
+ // remaining unsplit pieces, which may contain instances of the delimiter.
283
+ // The collection will contain at most `limit` + 1 elements.
284
+ // Example:
285
+ //
286
+ // using absl::MaxSplits;
287
+ // std::vector<std::string> v = absl::StrSplit("a,b,c", MaxSplits(',', 1));
288
+ //
289
+ // // v[0] == "a", v[1] == "b,c"
290
+ template <typename Delimiter>
291
+ inline strings_internal::MaxSplitsImpl<
292
+ typename strings_internal::SelectDelimiter<Delimiter>::type>
293
+ MaxSplits(Delimiter delimiter, int limit) {
294
+ typedef
295
+ typename strings_internal::SelectDelimiter<Delimiter>::type DelimiterType;
296
+ return strings_internal::MaxSplitsImpl<DelimiterType>(
297
+ DelimiterType(delimiter), limit);
298
+ }
299
+
300
+ //------------------------------------------------------------------------------
301
+ // Predicates
302
+ //------------------------------------------------------------------------------
303
+ //
304
+ // Predicates filter the results of a `StrSplit()` by determining whether or not
305
+ // a resultant element is included in the result set. A predicate may be passed
306
+ // as an optional third argument to the `StrSplit()` function.
307
+ //
308
+ // Predicates are unary functions (or functors) that take a single
309
+ // `absl::string_view` argument and return a bool indicating whether the
310
+ // argument should be included (`true`) or excluded (`false`).
311
+ //
312
+ // Predicates are useful when filtering out empty substrings. By default, empty
313
+ // substrings may be returned by `StrSplit()`, which is similar to the way split
314
+ // functions work in other programming languages.
315
+
316
+ // AllowEmpty()
317
+ //
318
+ // Always returns `true`, indicating that all strings--including empty
319
+ // strings--should be included in the split output. This predicate is not
320
+ // strictly needed because this is the default behavior of `StrSplit()`;
321
+ // however, it might be useful at some call sites to make the intent explicit.
322
+ //
323
+ // Example:
324
+ //
325
+ // std::vector<std::string> v = absl::StrSplit(" a , ,,b,", ',', AllowEmpty());
326
+ //
327
+ // // v[0] == " a ", v[1] == " ", v[2] == "", v[3] = "b", v[4] == ""
328
+ struct AllowEmpty {
329
+ bool operator()(absl::string_view) const { return true; }
330
+ };
331
+
332
+ // SkipEmpty()
333
+ //
334
+ // Returns `false` if the given `absl::string_view` is empty, indicating that
335
+ // `StrSplit()` should omit the empty string.
336
+ //
337
+ // Example:
338
+ //
339
+ // std::vector<std::string> v = absl::StrSplit(",a,,b,", ',', SkipEmpty());
340
+ //
341
+ // // v[0] == "a", v[1] == "b"
342
+ //
343
+ // Note: `SkipEmpty()` does not consider a string containing only whitespace
344
+ // to be empty. To skip such whitespace as well, use the `SkipWhitespace()`
345
+ // predicate.
346
+ struct SkipEmpty {
347
+ bool operator()(absl::string_view sp) const { return !sp.empty(); }
348
+ };
349
+
350
+ // SkipWhitespace()
351
+ //
352
+ // Returns `false` if the given `absl::string_view` is empty *or* contains only
353
+ // whitespace, indicating that `StrSplit()` should omit the string.
354
+ //
355
+ // Example:
356
+ //
357
+ // std::vector<std::string> v = absl::StrSplit(" a , ,,b,",
358
+ // ',', SkipWhitespace());
359
+ // // v[0] == " a ", v[1] == "b"
360
+ //
361
+ // // SkipEmpty() would return whitespace elements
362
+ // std::vector<std::string> v = absl::StrSplit(" a , ,,b,", ',', SkipEmpty());
363
+ // // v[0] == " a ", v[1] == " ", v[2] == "b"
364
+ struct SkipWhitespace {
365
+ bool operator()(absl::string_view sp) const {
366
+ sp = absl::StripAsciiWhitespace(sp);
367
+ return !sp.empty();
368
+ }
369
+ };
370
+
371
+ //------------------------------------------------------------------------------
372
+ // StrSplit()
373
+ //------------------------------------------------------------------------------
374
+
375
+ // StrSplit()
376
+ //
377
+ // Splits a given string based on the provided `Delimiter` object, returning the
378
+ // elements within the type specified by the caller. Optionally, you may pass a
379
+ // `Predicate` to `StrSplit()` indicating whether to include or exclude the
380
+ // resulting element within the final result set. (See the overviews for
381
+ // Delimiters and Predicates above.)
382
+ //
383
+ // Example:
384
+ //
385
+ // std::vector<std::string> v = absl::StrSplit("a,b,c,d", ',');
386
+ // // v[0] == "a", v[1] == "b", v[2] == "c", v[3] == "d"
387
+ //
388
+ // You can also provide an explicit `Delimiter` object:
389
+ //
390
+ // Example:
391
+ //
392
+ // using absl::ByAnyChar;
393
+ // std::vector<std::string> v = absl::StrSplit("a,b=c", ByAnyChar(",="));
394
+ // // v[0] == "a", v[1] == "b", v[2] == "c"
395
+ //
396
+ // See above for more information on delimiters.
397
+ //
398
+ // By default, empty strings are included in the result set. You can optionally
399
+ // include a third `Predicate` argument to apply a test for whether the
400
+ // resultant element should be included in the result set:
401
+ //
402
+ // Example:
403
+ //
404
+ // std::vector<std::string> v = absl::StrSplit(" a , ,,b,",
405
+ // ',', SkipWhitespace());
406
+ // // v[0] == " a ", v[1] == "b"
407
+ //
408
+ // See above for more information on predicates.
409
+ //
410
+ //------------------------------------------------------------------------------
411
+ // StrSplit() Return Types
412
+ //------------------------------------------------------------------------------
413
+ //
414
+ // The `StrSplit()` function adapts the returned collection to the collection
415
+ // specified by the caller (e.g. `std::vector` above). The returned collections
416
+ // may contain `std::string`, `absl::string_view` (in which case the original
417
+ // string being split must ensure that it outlives the collection), or any
418
+ // object that can be explicitly created from an `absl::string_view`. This
419
+ // behavior works for:
420
+ //
421
+ // 1) All standard STL containers including `std::vector`, `std::list`,
422
+ // `std::deque`, `std::set`,`std::multiset`, 'std::map`, and `std::multimap`
423
+ // 2) `std::pair` (which is not actually a container). See below.
424
+ //
425
+ // Example:
426
+ //
427
+ // // The results are returned as `absl::string_view` objects. Note that we
428
+ // // have to ensure that the input string outlives any results.
429
+ // std::vector<absl::string_view> v = absl::StrSplit("a,b,c", ',');
430
+ //
431
+ // // Stores results in a std::set<std::string>, which also performs
432
+ // // de-duplication and orders the elements in ascending order.
433
+ // std::set<std::string> a = absl::StrSplit("b,a,c,a,b", ',');
434
+ // // v[0] == "a", v[1] == "b", v[2] = "c"
435
+ //
436
+ // // `StrSplit()` can be used within a range-based for loop, in which case
437
+ // // each element will be of type `absl::string_view`.
438
+ // std::vector<std::string> v;
439
+ // for (const auto sv : absl::StrSplit("a,b,c", ',')) {
440
+ // if (sv != "b") v.emplace_back(sv);
441
+ // }
442
+ // // v[0] == "a", v[1] == "c"
443
+ //
444
+ // // Stores results in a map. The map implementation assumes that the input
445
+ // // is provided as a series of key/value pairs. For example, the 0th element
446
+ // // resulting from the split will be stored as a key to the 1st element. If
447
+ // // an odd number of elements are resolved, the last element is paired with
448
+ // // a default-constructed value (e.g., empty string).
449
+ // std::map<std::string, std::string> m = absl::StrSplit("a,b,c", ',');
450
+ // // m["a"] == "b", m["c"] == "" // last component value equals ""
451
+ //
452
+ // Splitting to `std::pair` is an interesting case because it can hold only two
453
+ // elements and is not a collection type. When splitting to a `std::pair` the
454
+ // first two split strings become the `std::pair` `.first` and `.second`
455
+ // members, respectively. The remaining split substrings are discarded. If there
456
+ // are less than two split substrings, the empty string is used for the
457
+ // corresponding
458
+ // `std::pair` member.
459
+ //
460
+ // Example:
461
+ //
462
+ // // Stores first two split strings as the members in a std::pair.
463
+ // std::pair<std::string, std::string> p = absl::StrSplit("a,b,c", ',');
464
+ // // p.first == "a", p.second == "b" // "c" is omitted.
465
+ //
466
+ // The `StrSplit()` function can be used multiple times to perform more
467
+ // complicated splitting logic, such as intelligently parsing key-value pairs.
468
+ //
469
+ // Example:
470
+ //
471
+ // // The input string "a=b=c,d=e,f=,g" becomes
472
+ // // { "a" => "b=c", "d" => "e", "f" => "", "g" => "" }
473
+ // std::map<std::string, std::string> m;
474
+ // for (absl::string_view sp : absl::StrSplit("a=b=c,d=e,f=,g", ',')) {
475
+ // m.insert(absl::StrSplit(sp, absl::MaxSplits('=', 1)));
476
+ // }
477
+ // EXPECT_EQ("b=c", m.find("a")->second);
478
+ // EXPECT_EQ("e", m.find("d")->second);
479
+ // EXPECT_EQ("", m.find("f")->second);
480
+ // EXPECT_EQ("", m.find("g")->second);
481
+ //
482
+ // WARNING: Due to a legacy bug that is maintained for backward compatibility,
483
+ // splitting the following empty string_views produces different results:
484
+ //
485
+ // absl::StrSplit(absl::string_view(""), '-'); // {""}
486
+ // absl::StrSplit(absl::string_view(), '-'); // {}, but should be {""}
487
+ //
488
+ // Try not to depend on this distinction because the bug may one day be fixed.
489
+ template <typename Delimiter>
490
+ strings_internal::Splitter<
491
+ typename strings_internal::SelectDelimiter<Delimiter>::type, AllowEmpty>
492
+ StrSplit(strings_internal::ConvertibleToStringView text, Delimiter d) {
493
+ using DelimiterType =
494
+ typename strings_internal::SelectDelimiter<Delimiter>::type;
495
+ return strings_internal::Splitter<DelimiterType, AllowEmpty>(
496
+ std::move(text), DelimiterType(d), AllowEmpty());
497
+ }
498
+
499
+ template <typename Delimiter, typename Predicate>
500
+ strings_internal::Splitter<
501
+ typename strings_internal::SelectDelimiter<Delimiter>::type, Predicate>
502
+ StrSplit(strings_internal::ConvertibleToStringView text, Delimiter d,
503
+ Predicate p) {
504
+ using DelimiterType =
505
+ typename strings_internal::SelectDelimiter<Delimiter>::type;
506
+ return strings_internal::Splitter<DelimiterType, Predicate>(
507
+ std::move(text), DelimiterType(d), std::move(p));
508
+ }
509
+
510
+ ABSL_NAMESPACE_END
511
+ } // namespace absl
512
+
513
+ #endif // ABSL_STRINGS_STR_SPLIT_H_