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.
- checksums.yaml +4 -4
- data/Makefile +1654 -1519
- data/etc/roots.pem +44 -0
- data/include/grpc/grpc_security.h +37 -15
- data/include/grpc/grpc_security_constants.h +27 -0
- data/include/grpc/impl/codegen/grpc_types.h +14 -0
- data/include/grpc/impl/codegen/port_platform.h +1 -1
- data/src/core/ext/filters/client_channel/client_channel.cc +0 -20
- data/src/core/ext/filters/client_channel/http_proxy.cc +4 -4
- data/src/core/ext/filters/client_channel/lb_policy.cc +4 -3
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +191 -201
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.cc +89 -0
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h +40 -0
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +3 -2
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +88 -121
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +28 -57
- data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +0 -7
- data/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +8 -9
- data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +53 -34
- data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +18 -5
- data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +24 -19
- data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +2 -1
- data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +4 -2
- data/src/core/ext/filters/client_channel/server_address.cc +6 -9
- data/src/core/ext/filters/client_channel/server_address.h +3 -10
- data/src/core/ext/filters/client_channel/xds/xds_api.cc +394 -150
- data/src/core/ext/filters/client_channel/xds/xds_api.h +75 -35
- data/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc +59 -22
- data/src/core/ext/filters/client_channel/xds/xds_bootstrap.h +13 -9
- data/src/core/ext/filters/client_channel/xds/xds_channel_secure.cc +8 -6
- data/src/core/ext/filters/client_channel/xds/xds_client.cc +456 -175
- data/src/core/ext/filters/client_channel/xds/xds_client.h +33 -21
- data/src/core/ext/filters/client_channel/xds/xds_client_stats.cc +5 -8
- data/src/core/ext/filters/client_channel/xds/xds_client_stats.h +18 -24
- data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +2 -2
- data/src/core/ext/upb-generated/src/proto/grpc/lb/v1/load_balancer.upb.c +13 -5
- data/src/core/ext/upb-generated/src/proto/grpc/lb/v1/load_balancer.upb.h +34 -0
- data/src/core/lib/channel/channelz.h +11 -1
- data/src/core/lib/gpr/time_precise.cc +1 -1
- data/src/core/lib/gprpp/optional.h +26 -0
- data/src/core/lib/gprpp/string_view.h +14 -10
- data/src/core/lib/iomgr/executor.cc +1 -1
- data/src/core/lib/iomgr/fork_posix.cc +4 -0
- data/src/core/lib/iomgr/poller/eventmanager_libuv.cc +87 -0
- data/src/core/lib/iomgr/poller/eventmanager_libuv.h +88 -0
- data/src/core/lib/iomgr/socket_utils_common_posix.cc +14 -0
- data/src/core/lib/iomgr/socket_utils_posix.h +12 -0
- data/src/core/lib/iomgr/tcp_custom.h +3 -0
- data/src/core/lib/iomgr/tcp_posix.cc +607 -56
- data/src/core/lib/iomgr/tcp_server_custom.cc +15 -2
- data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +8 -0
- data/src/core/lib/json/json.h +11 -1
- data/src/core/lib/json/json_reader.cc +206 -28
- data/src/core/lib/json/json_writer.cc +111 -24
- data/src/core/lib/security/credentials/composite/composite_credentials.cc +7 -0
- data/src/core/lib/security/credentials/composite/composite_credentials.h +5 -1
- data/src/core/lib/security/credentials/credentials.h +10 -1
- data/src/core/lib/security/credentials/fake/fake_credentials.h +2 -1
- data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +1 -1
- data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +6 -4
- data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -1
- data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.cc +20 -0
- data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +8 -0
- data/src/core/lib/security/credentials/tls/{spiffe_credentials.cc → tls_credentials.cc} +23 -24
- data/src/core/lib/security/credentials/tls/{spiffe_credentials.h → tls_credentials.h} +9 -9
- data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +13 -0
- data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +22 -2
- data/src/core/lib/security/security_connector/load_system_roots_fallback.cc +2 -2
- data/src/core/lib/security/security_connector/load_system_roots_linux.cc +2 -2
- data/src/core/lib/security/security_connector/local/local_security_connector.cc +30 -3
- data/src/core/lib/security/security_connector/ssl_utils.cc +45 -3
- data/src/core/lib/security/security_connector/ssl_utils.h +12 -0
- data/src/core/lib/security/security_connector/tls/{spiffe_security_connector.cc → tls_security_connector.cc} +82 -69
- data/src/core/lib/security/security_connector/tls/{spiffe_security_connector.h → tls_security_connector.h} +17 -18
- data/src/core/lib/security/transport/client_auth_filter.cc +33 -0
- data/src/core/lib/surface/completion_queue.cc +22 -1
- data/src/core/lib/surface/version.cc +1 -1
- data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +11 -1
- data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.h +1 -1
- data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +3 -3
- data/src/core/tsi/fake_transport_security.cc +7 -3
- data/src/core/tsi/fake_transport_security.h +2 -0
- data/src/core/tsi/ssl_transport_security.cc +144 -8
- data/src/core/tsi/ssl_transport_security.h +15 -1
- data/src/core/tsi/transport_security.cc +13 -0
- data/src/core/tsi/transport_security_grpc.cc +2 -2
- data/src/core/tsi/transport_security_grpc.h +2 -2
- data/src/core/tsi/transport_security_interface.h +12 -0
- data/src/ruby/bin/math_pb.rb +5 -5
- data/src/ruby/ext/grpc/rb_call_credentials.c +4 -1
- data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +2 -0
- data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +4 -1
- data/src/ruby/lib/grpc/version.rb +1 -1
- data/src/ruby/pb/grpc/health/v1/health_pb.rb +3 -3
- data/src/ruby/pb/src/proto/grpc/testing/empty_pb.rb +1 -1
- data/src/ruby/pb/src/proto/grpc/testing/messages_pb.rb +23 -13
- data/third_party/abseil-cpp/absl/algorithm/algorithm.h +159 -0
- data/third_party/abseil-cpp/absl/base/attributes.h +609 -0
- data/third_party/abseil-cpp/absl/base/call_once.h +226 -0
- data/third_party/abseil-cpp/absl/base/casts.h +184 -0
- data/third_party/abseil-cpp/absl/base/config.h +622 -0
- data/third_party/abseil-cpp/absl/base/const_init.h +76 -0
- data/third_party/abseil-cpp/absl/base/dynamic_annotations.cc +129 -0
- data/third_party/abseil-cpp/absl/base/dynamic_annotations.h +389 -0
- data/third_party/abseil-cpp/absl/base/internal/atomic_hook.h +179 -0
- data/third_party/abseil-cpp/absl/base/internal/bits.h +218 -0
- data/third_party/abseil-cpp/absl/base/internal/cycleclock.cc +107 -0
- data/third_party/abseil-cpp/absl/base/internal/cycleclock.h +94 -0
- data/third_party/abseil-cpp/absl/base/internal/endian.h +266 -0
- data/third_party/abseil-cpp/absl/base/internal/hide_ptr.h +51 -0
- data/third_party/abseil-cpp/absl/base/internal/identity.h +37 -0
- data/third_party/abseil-cpp/absl/base/internal/inline_variable.h +107 -0
- data/third_party/abseil-cpp/absl/base/internal/invoke.h +187 -0
- data/third_party/abseil-cpp/absl/base/internal/low_level_scheduling.h +107 -0
- data/third_party/abseil-cpp/absl/base/internal/per_thread_tls.h +52 -0
- data/third_party/abseil-cpp/absl/base/internal/raw_logging.cc +237 -0
- data/third_party/abseil-cpp/absl/base/internal/raw_logging.h +179 -0
- data/third_party/abseil-cpp/absl/base/internal/scheduling_mode.h +58 -0
- data/third_party/abseil-cpp/absl/base/internal/spinlock.cc +233 -0
- data/third_party/abseil-cpp/absl/base/internal/spinlock.h +243 -0
- data/third_party/abseil-cpp/absl/base/internal/spinlock_akaros.inc +35 -0
- data/third_party/abseil-cpp/absl/base/internal/spinlock_linux.inc +67 -0
- data/third_party/abseil-cpp/absl/base/internal/spinlock_posix.inc +46 -0
- data/third_party/abseil-cpp/absl/base/internal/spinlock_wait.cc +81 -0
- data/third_party/abseil-cpp/absl/base/internal/spinlock_wait.h +93 -0
- data/third_party/abseil-cpp/absl/base/internal/spinlock_win32.inc +37 -0
- data/third_party/abseil-cpp/absl/base/internal/sysinfo.cc +414 -0
- data/third_party/abseil-cpp/absl/base/internal/sysinfo.h +66 -0
- data/third_party/abseil-cpp/absl/base/internal/thread_annotations.h +271 -0
- data/third_party/abseil-cpp/absl/base/internal/thread_identity.cc +140 -0
- data/third_party/abseil-cpp/absl/base/internal/thread_identity.h +250 -0
- data/third_party/abseil-cpp/absl/base/internal/throw_delegate.cc +108 -0
- data/third_party/abseil-cpp/absl/base/internal/throw_delegate.h +75 -0
- data/third_party/abseil-cpp/absl/base/internal/tsan_mutex_interface.h +66 -0
- data/third_party/abseil-cpp/absl/base/internal/unaligned_access.h +158 -0
- data/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.cc +103 -0
- data/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.h +124 -0
- data/third_party/abseil-cpp/absl/base/log_severity.cc +27 -0
- data/third_party/abseil-cpp/absl/base/log_severity.h +121 -0
- data/third_party/abseil-cpp/absl/base/macros.h +220 -0
- data/third_party/abseil-cpp/absl/base/optimization.h +181 -0
- data/third_party/abseil-cpp/absl/base/options.h +214 -0
- data/third_party/abseil-cpp/absl/base/policy_checks.h +111 -0
- data/third_party/abseil-cpp/absl/base/port.h +26 -0
- data/third_party/abseil-cpp/absl/base/thread_annotations.h +280 -0
- data/third_party/abseil-cpp/absl/container/inlined_vector.h +848 -0
- data/third_party/abseil-cpp/absl/container/internal/compressed_tuple.h +265 -0
- data/third_party/abseil-cpp/absl/container/internal/inlined_vector.h +892 -0
- data/third_party/abseil-cpp/absl/memory/memory.h +695 -0
- data/third_party/abseil-cpp/absl/meta/type_traits.h +759 -0
- data/third_party/abseil-cpp/absl/numeric/int128.cc +404 -0
- data/third_party/abseil-cpp/absl/numeric/int128.h +1091 -0
- data/third_party/abseil-cpp/absl/numeric/int128_have_intrinsic.inc +302 -0
- data/third_party/abseil-cpp/absl/numeric/int128_no_intrinsic.inc +308 -0
- data/third_party/abseil-cpp/absl/strings/ascii.cc +200 -0
- data/third_party/abseil-cpp/absl/strings/ascii.h +241 -0
- data/third_party/abseil-cpp/absl/strings/charconv.cc +985 -0
- data/third_party/abseil-cpp/absl/strings/charconv.h +119 -0
- data/third_party/abseil-cpp/absl/strings/escaping.cc +949 -0
- data/third_party/abseil-cpp/absl/strings/escaping.h +164 -0
- data/third_party/abseil-cpp/absl/strings/internal/char_map.h +156 -0
- data/third_party/abseil-cpp/absl/strings/internal/charconv_bigint.cc +359 -0
- data/third_party/abseil-cpp/absl/strings/internal/charconv_bigint.h +421 -0
- data/third_party/abseil-cpp/absl/strings/internal/charconv_parse.cc +504 -0
- data/third_party/abseil-cpp/absl/strings/internal/charconv_parse.h +99 -0
- data/third_party/abseil-cpp/absl/strings/internal/escaping.cc +180 -0
- data/third_party/abseil-cpp/absl/strings/internal/escaping.h +58 -0
- data/third_party/abseil-cpp/absl/strings/internal/memutil.cc +112 -0
- data/third_party/abseil-cpp/absl/strings/internal/memutil.h +148 -0
- data/third_party/abseil-cpp/absl/strings/internal/ostringstream.cc +36 -0
- data/third_party/abseil-cpp/absl/strings/internal/ostringstream.h +89 -0
- data/third_party/abseil-cpp/absl/strings/internal/resize_uninitialized.h +73 -0
- data/third_party/abseil-cpp/absl/strings/internal/stl_type_traits.h +248 -0
- data/third_party/abseil-cpp/absl/strings/internal/str_join_internal.h +314 -0
- data/third_party/abseil-cpp/absl/strings/internal/str_split_internal.h +455 -0
- data/third_party/abseil-cpp/absl/strings/internal/utf8.cc +53 -0
- data/third_party/abseil-cpp/absl/strings/internal/utf8.h +50 -0
- data/third_party/abseil-cpp/absl/strings/match.cc +40 -0
- data/third_party/abseil-cpp/absl/strings/match.h +90 -0
- data/third_party/abseil-cpp/absl/strings/numbers.cc +916 -0
- data/third_party/abseil-cpp/absl/strings/numbers.h +263 -0
- data/third_party/abseil-cpp/absl/strings/str_cat.cc +246 -0
- data/third_party/abseil-cpp/absl/strings/str_cat.h +408 -0
- data/third_party/abseil-cpp/absl/strings/str_join.h +293 -0
- data/third_party/abseil-cpp/absl/strings/str_replace.cc +82 -0
- data/third_party/abseil-cpp/absl/strings/str_replace.h +219 -0
- data/third_party/abseil-cpp/absl/strings/str_split.cc +139 -0
- data/third_party/abseil-cpp/absl/strings/str_split.h +513 -0
- data/third_party/abseil-cpp/absl/strings/string_view.cc +235 -0
- data/third_party/abseil-cpp/absl/strings/string_view.h +615 -0
- data/third_party/abseil-cpp/absl/strings/strip.h +91 -0
- data/third_party/abseil-cpp/absl/strings/substitute.cc +171 -0
- data/third_party/abseil-cpp/absl/strings/substitute.h +693 -0
- data/third_party/abseil-cpp/absl/types/bad_optional_access.cc +48 -0
- data/third_party/abseil-cpp/absl/types/bad_optional_access.h +78 -0
- data/third_party/abseil-cpp/absl/types/internal/optional.h +396 -0
- data/third_party/abseil-cpp/absl/types/internal/span.h +128 -0
- data/third_party/abseil-cpp/absl/types/optional.h +776 -0
- data/third_party/abseil-cpp/absl/types/span.h +713 -0
- data/third_party/abseil-cpp/absl/utility/utility.h +350 -0
- data/third_party/upb/upb/decode.c +4 -0
- data/third_party/upb/upb/port.c +0 -1
- data/third_party/upb/upb/port_def.inc +1 -3
- data/third_party/upb/upb/table.c +2 -1
- metadata +147 -43
- data/src/core/lib/json/json_common.h +0 -34
- data/src/core/lib/json/json_reader.h +0 -146
- data/src/core/lib/json/json_string.cc +0 -367
- data/src/core/lib/json/json_writer.h +0 -84
@@ -0,0 +1,314 @@
|
|
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
|
+
// This file declares INTERNAL parts of the Join API that are inlined/templated
|
18
|
+
// or otherwise need to be available at compile time. The main abstractions
|
19
|
+
// defined in this file are:
|
20
|
+
//
|
21
|
+
// - A handful of default Formatters
|
22
|
+
// - JoinAlgorithm() overloads
|
23
|
+
// - JoinRange() overloads
|
24
|
+
// - JoinTuple()
|
25
|
+
//
|
26
|
+
// DO NOT INCLUDE THIS FILE DIRECTLY. Use this file by including
|
27
|
+
// absl/strings/str_join.h
|
28
|
+
//
|
29
|
+
// IWYU pragma: private, include "absl/strings/str_join.h"
|
30
|
+
|
31
|
+
#ifndef ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_
|
32
|
+
#define ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_
|
33
|
+
|
34
|
+
#include <cstring>
|
35
|
+
#include <iterator>
|
36
|
+
#include <memory>
|
37
|
+
#include <string>
|
38
|
+
#include <type_traits>
|
39
|
+
#include <utility>
|
40
|
+
|
41
|
+
#include "absl/strings/internal/ostringstream.h"
|
42
|
+
#include "absl/strings/internal/resize_uninitialized.h"
|
43
|
+
#include "absl/strings/str_cat.h"
|
44
|
+
|
45
|
+
namespace absl {
|
46
|
+
ABSL_NAMESPACE_BEGIN
|
47
|
+
namespace strings_internal {
|
48
|
+
|
49
|
+
//
|
50
|
+
// Formatter objects
|
51
|
+
//
|
52
|
+
// The following are implementation classes for standard Formatter objects. The
|
53
|
+
// factory functions that users will call to create and use these formatters are
|
54
|
+
// defined and documented in strings/join.h.
|
55
|
+
//
|
56
|
+
|
57
|
+
// The default formatter. Converts alpha-numeric types to strings.
|
58
|
+
struct AlphaNumFormatterImpl {
|
59
|
+
// This template is needed in order to support passing in a dereferenced
|
60
|
+
// vector<bool>::iterator
|
61
|
+
template <typename T>
|
62
|
+
void operator()(std::string* out, const T& t) const {
|
63
|
+
StrAppend(out, AlphaNum(t));
|
64
|
+
}
|
65
|
+
|
66
|
+
void operator()(std::string* out, const AlphaNum& t) const {
|
67
|
+
StrAppend(out, t);
|
68
|
+
}
|
69
|
+
};
|
70
|
+
|
71
|
+
// A type that's used to overload the JoinAlgorithm() function (defined below)
|
72
|
+
// for ranges that do not require additional formatting (e.g., a range of
|
73
|
+
// strings).
|
74
|
+
|
75
|
+
struct NoFormatter : public AlphaNumFormatterImpl {};
|
76
|
+
|
77
|
+
// Formats types to strings using the << operator.
|
78
|
+
class StreamFormatterImpl {
|
79
|
+
public:
|
80
|
+
// The method isn't const because it mutates state. Making it const will
|
81
|
+
// render StreamFormatterImpl thread-hostile.
|
82
|
+
template <typename T>
|
83
|
+
void operator()(std::string* out, const T& t) {
|
84
|
+
// The stream is created lazily to avoid paying the relatively high cost
|
85
|
+
// of its construction when joining an empty range.
|
86
|
+
if (strm_) {
|
87
|
+
strm_->clear(); // clear the bad, fail and eof bits in case they were set
|
88
|
+
strm_->str(out);
|
89
|
+
} else {
|
90
|
+
strm_.reset(new strings_internal::OStringStream(out));
|
91
|
+
}
|
92
|
+
*strm_ << t;
|
93
|
+
}
|
94
|
+
|
95
|
+
private:
|
96
|
+
std::unique_ptr<strings_internal::OStringStream> strm_;
|
97
|
+
};
|
98
|
+
|
99
|
+
// Formats a std::pair<>. The 'first' member is formatted using f1_ and the
|
100
|
+
// 'second' member is formatted using f2_. sep_ is the separator.
|
101
|
+
template <typename F1, typename F2>
|
102
|
+
class PairFormatterImpl {
|
103
|
+
public:
|
104
|
+
PairFormatterImpl(F1 f1, absl::string_view sep, F2 f2)
|
105
|
+
: f1_(std::move(f1)), sep_(sep), f2_(std::move(f2)) {}
|
106
|
+
|
107
|
+
template <typename T>
|
108
|
+
void operator()(std::string* out, const T& p) {
|
109
|
+
f1_(out, p.first);
|
110
|
+
out->append(sep_);
|
111
|
+
f2_(out, p.second);
|
112
|
+
}
|
113
|
+
|
114
|
+
template <typename T>
|
115
|
+
void operator()(std::string* out, const T& p) const {
|
116
|
+
f1_(out, p.first);
|
117
|
+
out->append(sep_);
|
118
|
+
f2_(out, p.second);
|
119
|
+
}
|
120
|
+
|
121
|
+
private:
|
122
|
+
F1 f1_;
|
123
|
+
std::string sep_;
|
124
|
+
F2 f2_;
|
125
|
+
};
|
126
|
+
|
127
|
+
// Wraps another formatter and dereferences the argument to operator() then
|
128
|
+
// passes the dereferenced argument to the wrapped formatter. This can be
|
129
|
+
// useful, for example, to join a std::vector<int*>.
|
130
|
+
template <typename Formatter>
|
131
|
+
class DereferenceFormatterImpl {
|
132
|
+
public:
|
133
|
+
DereferenceFormatterImpl() : f_() {}
|
134
|
+
explicit DereferenceFormatterImpl(Formatter&& f)
|
135
|
+
: f_(std::forward<Formatter>(f)) {}
|
136
|
+
|
137
|
+
template <typename T>
|
138
|
+
void operator()(std::string* out, const T& t) {
|
139
|
+
f_(out, *t);
|
140
|
+
}
|
141
|
+
|
142
|
+
template <typename T>
|
143
|
+
void operator()(std::string* out, const T& t) const {
|
144
|
+
f_(out, *t);
|
145
|
+
}
|
146
|
+
|
147
|
+
private:
|
148
|
+
Formatter f_;
|
149
|
+
};
|
150
|
+
|
151
|
+
// DefaultFormatter<T> is a traits class that selects a default Formatter to use
|
152
|
+
// for the given type T. The ::Type member names the Formatter to use. This is
|
153
|
+
// used by the strings::Join() functions that do NOT take a Formatter argument,
|
154
|
+
// in which case a default Formatter must be chosen.
|
155
|
+
//
|
156
|
+
// AlphaNumFormatterImpl is the default in the base template, followed by
|
157
|
+
// specializations for other types.
|
158
|
+
template <typename ValueType>
|
159
|
+
struct DefaultFormatter {
|
160
|
+
typedef AlphaNumFormatterImpl Type;
|
161
|
+
};
|
162
|
+
template <>
|
163
|
+
struct DefaultFormatter<const char*> {
|
164
|
+
typedef AlphaNumFormatterImpl Type;
|
165
|
+
};
|
166
|
+
template <>
|
167
|
+
struct DefaultFormatter<char*> {
|
168
|
+
typedef AlphaNumFormatterImpl Type;
|
169
|
+
};
|
170
|
+
template <>
|
171
|
+
struct DefaultFormatter<std::string> {
|
172
|
+
typedef NoFormatter Type;
|
173
|
+
};
|
174
|
+
template <>
|
175
|
+
struct DefaultFormatter<absl::string_view> {
|
176
|
+
typedef NoFormatter Type;
|
177
|
+
};
|
178
|
+
template <typename ValueType>
|
179
|
+
struct DefaultFormatter<ValueType*> {
|
180
|
+
typedef DereferenceFormatterImpl<typename DefaultFormatter<ValueType>::Type>
|
181
|
+
Type;
|
182
|
+
};
|
183
|
+
|
184
|
+
template <typename ValueType>
|
185
|
+
struct DefaultFormatter<std::unique_ptr<ValueType>>
|
186
|
+
: public DefaultFormatter<ValueType*> {};
|
187
|
+
|
188
|
+
//
|
189
|
+
// JoinAlgorithm() functions
|
190
|
+
//
|
191
|
+
|
192
|
+
// The main joining algorithm. This simply joins the elements in the given
|
193
|
+
// iterator range, each separated by the given separator, into an output string,
|
194
|
+
// and formats each element using the provided Formatter object.
|
195
|
+
template <typename Iterator, typename Formatter>
|
196
|
+
std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s,
|
197
|
+
Formatter&& f) {
|
198
|
+
std::string result;
|
199
|
+
absl::string_view sep("");
|
200
|
+
for (Iterator it = start; it != end; ++it) {
|
201
|
+
result.append(sep.data(), sep.size());
|
202
|
+
f(&result, *it);
|
203
|
+
sep = s;
|
204
|
+
}
|
205
|
+
return result;
|
206
|
+
}
|
207
|
+
|
208
|
+
// A joining algorithm that's optimized for a forward iterator range of
|
209
|
+
// string-like objects that do not need any additional formatting. This is to
|
210
|
+
// optimize the common case of joining, say, a std::vector<string> or a
|
211
|
+
// std::vector<absl::string_view>.
|
212
|
+
//
|
213
|
+
// This is an overload of the previous JoinAlgorithm() function. Here the
|
214
|
+
// Formatter argument is of type NoFormatter. Since NoFormatter is an internal
|
215
|
+
// type, this overload is only invoked when strings::Join() is called with a
|
216
|
+
// range of string-like objects (e.g., std::string, absl::string_view), and an
|
217
|
+
// explicit Formatter argument was NOT specified.
|
218
|
+
//
|
219
|
+
// The optimization is that the needed space will be reserved in the output
|
220
|
+
// string to avoid the need to resize while appending. To do this, the iterator
|
221
|
+
// range will be traversed twice: once to calculate the total needed size, and
|
222
|
+
// then again to copy the elements and delimiters to the output string.
|
223
|
+
template <typename Iterator,
|
224
|
+
typename = typename std::enable_if<std::is_convertible<
|
225
|
+
typename std::iterator_traits<Iterator>::iterator_category,
|
226
|
+
std::forward_iterator_tag>::value>::type>
|
227
|
+
std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s,
|
228
|
+
NoFormatter) {
|
229
|
+
std::string result;
|
230
|
+
if (start != end) {
|
231
|
+
// Sums size
|
232
|
+
size_t result_size = start->size();
|
233
|
+
for (Iterator it = start; ++it != end;) {
|
234
|
+
result_size += s.size();
|
235
|
+
result_size += it->size();
|
236
|
+
}
|
237
|
+
|
238
|
+
if (result_size > 0) {
|
239
|
+
STLStringResizeUninitialized(&result, result_size);
|
240
|
+
|
241
|
+
// Joins strings
|
242
|
+
char* result_buf = &*result.begin();
|
243
|
+
memcpy(result_buf, start->data(), start->size());
|
244
|
+
result_buf += start->size();
|
245
|
+
for (Iterator it = start; ++it != end;) {
|
246
|
+
memcpy(result_buf, s.data(), s.size());
|
247
|
+
result_buf += s.size();
|
248
|
+
memcpy(result_buf, it->data(), it->size());
|
249
|
+
result_buf += it->size();
|
250
|
+
}
|
251
|
+
}
|
252
|
+
}
|
253
|
+
|
254
|
+
return result;
|
255
|
+
}
|
256
|
+
|
257
|
+
// JoinTupleLoop implements a loop over the elements of a std::tuple, which
|
258
|
+
// are heterogeneous. The primary template matches the tuple interior case. It
|
259
|
+
// continues the iteration after appending a separator (for nonzero indices)
|
260
|
+
// and formatting an element of the tuple. The specialization for the I=N case
|
261
|
+
// matches the end-of-tuple, and terminates the iteration.
|
262
|
+
template <size_t I, size_t N>
|
263
|
+
struct JoinTupleLoop {
|
264
|
+
template <typename Tup, typename Formatter>
|
265
|
+
void operator()(std::string* out, const Tup& tup, absl::string_view sep,
|
266
|
+
Formatter&& fmt) {
|
267
|
+
if (I > 0) out->append(sep.data(), sep.size());
|
268
|
+
fmt(out, std::get<I>(tup));
|
269
|
+
JoinTupleLoop<I + 1, N>()(out, tup, sep, fmt);
|
270
|
+
}
|
271
|
+
};
|
272
|
+
template <size_t N>
|
273
|
+
struct JoinTupleLoop<N, N> {
|
274
|
+
template <typename Tup, typename Formatter>
|
275
|
+
void operator()(std::string*, const Tup&, absl::string_view, Formatter&&) {}
|
276
|
+
};
|
277
|
+
|
278
|
+
template <typename... T, typename Formatter>
|
279
|
+
std::string JoinAlgorithm(const std::tuple<T...>& tup, absl::string_view sep,
|
280
|
+
Formatter&& fmt) {
|
281
|
+
std::string result;
|
282
|
+
JoinTupleLoop<0, sizeof...(T)>()(&result, tup, sep, fmt);
|
283
|
+
return result;
|
284
|
+
}
|
285
|
+
|
286
|
+
template <typename Iterator>
|
287
|
+
std::string JoinRange(Iterator first, Iterator last,
|
288
|
+
absl::string_view separator) {
|
289
|
+
// No formatter was explicitly given, so a default must be chosen.
|
290
|
+
typedef typename std::iterator_traits<Iterator>::value_type ValueType;
|
291
|
+
typedef typename DefaultFormatter<ValueType>::Type Formatter;
|
292
|
+
return JoinAlgorithm(first, last, separator, Formatter());
|
293
|
+
}
|
294
|
+
|
295
|
+
template <typename Range, typename Formatter>
|
296
|
+
std::string JoinRange(const Range& range, absl::string_view separator,
|
297
|
+
Formatter&& fmt) {
|
298
|
+
using std::begin;
|
299
|
+
using std::end;
|
300
|
+
return JoinAlgorithm(begin(range), end(range), separator, fmt);
|
301
|
+
}
|
302
|
+
|
303
|
+
template <typename Range>
|
304
|
+
std::string JoinRange(const Range& range, absl::string_view separator) {
|
305
|
+
using std::begin;
|
306
|
+
using std::end;
|
307
|
+
return JoinRange(begin(range), end(range), separator);
|
308
|
+
}
|
309
|
+
|
310
|
+
} // namespace strings_internal
|
311
|
+
ABSL_NAMESPACE_END
|
312
|
+
} // namespace absl
|
313
|
+
|
314
|
+
#endif // ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_
|
@@ -0,0 +1,455 @@
|
|
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
|
+
|
16
|
+
// This file declares INTERNAL parts of the Split API that are inline/templated
|
17
|
+
// or otherwise need to be available at compile time. The main abstractions
|
18
|
+
// defined in here are
|
19
|
+
//
|
20
|
+
// - ConvertibleToStringView
|
21
|
+
// - SplitIterator<>
|
22
|
+
// - Splitter<>
|
23
|
+
//
|
24
|
+
// DO NOT INCLUDE THIS FILE DIRECTLY. Use this file by including
|
25
|
+
// absl/strings/str_split.h.
|
26
|
+
//
|
27
|
+
// IWYU pragma: private, include "absl/strings/str_split.h"
|
28
|
+
|
29
|
+
#ifndef ABSL_STRINGS_INTERNAL_STR_SPLIT_INTERNAL_H_
|
30
|
+
#define ABSL_STRINGS_INTERNAL_STR_SPLIT_INTERNAL_H_
|
31
|
+
|
32
|
+
#include <array>
|
33
|
+
#include <initializer_list>
|
34
|
+
#include <iterator>
|
35
|
+
#include <map>
|
36
|
+
#include <type_traits>
|
37
|
+
#include <utility>
|
38
|
+
#include <vector>
|
39
|
+
|
40
|
+
#include "absl/base/macros.h"
|
41
|
+
#include "absl/base/port.h"
|
42
|
+
#include "absl/meta/type_traits.h"
|
43
|
+
#include "absl/strings/string_view.h"
|
44
|
+
|
45
|
+
#ifdef _GLIBCXX_DEBUG
|
46
|
+
#include "absl/strings/internal/stl_type_traits.h"
|
47
|
+
#endif // _GLIBCXX_DEBUG
|
48
|
+
|
49
|
+
namespace absl {
|
50
|
+
ABSL_NAMESPACE_BEGIN
|
51
|
+
namespace strings_internal {
|
52
|
+
|
53
|
+
// This class is implicitly constructible from everything that absl::string_view
|
54
|
+
// is implicitly constructible from. If it's constructed from a temporary
|
55
|
+
// string, the data is moved into a data member so its lifetime matches that of
|
56
|
+
// the ConvertibleToStringView instance.
|
57
|
+
class ConvertibleToStringView {
|
58
|
+
public:
|
59
|
+
ConvertibleToStringView(const char* s) // NOLINT(runtime/explicit)
|
60
|
+
: value_(s) {}
|
61
|
+
ConvertibleToStringView(char* s) : value_(s) {} // NOLINT(runtime/explicit)
|
62
|
+
ConvertibleToStringView(absl::string_view s) // NOLINT(runtime/explicit)
|
63
|
+
: value_(s) {}
|
64
|
+
ConvertibleToStringView(const std::string& s) // NOLINT(runtime/explicit)
|
65
|
+
: value_(s) {}
|
66
|
+
|
67
|
+
// Matches rvalue strings and moves their data to a member.
|
68
|
+
ConvertibleToStringView(std::string&& s) // NOLINT(runtime/explicit)
|
69
|
+
: copy_(std::move(s)), value_(copy_) {}
|
70
|
+
|
71
|
+
ConvertibleToStringView(const ConvertibleToStringView& other)
|
72
|
+
: copy_(other.copy_),
|
73
|
+
value_(other.IsSelfReferential() ? copy_ : other.value_) {}
|
74
|
+
|
75
|
+
ConvertibleToStringView(ConvertibleToStringView&& other) {
|
76
|
+
StealMembers(std::move(other));
|
77
|
+
}
|
78
|
+
|
79
|
+
ConvertibleToStringView& operator=(ConvertibleToStringView other) {
|
80
|
+
StealMembers(std::move(other));
|
81
|
+
return *this;
|
82
|
+
}
|
83
|
+
|
84
|
+
absl::string_view value() const { return value_; }
|
85
|
+
|
86
|
+
private:
|
87
|
+
// Returns true if ctsp's value refers to its internal copy_ member.
|
88
|
+
bool IsSelfReferential() const { return value_.data() == copy_.data(); }
|
89
|
+
|
90
|
+
void StealMembers(ConvertibleToStringView&& other) {
|
91
|
+
if (other.IsSelfReferential()) {
|
92
|
+
copy_ = std::move(other.copy_);
|
93
|
+
value_ = copy_;
|
94
|
+
other.value_ = other.copy_;
|
95
|
+
} else {
|
96
|
+
value_ = other.value_;
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
// Holds the data moved from temporary std::string arguments. Declared first
|
101
|
+
// so that 'value' can refer to 'copy_'.
|
102
|
+
std::string copy_;
|
103
|
+
absl::string_view value_;
|
104
|
+
};
|
105
|
+
|
106
|
+
// An iterator that enumerates the parts of a string from a Splitter. The text
|
107
|
+
// to be split, the Delimiter, and the Predicate are all taken from the given
|
108
|
+
// Splitter object. Iterators may only be compared if they refer to the same
|
109
|
+
// Splitter instance.
|
110
|
+
//
|
111
|
+
// This class is NOT part of the public splitting API.
|
112
|
+
template <typename Splitter>
|
113
|
+
class SplitIterator {
|
114
|
+
public:
|
115
|
+
using iterator_category = std::input_iterator_tag;
|
116
|
+
using value_type = absl::string_view;
|
117
|
+
using difference_type = ptrdiff_t;
|
118
|
+
using pointer = const value_type*;
|
119
|
+
using reference = const value_type&;
|
120
|
+
|
121
|
+
enum State { kInitState, kLastState, kEndState };
|
122
|
+
SplitIterator(State state, const Splitter* splitter)
|
123
|
+
: pos_(0),
|
124
|
+
state_(state),
|
125
|
+
splitter_(splitter),
|
126
|
+
delimiter_(splitter->delimiter()),
|
127
|
+
predicate_(splitter->predicate()) {
|
128
|
+
// Hack to maintain backward compatibility. This one block makes it so an
|
129
|
+
// empty absl::string_view whose .data() happens to be nullptr behaves
|
130
|
+
// *differently* from an otherwise empty absl::string_view whose .data() is
|
131
|
+
// not nullptr. This is an undesirable difference in general, but this
|
132
|
+
// behavior is maintained to avoid breaking existing code that happens to
|
133
|
+
// depend on this old behavior/bug. Perhaps it will be fixed one day. The
|
134
|
+
// difference in behavior is as follows:
|
135
|
+
// Split(absl::string_view(""), '-'); // {""}
|
136
|
+
// Split(absl::string_view(), '-'); // {}
|
137
|
+
if (splitter_->text().data() == nullptr) {
|
138
|
+
state_ = kEndState;
|
139
|
+
pos_ = splitter_->text().size();
|
140
|
+
return;
|
141
|
+
}
|
142
|
+
|
143
|
+
if (state_ == kEndState) {
|
144
|
+
pos_ = splitter_->text().size();
|
145
|
+
} else {
|
146
|
+
++(*this);
|
147
|
+
}
|
148
|
+
}
|
149
|
+
|
150
|
+
bool at_end() const { return state_ == kEndState; }
|
151
|
+
|
152
|
+
reference operator*() const { return curr_; }
|
153
|
+
pointer operator->() const { return &curr_; }
|
154
|
+
|
155
|
+
SplitIterator& operator++() {
|
156
|
+
do {
|
157
|
+
if (state_ == kLastState) {
|
158
|
+
state_ = kEndState;
|
159
|
+
return *this;
|
160
|
+
}
|
161
|
+
const absl::string_view text = splitter_->text();
|
162
|
+
const absl::string_view d = delimiter_.Find(text, pos_);
|
163
|
+
if (d.data() == text.data() + text.size()) state_ = kLastState;
|
164
|
+
curr_ = text.substr(pos_, d.data() - (text.data() + pos_));
|
165
|
+
pos_ += curr_.size() + d.size();
|
166
|
+
} while (!predicate_(curr_));
|
167
|
+
return *this;
|
168
|
+
}
|
169
|
+
|
170
|
+
SplitIterator operator++(int) {
|
171
|
+
SplitIterator old(*this);
|
172
|
+
++(*this);
|
173
|
+
return old;
|
174
|
+
}
|
175
|
+
|
176
|
+
friend bool operator==(const SplitIterator& a, const SplitIterator& b) {
|
177
|
+
return a.state_ == b.state_ && a.pos_ == b.pos_;
|
178
|
+
}
|
179
|
+
|
180
|
+
friend bool operator!=(const SplitIterator& a, const SplitIterator& b) {
|
181
|
+
return !(a == b);
|
182
|
+
}
|
183
|
+
|
184
|
+
private:
|
185
|
+
size_t pos_;
|
186
|
+
State state_;
|
187
|
+
absl::string_view curr_;
|
188
|
+
const Splitter* splitter_;
|
189
|
+
typename Splitter::DelimiterType delimiter_;
|
190
|
+
typename Splitter::PredicateType predicate_;
|
191
|
+
};
|
192
|
+
|
193
|
+
// HasMappedType<T>::value is true iff there exists a type T::mapped_type.
|
194
|
+
template <typename T, typename = void>
|
195
|
+
struct HasMappedType : std::false_type {};
|
196
|
+
template <typename T>
|
197
|
+
struct HasMappedType<T, absl::void_t<typename T::mapped_type>>
|
198
|
+
: std::true_type {};
|
199
|
+
|
200
|
+
// HasValueType<T>::value is true iff there exists a type T::value_type.
|
201
|
+
template <typename T, typename = void>
|
202
|
+
struct HasValueType : std::false_type {};
|
203
|
+
template <typename T>
|
204
|
+
struct HasValueType<T, absl::void_t<typename T::value_type>> : std::true_type {
|
205
|
+
};
|
206
|
+
|
207
|
+
// HasConstIterator<T>::value is true iff there exists a type T::const_iterator.
|
208
|
+
template <typename T, typename = void>
|
209
|
+
struct HasConstIterator : std::false_type {};
|
210
|
+
template <typename T>
|
211
|
+
struct HasConstIterator<T, absl::void_t<typename T::const_iterator>>
|
212
|
+
: std::true_type {};
|
213
|
+
|
214
|
+
// IsInitializerList<T>::value is true iff T is an std::initializer_list. More
|
215
|
+
// details below in Splitter<> where this is used.
|
216
|
+
std::false_type IsInitializerListDispatch(...); // default: No
|
217
|
+
template <typename T>
|
218
|
+
std::true_type IsInitializerListDispatch(std::initializer_list<T>*);
|
219
|
+
template <typename T>
|
220
|
+
struct IsInitializerList
|
221
|
+
: decltype(IsInitializerListDispatch(static_cast<T*>(nullptr))) {};
|
222
|
+
|
223
|
+
// A SplitterIsConvertibleTo<C>::type alias exists iff the specified condition
|
224
|
+
// is true for type 'C'.
|
225
|
+
//
|
226
|
+
// Restricts conversion to container-like types (by testing for the presence of
|
227
|
+
// a const_iterator member type) and also to disable conversion to an
|
228
|
+
// std::initializer_list (which also has a const_iterator). Otherwise, code
|
229
|
+
// compiled in C++11 will get an error due to ambiguous conversion paths (in
|
230
|
+
// C++11 std::vector<T>::operator= is overloaded to take either a std::vector<T>
|
231
|
+
// or an std::initializer_list<T>).
|
232
|
+
|
233
|
+
template <typename C, bool has_value_type, bool has_mapped_type>
|
234
|
+
struct SplitterIsConvertibleToImpl : std::false_type {};
|
235
|
+
|
236
|
+
template <typename C>
|
237
|
+
struct SplitterIsConvertibleToImpl<C, true, false>
|
238
|
+
: std::is_constructible<typename C::value_type, absl::string_view> {};
|
239
|
+
|
240
|
+
template <typename C>
|
241
|
+
struct SplitterIsConvertibleToImpl<C, true, true>
|
242
|
+
: absl::conjunction<
|
243
|
+
std::is_constructible<typename C::key_type, absl::string_view>,
|
244
|
+
std::is_constructible<typename C::mapped_type, absl::string_view>> {};
|
245
|
+
|
246
|
+
template <typename C>
|
247
|
+
struct SplitterIsConvertibleTo
|
248
|
+
: SplitterIsConvertibleToImpl<
|
249
|
+
C,
|
250
|
+
#ifdef _GLIBCXX_DEBUG
|
251
|
+
!IsStrictlyBaseOfAndConvertibleToSTLContainer<C>::value &&
|
252
|
+
#endif // _GLIBCXX_DEBUG
|
253
|
+
!IsInitializerList<
|
254
|
+
typename std::remove_reference<C>::type>::value &&
|
255
|
+
HasValueType<C>::value && HasConstIterator<C>::value,
|
256
|
+
HasMappedType<C>::value> {
|
257
|
+
};
|
258
|
+
|
259
|
+
// This class implements the range that is returned by absl::StrSplit(). This
|
260
|
+
// class has templated conversion operators that allow it to be implicitly
|
261
|
+
// converted to a variety of types that the caller may have specified on the
|
262
|
+
// left-hand side of an assignment.
|
263
|
+
//
|
264
|
+
// The main interface for interacting with this class is through its implicit
|
265
|
+
// conversion operators. However, this class may also be used like a container
|
266
|
+
// in that it has .begin() and .end() member functions. It may also be used
|
267
|
+
// within a range-for loop.
|
268
|
+
//
|
269
|
+
// Output containers can be collections of any type that is constructible from
|
270
|
+
// an absl::string_view.
|
271
|
+
//
|
272
|
+
// An Predicate functor may be supplied. This predicate will be used to filter
|
273
|
+
// the split strings: only strings for which the predicate returns true will be
|
274
|
+
// kept. A Predicate object is any unary functor that takes an absl::string_view
|
275
|
+
// and returns bool.
|
276
|
+
template <typename Delimiter, typename Predicate>
|
277
|
+
class Splitter {
|
278
|
+
public:
|
279
|
+
using DelimiterType = Delimiter;
|
280
|
+
using PredicateType = Predicate;
|
281
|
+
using const_iterator = strings_internal::SplitIterator<Splitter>;
|
282
|
+
using value_type = typename std::iterator_traits<const_iterator>::value_type;
|
283
|
+
|
284
|
+
Splitter(ConvertibleToStringView input_text, Delimiter d, Predicate p)
|
285
|
+
: text_(std::move(input_text)),
|
286
|
+
delimiter_(std::move(d)),
|
287
|
+
predicate_(std::move(p)) {}
|
288
|
+
|
289
|
+
absl::string_view text() const { return text_.value(); }
|
290
|
+
const Delimiter& delimiter() const { return delimiter_; }
|
291
|
+
const Predicate& predicate() const { return predicate_; }
|
292
|
+
|
293
|
+
// Range functions that iterate the split substrings as absl::string_view
|
294
|
+
// objects. These methods enable a Splitter to be used in a range-based for
|
295
|
+
// loop.
|
296
|
+
const_iterator begin() const { return {const_iterator::kInitState, this}; }
|
297
|
+
const_iterator end() const { return {const_iterator::kEndState, this}; }
|
298
|
+
|
299
|
+
// An implicit conversion operator that is restricted to only those containers
|
300
|
+
// that the splitter is convertible to.
|
301
|
+
template <typename Container,
|
302
|
+
typename = typename std::enable_if<
|
303
|
+
SplitterIsConvertibleTo<Container>::value>::type>
|
304
|
+
operator Container() const { // NOLINT(runtime/explicit)
|
305
|
+
return ConvertToContainer<Container, typename Container::value_type,
|
306
|
+
HasMappedType<Container>::value>()(*this);
|
307
|
+
}
|
308
|
+
|
309
|
+
// Returns a pair with its .first and .second members set to the first two
|
310
|
+
// strings returned by the begin() iterator. Either/both of .first and .second
|
311
|
+
// will be constructed with empty strings if the iterator doesn't have a
|
312
|
+
// corresponding value.
|
313
|
+
template <typename First, typename Second>
|
314
|
+
operator std::pair<First, Second>() const { // NOLINT(runtime/explicit)
|
315
|
+
absl::string_view first, second;
|
316
|
+
auto it = begin();
|
317
|
+
if (it != end()) {
|
318
|
+
first = *it;
|
319
|
+
if (++it != end()) {
|
320
|
+
second = *it;
|
321
|
+
}
|
322
|
+
}
|
323
|
+
return {First(first), Second(second)};
|
324
|
+
}
|
325
|
+
|
326
|
+
private:
|
327
|
+
// ConvertToContainer is a functor converting a Splitter to the requested
|
328
|
+
// Container of ValueType. It is specialized below to optimize splitting to
|
329
|
+
// certain combinations of Container and ValueType.
|
330
|
+
//
|
331
|
+
// This base template handles the generic case of storing the split results in
|
332
|
+
// the requested non-map-like container and converting the split substrings to
|
333
|
+
// the requested type.
|
334
|
+
template <typename Container, typename ValueType, bool is_map = false>
|
335
|
+
struct ConvertToContainer {
|
336
|
+
Container operator()(const Splitter& splitter) const {
|
337
|
+
Container c;
|
338
|
+
auto it = std::inserter(c, c.end());
|
339
|
+
for (const auto sp : splitter) {
|
340
|
+
*it++ = ValueType(sp);
|
341
|
+
}
|
342
|
+
return c;
|
343
|
+
}
|
344
|
+
};
|
345
|
+
|
346
|
+
// Partial specialization for a std::vector<absl::string_view>.
|
347
|
+
//
|
348
|
+
// Optimized for the common case of splitting to a
|
349
|
+
// std::vector<absl::string_view>. In this case we first split the results to
|
350
|
+
// a small array of absl::string_view on the stack, to reduce reallocations.
|
351
|
+
template <typename A>
|
352
|
+
struct ConvertToContainer<std::vector<absl::string_view, A>,
|
353
|
+
absl::string_view, false> {
|
354
|
+
std::vector<absl::string_view, A> operator()(
|
355
|
+
const Splitter& splitter) const {
|
356
|
+
struct raw_view {
|
357
|
+
const char* data;
|
358
|
+
size_t size;
|
359
|
+
operator absl::string_view() const { // NOLINT(runtime/explicit)
|
360
|
+
return {data, size};
|
361
|
+
}
|
362
|
+
};
|
363
|
+
std::vector<absl::string_view, A> v;
|
364
|
+
std::array<raw_view, 16> ar;
|
365
|
+
for (auto it = splitter.begin(); !it.at_end();) {
|
366
|
+
size_t index = 0;
|
367
|
+
do {
|
368
|
+
ar[index].data = it->data();
|
369
|
+
ar[index].size = it->size();
|
370
|
+
++it;
|
371
|
+
} while (++index != ar.size() && !it.at_end());
|
372
|
+
v.insert(v.end(), ar.begin(), ar.begin() + index);
|
373
|
+
}
|
374
|
+
return v;
|
375
|
+
}
|
376
|
+
};
|
377
|
+
|
378
|
+
// Partial specialization for a std::vector<std::string>.
|
379
|
+
//
|
380
|
+
// Optimized for the common case of splitting to a std::vector<std::string>.
|
381
|
+
// In this case we first split the results to a std::vector<absl::string_view>
|
382
|
+
// so the returned std::vector<std::string> can have space reserved to avoid
|
383
|
+
// std::string moves.
|
384
|
+
template <typename A>
|
385
|
+
struct ConvertToContainer<std::vector<std::string, A>, std::string, false> {
|
386
|
+
std::vector<std::string, A> operator()(const Splitter& splitter) const {
|
387
|
+
const std::vector<absl::string_view> v = splitter;
|
388
|
+
return std::vector<std::string, A>(v.begin(), v.end());
|
389
|
+
}
|
390
|
+
};
|
391
|
+
|
392
|
+
// Partial specialization for containers of pairs (e.g., maps).
|
393
|
+
//
|
394
|
+
// The algorithm is to insert a new pair into the map for each even-numbered
|
395
|
+
// item, with the even-numbered item as the key with a default-constructed
|
396
|
+
// value. Each odd-numbered item will then be assigned to the last pair's
|
397
|
+
// value.
|
398
|
+
template <typename Container, typename First, typename Second>
|
399
|
+
struct ConvertToContainer<Container, std::pair<const First, Second>, true> {
|
400
|
+
Container operator()(const Splitter& splitter) const {
|
401
|
+
Container m;
|
402
|
+
typename Container::iterator it;
|
403
|
+
bool insert = true;
|
404
|
+
for (const auto sp : splitter) {
|
405
|
+
if (insert) {
|
406
|
+
it = Inserter<Container>::Insert(&m, First(sp), Second());
|
407
|
+
} else {
|
408
|
+
it->second = Second(sp);
|
409
|
+
}
|
410
|
+
insert = !insert;
|
411
|
+
}
|
412
|
+
return m;
|
413
|
+
}
|
414
|
+
|
415
|
+
// Inserts the key and value into the given map, returning an iterator to
|
416
|
+
// the inserted item. Specialized for std::map and std::multimap to use
|
417
|
+
// emplace() and adapt emplace()'s return value.
|
418
|
+
template <typename Map>
|
419
|
+
struct Inserter {
|
420
|
+
using M = Map;
|
421
|
+
template <typename... Args>
|
422
|
+
static typename M::iterator Insert(M* m, Args&&... args) {
|
423
|
+
return m->insert(std::make_pair(std::forward<Args>(args)...)).first;
|
424
|
+
}
|
425
|
+
};
|
426
|
+
|
427
|
+
template <typename... Ts>
|
428
|
+
struct Inserter<std::map<Ts...>> {
|
429
|
+
using M = std::map<Ts...>;
|
430
|
+
template <typename... Args>
|
431
|
+
static typename M::iterator Insert(M* m, Args&&... args) {
|
432
|
+
return m->emplace(std::make_pair(std::forward<Args>(args)...)).first;
|
433
|
+
}
|
434
|
+
};
|
435
|
+
|
436
|
+
template <typename... Ts>
|
437
|
+
struct Inserter<std::multimap<Ts...>> {
|
438
|
+
using M = std::multimap<Ts...>;
|
439
|
+
template <typename... Args>
|
440
|
+
static typename M::iterator Insert(M* m, Args&&... args) {
|
441
|
+
return m->emplace(std::make_pair(std::forward<Args>(args)...));
|
442
|
+
}
|
443
|
+
};
|
444
|
+
};
|
445
|
+
|
446
|
+
ConvertibleToStringView text_;
|
447
|
+
Delimiter delimiter_;
|
448
|
+
Predicate predicate_;
|
449
|
+
};
|
450
|
+
|
451
|
+
} // namespace strings_internal
|
452
|
+
ABSL_NAMESPACE_END
|
453
|
+
} // namespace absl
|
454
|
+
|
455
|
+
#endif // ABSL_STRINGS_INTERNAL_STR_SPLIT_INTERNAL_H_
|