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,91 @@
|
|
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: strip.h
|
18
|
+
// -----------------------------------------------------------------------------
|
19
|
+
//
|
20
|
+
// This file contains various functions for stripping substrings from a string.
|
21
|
+
#ifndef ABSL_STRINGS_STRIP_H_
|
22
|
+
#define ABSL_STRINGS_STRIP_H_
|
23
|
+
|
24
|
+
#include <cstddef>
|
25
|
+
#include <string>
|
26
|
+
|
27
|
+
#include "absl/base/macros.h"
|
28
|
+
#include "absl/strings/ascii.h"
|
29
|
+
#include "absl/strings/match.h"
|
30
|
+
#include "absl/strings/string_view.h"
|
31
|
+
|
32
|
+
namespace absl {
|
33
|
+
ABSL_NAMESPACE_BEGIN
|
34
|
+
|
35
|
+
// ConsumePrefix()
|
36
|
+
//
|
37
|
+
// Strips the `expected` prefix from the start of the given string, returning
|
38
|
+
// `true` if the strip operation succeeded or false otherwise.
|
39
|
+
//
|
40
|
+
// Example:
|
41
|
+
//
|
42
|
+
// absl::string_view input("abc");
|
43
|
+
// EXPECT_TRUE(absl::ConsumePrefix(&input, "a"));
|
44
|
+
// EXPECT_EQ(input, "bc");
|
45
|
+
inline bool ConsumePrefix(absl::string_view* str, absl::string_view expected) {
|
46
|
+
if (!absl::StartsWith(*str, expected)) return false;
|
47
|
+
str->remove_prefix(expected.size());
|
48
|
+
return true;
|
49
|
+
}
|
50
|
+
// ConsumeSuffix()
|
51
|
+
//
|
52
|
+
// Strips the `expected` suffix from the end of the given string, returning
|
53
|
+
// `true` if the strip operation succeeded or false otherwise.
|
54
|
+
//
|
55
|
+
// Example:
|
56
|
+
//
|
57
|
+
// absl::string_view input("abcdef");
|
58
|
+
// EXPECT_TRUE(absl::ConsumeSuffix(&input, "def"));
|
59
|
+
// EXPECT_EQ(input, "abc");
|
60
|
+
inline bool ConsumeSuffix(absl::string_view* str, absl::string_view expected) {
|
61
|
+
if (!absl::EndsWith(*str, expected)) return false;
|
62
|
+
str->remove_suffix(expected.size());
|
63
|
+
return true;
|
64
|
+
}
|
65
|
+
|
66
|
+
// StripPrefix()
|
67
|
+
//
|
68
|
+
// Returns a view into the input string 'str' with the given 'prefix' removed,
|
69
|
+
// but leaving the original string intact. If the prefix does not match at the
|
70
|
+
// start of the string, returns the original string instead.
|
71
|
+
ABSL_MUST_USE_RESULT inline absl::string_view StripPrefix(
|
72
|
+
absl::string_view str, absl::string_view prefix) {
|
73
|
+
if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size());
|
74
|
+
return str;
|
75
|
+
}
|
76
|
+
|
77
|
+
// StripSuffix()
|
78
|
+
//
|
79
|
+
// Returns a view into the input string 'str' with the given 'suffix' removed,
|
80
|
+
// but leaving the original string intact. If the suffix does not match at the
|
81
|
+
// end of the string, returns the original string instead.
|
82
|
+
ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix(
|
83
|
+
absl::string_view str, absl::string_view suffix) {
|
84
|
+
if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size());
|
85
|
+
return str;
|
86
|
+
}
|
87
|
+
|
88
|
+
ABSL_NAMESPACE_END
|
89
|
+
} // namespace absl
|
90
|
+
|
91
|
+
#endif // ABSL_STRINGS_STRIP_H_
|
@@ -0,0 +1,171 @@
|
|
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/substitute.h"
|
16
|
+
|
17
|
+
#include <algorithm>
|
18
|
+
|
19
|
+
#include "absl/base/internal/raw_logging.h"
|
20
|
+
#include "absl/strings/ascii.h"
|
21
|
+
#include "absl/strings/escaping.h"
|
22
|
+
#include "absl/strings/internal/resize_uninitialized.h"
|
23
|
+
#include "absl/strings/string_view.h"
|
24
|
+
|
25
|
+
namespace absl {
|
26
|
+
ABSL_NAMESPACE_BEGIN
|
27
|
+
namespace substitute_internal {
|
28
|
+
|
29
|
+
void SubstituteAndAppendArray(std::string* output, absl::string_view format,
|
30
|
+
const absl::string_view* args_array,
|
31
|
+
size_t num_args) {
|
32
|
+
// Determine total size needed.
|
33
|
+
size_t size = 0;
|
34
|
+
for (size_t i = 0; i < format.size(); i++) {
|
35
|
+
if (format[i] == '$') {
|
36
|
+
if (i + 1 >= format.size()) {
|
37
|
+
#ifndef NDEBUG
|
38
|
+
ABSL_RAW_LOG(FATAL,
|
39
|
+
"Invalid absl::Substitute() format std::string: \"%s\".",
|
40
|
+
absl::CEscape(format).c_str());
|
41
|
+
#endif
|
42
|
+
return;
|
43
|
+
} else if (absl::ascii_isdigit(format[i + 1])) {
|
44
|
+
int index = format[i + 1] - '0';
|
45
|
+
if (static_cast<size_t>(index) >= num_args) {
|
46
|
+
#ifndef NDEBUG
|
47
|
+
ABSL_RAW_LOG(
|
48
|
+
FATAL,
|
49
|
+
"Invalid absl::Substitute() format std::string: asked for \"$"
|
50
|
+
"%d\", but only %d args were given. Full format std::string was: "
|
51
|
+
"\"%s\".",
|
52
|
+
index, static_cast<int>(num_args), absl::CEscape(format).c_str());
|
53
|
+
#endif
|
54
|
+
return;
|
55
|
+
}
|
56
|
+
size += args_array[index].size();
|
57
|
+
++i; // Skip next char.
|
58
|
+
} else if (format[i + 1] == '$') {
|
59
|
+
++size;
|
60
|
+
++i; // Skip next char.
|
61
|
+
} else {
|
62
|
+
#ifndef NDEBUG
|
63
|
+
ABSL_RAW_LOG(FATAL,
|
64
|
+
"Invalid absl::Substitute() format std::string: \"%s\".",
|
65
|
+
absl::CEscape(format).c_str());
|
66
|
+
#endif
|
67
|
+
return;
|
68
|
+
}
|
69
|
+
} else {
|
70
|
+
++size;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
if (size == 0) return;
|
75
|
+
|
76
|
+
// Build the std::string.
|
77
|
+
size_t original_size = output->size();
|
78
|
+
strings_internal::STLStringResizeUninitialized(output, original_size + size);
|
79
|
+
char* target = &(*output)[original_size];
|
80
|
+
for (size_t i = 0; i < format.size(); i++) {
|
81
|
+
if (format[i] == '$') {
|
82
|
+
if (absl::ascii_isdigit(format[i + 1])) {
|
83
|
+
const absl::string_view src = args_array[format[i + 1] - '0'];
|
84
|
+
target = std::copy(src.begin(), src.end(), target);
|
85
|
+
++i; // Skip next char.
|
86
|
+
} else if (format[i + 1] == '$') {
|
87
|
+
*target++ = '$';
|
88
|
+
++i; // Skip next char.
|
89
|
+
}
|
90
|
+
} else {
|
91
|
+
*target++ = format[i];
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
assert(target == output->data() + output->size());
|
96
|
+
}
|
97
|
+
|
98
|
+
Arg::Arg(const void* value) {
|
99
|
+
static_assert(sizeof(scratch_) >= sizeof(value) * 2 + 2,
|
100
|
+
"fix sizeof(scratch_)");
|
101
|
+
if (value == nullptr) {
|
102
|
+
piece_ = "NULL";
|
103
|
+
} else {
|
104
|
+
char* ptr = scratch_ + sizeof(scratch_);
|
105
|
+
uintptr_t num = reinterpret_cast<uintptr_t>(value);
|
106
|
+
do {
|
107
|
+
*--ptr = absl::numbers_internal::kHexChar[num & 0xf];
|
108
|
+
num >>= 4;
|
109
|
+
} while (num != 0);
|
110
|
+
*--ptr = 'x';
|
111
|
+
*--ptr = '0';
|
112
|
+
piece_ = absl::string_view(ptr, scratch_ + sizeof(scratch_) - ptr);
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
// TODO(jorg): Don't duplicate so much code between here and str_cat.cc
|
117
|
+
Arg::Arg(Hex hex) {
|
118
|
+
char* const end = &scratch_[numbers_internal::kFastToBufferSize];
|
119
|
+
char* writer = end;
|
120
|
+
uint64_t value = hex.value;
|
121
|
+
do {
|
122
|
+
*--writer = absl::numbers_internal::kHexChar[value & 0xF];
|
123
|
+
value >>= 4;
|
124
|
+
} while (value != 0);
|
125
|
+
|
126
|
+
char* beg;
|
127
|
+
if (end - writer < hex.width) {
|
128
|
+
beg = end - hex.width;
|
129
|
+
std::fill_n(beg, writer - beg, hex.fill);
|
130
|
+
} else {
|
131
|
+
beg = writer;
|
132
|
+
}
|
133
|
+
|
134
|
+
piece_ = absl::string_view(beg, end - beg);
|
135
|
+
}
|
136
|
+
|
137
|
+
// TODO(jorg): Don't duplicate so much code between here and str_cat.cc
|
138
|
+
Arg::Arg(Dec dec) {
|
139
|
+
assert(dec.width <= numbers_internal::kFastToBufferSize);
|
140
|
+
char* const end = &scratch_[numbers_internal::kFastToBufferSize];
|
141
|
+
char* const minfill = end - dec.width;
|
142
|
+
char* writer = end;
|
143
|
+
uint64_t value = dec.value;
|
144
|
+
bool neg = dec.neg;
|
145
|
+
while (value > 9) {
|
146
|
+
*--writer = '0' + (value % 10);
|
147
|
+
value /= 10;
|
148
|
+
}
|
149
|
+
*--writer = '0' + value;
|
150
|
+
if (neg) *--writer = '-';
|
151
|
+
|
152
|
+
ptrdiff_t fillers = writer - minfill;
|
153
|
+
if (fillers > 0) {
|
154
|
+
// Tricky: if the fill character is ' ', then it's <fill><+/-><digits>
|
155
|
+
// But...: if the fill character is '0', then it's <+/-><fill><digits>
|
156
|
+
bool add_sign_again = false;
|
157
|
+
if (neg && dec.fill == '0') { // If filling with '0',
|
158
|
+
++writer; // ignore the sign we just added
|
159
|
+
add_sign_again = true; // and re-add the sign later.
|
160
|
+
}
|
161
|
+
writer -= fillers;
|
162
|
+
std::fill_n(writer, fillers, dec.fill);
|
163
|
+
if (add_sign_again) *--writer = '-';
|
164
|
+
}
|
165
|
+
|
166
|
+
piece_ = absl::string_view(writer, end - writer);
|
167
|
+
}
|
168
|
+
|
169
|
+
} // namespace substitute_internal
|
170
|
+
ABSL_NAMESPACE_END
|
171
|
+
} // namespace absl
|
@@ -0,0 +1,693 @@
|
|
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: substitute.h
|
18
|
+
// -----------------------------------------------------------------------------
|
19
|
+
//
|
20
|
+
// This package contains functions for efficiently performing string
|
21
|
+
// substitutions using a format string with positional notation:
|
22
|
+
// `Substitute()` and `SubstituteAndAppend()`.
|
23
|
+
//
|
24
|
+
// Unlike printf-style format specifiers, `Substitute()` functions do not need
|
25
|
+
// to specify the type of the substitution arguments. Supported arguments
|
26
|
+
// following the format string, such as strings, string_views, ints,
|
27
|
+
// floats, and bools, are automatically converted to strings during the
|
28
|
+
// substitution process. (See below for a full list of supported types.)
|
29
|
+
//
|
30
|
+
// `Substitute()` does not allow you to specify *how* to format a value, beyond
|
31
|
+
// the default conversion to string. For example, you cannot format an integer
|
32
|
+
// in hex.
|
33
|
+
//
|
34
|
+
// The format string uses positional identifiers indicated by a dollar sign ($)
|
35
|
+
// and single digit positional ids to indicate which substitution arguments to
|
36
|
+
// use at that location within the format string.
|
37
|
+
//
|
38
|
+
// A '$$' sequence in the format string causes a literal '$' character to be
|
39
|
+
// output.
|
40
|
+
//
|
41
|
+
// Example 1:
|
42
|
+
// std::string s = Substitute("$1 purchased $0 $2 for $$10. Thanks $1!",
|
43
|
+
// 5, "Bob", "Apples");
|
44
|
+
// EXPECT_EQ("Bob purchased 5 Apples for $10. Thanks Bob!", s);
|
45
|
+
//
|
46
|
+
// Example 2:
|
47
|
+
// std::string s = "Hi. ";
|
48
|
+
// SubstituteAndAppend(&s, "My name is $0 and I am $1 years old.", "Bob", 5);
|
49
|
+
// EXPECT_EQ("Hi. My name is Bob and I am 5 years old.", s);
|
50
|
+
//
|
51
|
+
// Supported types:
|
52
|
+
// * absl::string_view, std::string, const char* (null is equivalent to "")
|
53
|
+
// * int32_t, int64_t, uint32_t, uint64
|
54
|
+
// * float, double
|
55
|
+
// * bool (Printed as "true" or "false")
|
56
|
+
// * pointer types other than char* (Printed as "0x<lower case hex string>",
|
57
|
+
// except that null is printed as "NULL")
|
58
|
+
//
|
59
|
+
// If an invalid format string is provided, Substitute returns an empty string
|
60
|
+
// and SubstituteAndAppend does not change the provided output string.
|
61
|
+
// A format string is invalid if it:
|
62
|
+
// * ends in an unescaped $ character,
|
63
|
+
// e.g. "Hello $", or
|
64
|
+
// * calls for a position argument which is not provided,
|
65
|
+
// e.g. Substitute("Hello $2", "world"), or
|
66
|
+
// * specifies a non-digit, non-$ character after an unescaped $ character,
|
67
|
+
// e.g. "Hello $f".
|
68
|
+
// In debug mode, i.e. #ifndef NDEBUG, such errors terminate the program.
|
69
|
+
|
70
|
+
#ifndef ABSL_STRINGS_SUBSTITUTE_H_
|
71
|
+
#define ABSL_STRINGS_SUBSTITUTE_H_
|
72
|
+
|
73
|
+
#include <cstring>
|
74
|
+
#include <string>
|
75
|
+
#include <type_traits>
|
76
|
+
#include <vector>
|
77
|
+
|
78
|
+
#include "absl/base/macros.h"
|
79
|
+
#include "absl/base/port.h"
|
80
|
+
#include "absl/strings/ascii.h"
|
81
|
+
#include "absl/strings/escaping.h"
|
82
|
+
#include "absl/strings/numbers.h"
|
83
|
+
#include "absl/strings/str_cat.h"
|
84
|
+
#include "absl/strings/str_split.h"
|
85
|
+
#include "absl/strings/string_view.h"
|
86
|
+
#include "absl/strings/strip.h"
|
87
|
+
|
88
|
+
namespace absl {
|
89
|
+
ABSL_NAMESPACE_BEGIN
|
90
|
+
namespace substitute_internal {
|
91
|
+
|
92
|
+
// Arg
|
93
|
+
//
|
94
|
+
// This class provides an argument type for `absl::Substitute()` and
|
95
|
+
// `absl::SubstituteAndAppend()`. `Arg` handles implicit conversion of various
|
96
|
+
// types to a string. (`Arg` is very similar to the `AlphaNum` class in
|
97
|
+
// `StrCat()`.)
|
98
|
+
//
|
99
|
+
// This class has implicit constructors.
|
100
|
+
class Arg {
|
101
|
+
public:
|
102
|
+
// Overloads for std::string-y things
|
103
|
+
//
|
104
|
+
// Explicitly overload `const char*` so the compiler doesn't cast to `bool`.
|
105
|
+
Arg(const char* value) // NOLINT(runtime/explicit)
|
106
|
+
: piece_(absl::NullSafeStringView(value)) {}
|
107
|
+
template <typename Allocator>
|
108
|
+
Arg( // NOLINT
|
109
|
+
const std::basic_string<char, std::char_traits<char>, Allocator>&
|
110
|
+
value) noexcept
|
111
|
+
: piece_(value) {}
|
112
|
+
Arg(absl::string_view value) // NOLINT(runtime/explicit)
|
113
|
+
: piece_(value) {}
|
114
|
+
|
115
|
+
// Overloads for primitives
|
116
|
+
//
|
117
|
+
// No overloads are available for signed and unsigned char because if people
|
118
|
+
// are explicitly declaring their chars as signed or unsigned then they are
|
119
|
+
// probably using them as 8-bit integers and would probably prefer an integer
|
120
|
+
// representation. However, we can't really know, so we make the caller decide
|
121
|
+
// what to do.
|
122
|
+
Arg(char value) // NOLINT(runtime/explicit)
|
123
|
+
: piece_(scratch_, 1) { scratch_[0] = value; }
|
124
|
+
Arg(short value) // NOLINT(*)
|
125
|
+
: piece_(scratch_,
|
126
|
+
numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
|
127
|
+
Arg(unsigned short value) // NOLINT(*)
|
128
|
+
: piece_(scratch_,
|
129
|
+
numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
|
130
|
+
Arg(int value) // NOLINT(runtime/explicit)
|
131
|
+
: piece_(scratch_,
|
132
|
+
numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
|
133
|
+
Arg(unsigned int value) // NOLINT(runtime/explicit)
|
134
|
+
: piece_(scratch_,
|
135
|
+
numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
|
136
|
+
Arg(long value) // NOLINT(*)
|
137
|
+
: piece_(scratch_,
|
138
|
+
numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
|
139
|
+
Arg(unsigned long value) // NOLINT(*)
|
140
|
+
: piece_(scratch_,
|
141
|
+
numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
|
142
|
+
Arg(long long value) // NOLINT(*)
|
143
|
+
: piece_(scratch_,
|
144
|
+
numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
|
145
|
+
Arg(unsigned long long value) // NOLINT(*)
|
146
|
+
: piece_(scratch_,
|
147
|
+
numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
|
148
|
+
Arg(float value) // NOLINT(runtime/explicit)
|
149
|
+
: piece_(scratch_, numbers_internal::SixDigitsToBuffer(value, scratch_)) {
|
150
|
+
}
|
151
|
+
Arg(double value) // NOLINT(runtime/explicit)
|
152
|
+
: piece_(scratch_, numbers_internal::SixDigitsToBuffer(value, scratch_)) {
|
153
|
+
}
|
154
|
+
Arg(bool value) // NOLINT(runtime/explicit)
|
155
|
+
: piece_(value ? "true" : "false") {}
|
156
|
+
|
157
|
+
Arg(Hex hex); // NOLINT(runtime/explicit)
|
158
|
+
Arg(Dec dec); // NOLINT(runtime/explicit)
|
159
|
+
|
160
|
+
// vector<bool>::reference and const_reference require special help to
|
161
|
+
// convert to `AlphaNum` because it requires two user defined conversions.
|
162
|
+
template <typename T,
|
163
|
+
absl::enable_if_t<
|
164
|
+
std::is_class<T>::value &&
|
165
|
+
(std::is_same<T, std::vector<bool>::reference>::value ||
|
166
|
+
std::is_same<T, std::vector<bool>::const_reference>::value)>* =
|
167
|
+
nullptr>
|
168
|
+
Arg(T value) // NOLINT(google-explicit-constructor)
|
169
|
+
: Arg(static_cast<bool>(value)) {}
|
170
|
+
|
171
|
+
// `void*` values, with the exception of `char*`, are printed as
|
172
|
+
// "0x<hex value>". However, in the case of `nullptr`, "NULL" is printed.
|
173
|
+
Arg(const void* value); // NOLINT(runtime/explicit)
|
174
|
+
|
175
|
+
Arg(const Arg&) = delete;
|
176
|
+
Arg& operator=(const Arg&) = delete;
|
177
|
+
|
178
|
+
absl::string_view piece() const { return piece_; }
|
179
|
+
|
180
|
+
private:
|
181
|
+
absl::string_view piece_;
|
182
|
+
char scratch_[numbers_internal::kFastToBufferSize];
|
183
|
+
};
|
184
|
+
|
185
|
+
// Internal helper function. Don't call this from outside this implementation.
|
186
|
+
// This interface may change without notice.
|
187
|
+
void SubstituteAndAppendArray(std::string* output, absl::string_view format,
|
188
|
+
const absl::string_view* args_array,
|
189
|
+
size_t num_args);
|
190
|
+
|
191
|
+
#if defined(ABSL_BAD_CALL_IF)
|
192
|
+
constexpr int CalculateOneBit(const char* format) {
|
193
|
+
// Returns:
|
194
|
+
// * 2^N for '$N' when N is in [0-9]
|
195
|
+
// * 0 for correct '$' escaping: '$$'.
|
196
|
+
// * -1 otherwise.
|
197
|
+
return (*format < '0' || *format > '9') ? (*format == '$' ? 0 : -1)
|
198
|
+
: (1 << (*format - '0'));
|
199
|
+
}
|
200
|
+
|
201
|
+
constexpr const char* SkipNumber(const char* format) {
|
202
|
+
return !*format ? format : (format + 1);
|
203
|
+
}
|
204
|
+
|
205
|
+
constexpr int PlaceholderBitmask(const char* format) {
|
206
|
+
return !*format ? 0 : *format != '$'
|
207
|
+
? PlaceholderBitmask(format + 1)
|
208
|
+
: (CalculateOneBit(format + 1) |
|
209
|
+
PlaceholderBitmask(SkipNumber(format + 1)));
|
210
|
+
}
|
211
|
+
#endif // ABSL_BAD_CALL_IF
|
212
|
+
|
213
|
+
} // namespace substitute_internal
|
214
|
+
|
215
|
+
//
|
216
|
+
// PUBLIC API
|
217
|
+
//
|
218
|
+
|
219
|
+
// SubstituteAndAppend()
|
220
|
+
//
|
221
|
+
// Substitutes variables into a given format string and appends to a given
|
222
|
+
// output string. See file comments above for usage.
|
223
|
+
//
|
224
|
+
// The declarations of `SubstituteAndAppend()` below consist of overloads
|
225
|
+
// for passing 0 to 10 arguments, respectively.
|
226
|
+
//
|
227
|
+
// NOTE: A zero-argument `SubstituteAndAppend()` may be used within variadic
|
228
|
+
// templates to allow a variable number of arguments.
|
229
|
+
//
|
230
|
+
// Example:
|
231
|
+
// template <typename... Args>
|
232
|
+
// void VarMsg(std::string* boilerplate, absl::string_view format,
|
233
|
+
// const Args&... args) {
|
234
|
+
// absl::SubstituteAndAppend(boilerplate, format, args...);
|
235
|
+
// }
|
236
|
+
//
|
237
|
+
inline void SubstituteAndAppend(std::string* output, absl::string_view format) {
|
238
|
+
substitute_internal::SubstituteAndAppendArray(output, format, nullptr, 0);
|
239
|
+
}
|
240
|
+
|
241
|
+
inline void SubstituteAndAppend(std::string* output, absl::string_view format,
|
242
|
+
const substitute_internal::Arg& a0) {
|
243
|
+
const absl::string_view args[] = {a0.piece()};
|
244
|
+
substitute_internal::SubstituteAndAppendArray(output, format, args,
|
245
|
+
ABSL_ARRAYSIZE(args));
|
246
|
+
}
|
247
|
+
|
248
|
+
inline void SubstituteAndAppend(std::string* output, absl::string_view format,
|
249
|
+
const substitute_internal::Arg& a0,
|
250
|
+
const substitute_internal::Arg& a1) {
|
251
|
+
const absl::string_view args[] = {a0.piece(), a1.piece()};
|
252
|
+
substitute_internal::SubstituteAndAppendArray(output, format, args,
|
253
|
+
ABSL_ARRAYSIZE(args));
|
254
|
+
}
|
255
|
+
|
256
|
+
inline void SubstituteAndAppend(std::string* output, absl::string_view format,
|
257
|
+
const substitute_internal::Arg& a0,
|
258
|
+
const substitute_internal::Arg& a1,
|
259
|
+
const substitute_internal::Arg& a2) {
|
260
|
+
const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece()};
|
261
|
+
substitute_internal::SubstituteAndAppendArray(output, format, args,
|
262
|
+
ABSL_ARRAYSIZE(args));
|
263
|
+
}
|
264
|
+
|
265
|
+
inline void SubstituteAndAppend(std::string* output, absl::string_view format,
|
266
|
+
const substitute_internal::Arg& a0,
|
267
|
+
const substitute_internal::Arg& a1,
|
268
|
+
const substitute_internal::Arg& a2,
|
269
|
+
const substitute_internal::Arg& a3) {
|
270
|
+
const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
|
271
|
+
a3.piece()};
|
272
|
+
substitute_internal::SubstituteAndAppendArray(output, format, args,
|
273
|
+
ABSL_ARRAYSIZE(args));
|
274
|
+
}
|
275
|
+
|
276
|
+
inline void SubstituteAndAppend(std::string* output, absl::string_view format,
|
277
|
+
const substitute_internal::Arg& a0,
|
278
|
+
const substitute_internal::Arg& a1,
|
279
|
+
const substitute_internal::Arg& a2,
|
280
|
+
const substitute_internal::Arg& a3,
|
281
|
+
const substitute_internal::Arg& a4) {
|
282
|
+
const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
|
283
|
+
a3.piece(), a4.piece()};
|
284
|
+
substitute_internal::SubstituteAndAppendArray(output, format, args,
|
285
|
+
ABSL_ARRAYSIZE(args));
|
286
|
+
}
|
287
|
+
|
288
|
+
inline void SubstituteAndAppend(std::string* output, absl::string_view format,
|
289
|
+
const substitute_internal::Arg& a0,
|
290
|
+
const substitute_internal::Arg& a1,
|
291
|
+
const substitute_internal::Arg& a2,
|
292
|
+
const substitute_internal::Arg& a3,
|
293
|
+
const substitute_internal::Arg& a4,
|
294
|
+
const substitute_internal::Arg& a5) {
|
295
|
+
const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
|
296
|
+
a3.piece(), a4.piece(), a5.piece()};
|
297
|
+
substitute_internal::SubstituteAndAppendArray(output, format, args,
|
298
|
+
ABSL_ARRAYSIZE(args));
|
299
|
+
}
|
300
|
+
|
301
|
+
inline void SubstituteAndAppend(std::string* output, absl::string_view format,
|
302
|
+
const substitute_internal::Arg& a0,
|
303
|
+
const substitute_internal::Arg& a1,
|
304
|
+
const substitute_internal::Arg& a2,
|
305
|
+
const substitute_internal::Arg& a3,
|
306
|
+
const substitute_internal::Arg& a4,
|
307
|
+
const substitute_internal::Arg& a5,
|
308
|
+
const substitute_internal::Arg& a6) {
|
309
|
+
const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
|
310
|
+
a3.piece(), a4.piece(), a5.piece(),
|
311
|
+
a6.piece()};
|
312
|
+
substitute_internal::SubstituteAndAppendArray(output, format, args,
|
313
|
+
ABSL_ARRAYSIZE(args));
|
314
|
+
}
|
315
|
+
|
316
|
+
inline void SubstituteAndAppend(
|
317
|
+
std::string* output, absl::string_view format,
|
318
|
+
const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
|
319
|
+
const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
|
320
|
+
const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
|
321
|
+
const substitute_internal::Arg& a6, const substitute_internal::Arg& a7) {
|
322
|
+
const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
|
323
|
+
a3.piece(), a4.piece(), a5.piece(),
|
324
|
+
a6.piece(), a7.piece()};
|
325
|
+
substitute_internal::SubstituteAndAppendArray(output, format, args,
|
326
|
+
ABSL_ARRAYSIZE(args));
|
327
|
+
}
|
328
|
+
|
329
|
+
inline void SubstituteAndAppend(
|
330
|
+
std::string* output, absl::string_view format,
|
331
|
+
const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
|
332
|
+
const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
|
333
|
+
const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
|
334
|
+
const substitute_internal::Arg& a6, const substitute_internal::Arg& a7,
|
335
|
+
const substitute_internal::Arg& a8) {
|
336
|
+
const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
|
337
|
+
a3.piece(), a4.piece(), a5.piece(),
|
338
|
+
a6.piece(), a7.piece(), a8.piece()};
|
339
|
+
substitute_internal::SubstituteAndAppendArray(output, format, args,
|
340
|
+
ABSL_ARRAYSIZE(args));
|
341
|
+
}
|
342
|
+
|
343
|
+
inline void SubstituteAndAppend(
|
344
|
+
std::string* output, absl::string_view format,
|
345
|
+
const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
|
346
|
+
const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
|
347
|
+
const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
|
348
|
+
const substitute_internal::Arg& a6, const substitute_internal::Arg& a7,
|
349
|
+
const substitute_internal::Arg& a8, const substitute_internal::Arg& a9) {
|
350
|
+
const absl::string_view args[] = {
|
351
|
+
a0.piece(), a1.piece(), a2.piece(), a3.piece(), a4.piece(),
|
352
|
+
a5.piece(), a6.piece(), a7.piece(), a8.piece(), a9.piece()};
|
353
|
+
substitute_internal::SubstituteAndAppendArray(output, format, args,
|
354
|
+
ABSL_ARRAYSIZE(args));
|
355
|
+
}
|
356
|
+
|
357
|
+
#if defined(ABSL_BAD_CALL_IF)
|
358
|
+
// This body of functions catches cases where the number of placeholders
|
359
|
+
// doesn't match the number of data arguments.
|
360
|
+
void SubstituteAndAppend(std::string* output, const char* format)
|
361
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 0,
|
362
|
+
"There were no substitution arguments "
|
363
|
+
"but this format std::string has a $[0-9] in it");
|
364
|
+
|
365
|
+
void SubstituteAndAppend(std::string* output, const char* format,
|
366
|
+
const substitute_internal::Arg& a0)
|
367
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1,
|
368
|
+
"There was 1 substitution argument given, but "
|
369
|
+
"this format std::string is either missing its $0, or "
|
370
|
+
"contains one of $1-$9");
|
371
|
+
|
372
|
+
void SubstituteAndAppend(std::string* output, const char* format,
|
373
|
+
const substitute_internal::Arg& a0,
|
374
|
+
const substitute_internal::Arg& a1)
|
375
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 3,
|
376
|
+
"There were 2 substitution arguments given, but "
|
377
|
+
"this format std::string is either missing its $0/$1, or "
|
378
|
+
"contains one of $2-$9");
|
379
|
+
|
380
|
+
void SubstituteAndAppend(std::string* output, const char* format,
|
381
|
+
const substitute_internal::Arg& a0,
|
382
|
+
const substitute_internal::Arg& a1,
|
383
|
+
const substitute_internal::Arg& a2)
|
384
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 7,
|
385
|
+
"There were 3 substitution arguments given, but "
|
386
|
+
"this format std::string is either missing its $0/$1/$2, or "
|
387
|
+
"contains one of $3-$9");
|
388
|
+
|
389
|
+
void SubstituteAndAppend(std::string* output, const char* format,
|
390
|
+
const substitute_internal::Arg& a0,
|
391
|
+
const substitute_internal::Arg& a1,
|
392
|
+
const substitute_internal::Arg& a2,
|
393
|
+
const substitute_internal::Arg& a3)
|
394
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 15,
|
395
|
+
"There were 4 substitution arguments given, but "
|
396
|
+
"this format std::string is either missing its $0-$3, or "
|
397
|
+
"contains one of $4-$9");
|
398
|
+
|
399
|
+
void SubstituteAndAppend(std::string* output, const char* format,
|
400
|
+
const substitute_internal::Arg& a0,
|
401
|
+
const substitute_internal::Arg& a1,
|
402
|
+
const substitute_internal::Arg& a2,
|
403
|
+
const substitute_internal::Arg& a3,
|
404
|
+
const substitute_internal::Arg& a4)
|
405
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 31,
|
406
|
+
"There were 5 substitution arguments given, but "
|
407
|
+
"this format std::string is either missing its $0-$4, or "
|
408
|
+
"contains one of $5-$9");
|
409
|
+
|
410
|
+
void SubstituteAndAppend(std::string* output, const char* format,
|
411
|
+
const substitute_internal::Arg& a0,
|
412
|
+
const substitute_internal::Arg& a1,
|
413
|
+
const substitute_internal::Arg& a2,
|
414
|
+
const substitute_internal::Arg& a3,
|
415
|
+
const substitute_internal::Arg& a4,
|
416
|
+
const substitute_internal::Arg& a5)
|
417
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 63,
|
418
|
+
"There were 6 substitution arguments given, but "
|
419
|
+
"this format std::string is either missing its $0-$5, or "
|
420
|
+
"contains one of $6-$9");
|
421
|
+
|
422
|
+
void SubstituteAndAppend(
|
423
|
+
std::string* output, const char* format, const substitute_internal::Arg& a0,
|
424
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
425
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
|
426
|
+
const substitute_internal::Arg& a5, const substitute_internal::Arg& a6)
|
427
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 127,
|
428
|
+
"There were 7 substitution arguments given, but "
|
429
|
+
"this format std::string is either missing its $0-$6, or "
|
430
|
+
"contains one of $7-$9");
|
431
|
+
|
432
|
+
void SubstituteAndAppend(
|
433
|
+
std::string* output, const char* format, const substitute_internal::Arg& a0,
|
434
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
435
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
|
436
|
+
const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
|
437
|
+
const substitute_internal::Arg& a7)
|
438
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 255,
|
439
|
+
"There were 8 substitution arguments given, but "
|
440
|
+
"this format std::string is either missing its $0-$7, or "
|
441
|
+
"contains one of $8-$9");
|
442
|
+
|
443
|
+
void SubstituteAndAppend(
|
444
|
+
std::string* output, const char* format, const substitute_internal::Arg& a0,
|
445
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
446
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
|
447
|
+
const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
|
448
|
+
const substitute_internal::Arg& a7, const substitute_internal::Arg& a8)
|
449
|
+
ABSL_BAD_CALL_IF(
|
450
|
+
substitute_internal::PlaceholderBitmask(format) != 511,
|
451
|
+
"There were 9 substitution arguments given, but "
|
452
|
+
"this format std::string is either missing its $0-$8, or contains a $9");
|
453
|
+
|
454
|
+
void SubstituteAndAppend(
|
455
|
+
std::string* output, const char* format, const substitute_internal::Arg& a0,
|
456
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
457
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
|
458
|
+
const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
|
459
|
+
const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
|
460
|
+
const substitute_internal::Arg& a9)
|
461
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1023,
|
462
|
+
"There were 10 substitution arguments given, but this "
|
463
|
+
"format std::string doesn't contain all of $0 through $9");
|
464
|
+
#endif // ABSL_BAD_CALL_IF
|
465
|
+
|
466
|
+
// Substitute()
|
467
|
+
//
|
468
|
+
// Substitutes variables into a given format string. See file comments above
|
469
|
+
// for usage.
|
470
|
+
//
|
471
|
+
// The declarations of `Substitute()` below consist of overloads for passing 0
|
472
|
+
// to 10 arguments, respectively.
|
473
|
+
//
|
474
|
+
// NOTE: A zero-argument `Substitute()` may be used within variadic templates to
|
475
|
+
// allow a variable number of arguments.
|
476
|
+
//
|
477
|
+
// Example:
|
478
|
+
// template <typename... Args>
|
479
|
+
// void VarMsg(absl::string_view format, const Args&... args) {
|
480
|
+
// std::string s = absl::Substitute(format, args...);
|
481
|
+
|
482
|
+
ABSL_MUST_USE_RESULT inline std::string Substitute(absl::string_view format) {
|
483
|
+
std::string result;
|
484
|
+
SubstituteAndAppend(&result, format);
|
485
|
+
return result;
|
486
|
+
}
|
487
|
+
|
488
|
+
ABSL_MUST_USE_RESULT inline std::string Substitute(
|
489
|
+
absl::string_view format, const substitute_internal::Arg& a0) {
|
490
|
+
std::string result;
|
491
|
+
SubstituteAndAppend(&result, format, a0);
|
492
|
+
return result;
|
493
|
+
}
|
494
|
+
|
495
|
+
ABSL_MUST_USE_RESULT inline std::string Substitute(
|
496
|
+
absl::string_view format, const substitute_internal::Arg& a0,
|
497
|
+
const substitute_internal::Arg& a1) {
|
498
|
+
std::string result;
|
499
|
+
SubstituteAndAppend(&result, format, a0, a1);
|
500
|
+
return result;
|
501
|
+
}
|
502
|
+
|
503
|
+
ABSL_MUST_USE_RESULT inline std::string Substitute(
|
504
|
+
absl::string_view format, const substitute_internal::Arg& a0,
|
505
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2) {
|
506
|
+
std::string result;
|
507
|
+
SubstituteAndAppend(&result, format, a0, a1, a2);
|
508
|
+
return result;
|
509
|
+
}
|
510
|
+
|
511
|
+
ABSL_MUST_USE_RESULT inline std::string Substitute(
|
512
|
+
absl::string_view format, const substitute_internal::Arg& a0,
|
513
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
514
|
+
const substitute_internal::Arg& a3) {
|
515
|
+
std::string result;
|
516
|
+
SubstituteAndAppend(&result, format, a0, a1, a2, a3);
|
517
|
+
return result;
|
518
|
+
}
|
519
|
+
|
520
|
+
ABSL_MUST_USE_RESULT inline std::string Substitute(
|
521
|
+
absl::string_view format, const substitute_internal::Arg& a0,
|
522
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
523
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4) {
|
524
|
+
std::string result;
|
525
|
+
SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4);
|
526
|
+
return result;
|
527
|
+
}
|
528
|
+
|
529
|
+
ABSL_MUST_USE_RESULT inline std::string Substitute(
|
530
|
+
absl::string_view format, const substitute_internal::Arg& a0,
|
531
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
532
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
|
533
|
+
const substitute_internal::Arg& a5) {
|
534
|
+
std::string result;
|
535
|
+
SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5);
|
536
|
+
return result;
|
537
|
+
}
|
538
|
+
|
539
|
+
ABSL_MUST_USE_RESULT inline std::string Substitute(
|
540
|
+
absl::string_view format, const substitute_internal::Arg& a0,
|
541
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
542
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
|
543
|
+
const substitute_internal::Arg& a5, const substitute_internal::Arg& a6) {
|
544
|
+
std::string result;
|
545
|
+
SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6);
|
546
|
+
return result;
|
547
|
+
}
|
548
|
+
|
549
|
+
ABSL_MUST_USE_RESULT inline std::string Substitute(
|
550
|
+
absl::string_view format, const substitute_internal::Arg& a0,
|
551
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
552
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
|
553
|
+
const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
|
554
|
+
const substitute_internal::Arg& a7) {
|
555
|
+
std::string result;
|
556
|
+
SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7);
|
557
|
+
return result;
|
558
|
+
}
|
559
|
+
|
560
|
+
ABSL_MUST_USE_RESULT inline std::string Substitute(
|
561
|
+
absl::string_view format, const substitute_internal::Arg& a0,
|
562
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
563
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
|
564
|
+
const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
|
565
|
+
const substitute_internal::Arg& a7, const substitute_internal::Arg& a8) {
|
566
|
+
std::string result;
|
567
|
+
SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7, a8);
|
568
|
+
return result;
|
569
|
+
}
|
570
|
+
|
571
|
+
ABSL_MUST_USE_RESULT inline std::string Substitute(
|
572
|
+
absl::string_view format, const substitute_internal::Arg& a0,
|
573
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
574
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
|
575
|
+
const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
|
576
|
+
const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
|
577
|
+
const substitute_internal::Arg& a9) {
|
578
|
+
std::string result;
|
579
|
+
SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
|
580
|
+
return result;
|
581
|
+
}
|
582
|
+
|
583
|
+
#if defined(ABSL_BAD_CALL_IF)
|
584
|
+
// This body of functions catches cases where the number of placeholders
|
585
|
+
// doesn't match the number of data arguments.
|
586
|
+
std::string Substitute(const char* format)
|
587
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 0,
|
588
|
+
"There were no substitution arguments "
|
589
|
+
"but this format std::string has a $[0-9] in it");
|
590
|
+
|
591
|
+
std::string Substitute(const char* format, const substitute_internal::Arg& a0)
|
592
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1,
|
593
|
+
"There was 1 substitution argument given, but "
|
594
|
+
"this format std::string is either missing its $0, or "
|
595
|
+
"contains one of $1-$9");
|
596
|
+
|
597
|
+
std::string Substitute(const char* format, const substitute_internal::Arg& a0,
|
598
|
+
const substitute_internal::Arg& a1)
|
599
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 3,
|
600
|
+
"There were 2 substitution arguments given, but "
|
601
|
+
"this format std::string is either missing its $0/$1, or "
|
602
|
+
"contains one of $2-$9");
|
603
|
+
|
604
|
+
std::string Substitute(const char* format, const substitute_internal::Arg& a0,
|
605
|
+
const substitute_internal::Arg& a1,
|
606
|
+
const substitute_internal::Arg& a2)
|
607
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 7,
|
608
|
+
"There were 3 substitution arguments given, but "
|
609
|
+
"this format std::string is either missing its $0/$1/$2, or "
|
610
|
+
"contains one of $3-$9");
|
611
|
+
|
612
|
+
std::string Substitute(const char* format, const substitute_internal::Arg& a0,
|
613
|
+
const substitute_internal::Arg& a1,
|
614
|
+
const substitute_internal::Arg& a2,
|
615
|
+
const substitute_internal::Arg& a3)
|
616
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 15,
|
617
|
+
"There were 4 substitution arguments given, but "
|
618
|
+
"this format std::string is either missing its $0-$3, or "
|
619
|
+
"contains one of $4-$9");
|
620
|
+
|
621
|
+
std::string Substitute(const char* format, const substitute_internal::Arg& a0,
|
622
|
+
const substitute_internal::Arg& a1,
|
623
|
+
const substitute_internal::Arg& a2,
|
624
|
+
const substitute_internal::Arg& a3,
|
625
|
+
const substitute_internal::Arg& a4)
|
626
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 31,
|
627
|
+
"There were 5 substitution arguments given, but "
|
628
|
+
"this format std::string is either missing its $0-$4, or "
|
629
|
+
"contains one of $5-$9");
|
630
|
+
|
631
|
+
std::string Substitute(const char* format, const substitute_internal::Arg& a0,
|
632
|
+
const substitute_internal::Arg& a1,
|
633
|
+
const substitute_internal::Arg& a2,
|
634
|
+
const substitute_internal::Arg& a3,
|
635
|
+
const substitute_internal::Arg& a4,
|
636
|
+
const substitute_internal::Arg& a5)
|
637
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 63,
|
638
|
+
"There were 6 substitution arguments given, but "
|
639
|
+
"this format std::string is either missing its $0-$5, or "
|
640
|
+
"contains one of $6-$9");
|
641
|
+
|
642
|
+
std::string Substitute(const char* format, const substitute_internal::Arg& a0,
|
643
|
+
const substitute_internal::Arg& a1,
|
644
|
+
const substitute_internal::Arg& a2,
|
645
|
+
const substitute_internal::Arg& a3,
|
646
|
+
const substitute_internal::Arg& a4,
|
647
|
+
const substitute_internal::Arg& a5,
|
648
|
+
const substitute_internal::Arg& a6)
|
649
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 127,
|
650
|
+
"There were 7 substitution arguments given, but "
|
651
|
+
"this format std::string is either missing its $0-$6, or "
|
652
|
+
"contains one of $7-$9");
|
653
|
+
|
654
|
+
std::string Substitute(const char* format, const substitute_internal::Arg& a0,
|
655
|
+
const substitute_internal::Arg& a1,
|
656
|
+
const substitute_internal::Arg& a2,
|
657
|
+
const substitute_internal::Arg& a3,
|
658
|
+
const substitute_internal::Arg& a4,
|
659
|
+
const substitute_internal::Arg& a5,
|
660
|
+
const substitute_internal::Arg& a6,
|
661
|
+
const substitute_internal::Arg& a7)
|
662
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 255,
|
663
|
+
"There were 8 substitution arguments given, but "
|
664
|
+
"this format std::string is either missing its $0-$7, or "
|
665
|
+
"contains one of $8-$9");
|
666
|
+
|
667
|
+
std::string Substitute(
|
668
|
+
const char* format, const substitute_internal::Arg& a0,
|
669
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
670
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
|
671
|
+
const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
|
672
|
+
const substitute_internal::Arg& a7, const substitute_internal::Arg& a8)
|
673
|
+
ABSL_BAD_CALL_IF(
|
674
|
+
substitute_internal::PlaceholderBitmask(format) != 511,
|
675
|
+
"There were 9 substitution arguments given, but "
|
676
|
+
"this format std::string is either missing its $0-$8, or contains a $9");
|
677
|
+
|
678
|
+
std::string Substitute(
|
679
|
+
const char* format, const substitute_internal::Arg& a0,
|
680
|
+
const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
|
681
|
+
const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
|
682
|
+
const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
|
683
|
+
const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
|
684
|
+
const substitute_internal::Arg& a9)
|
685
|
+
ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1023,
|
686
|
+
"There were 10 substitution arguments given, but this "
|
687
|
+
"format std::string doesn't contain all of $0 through $9");
|
688
|
+
#endif // ABSL_BAD_CALL_IF
|
689
|
+
|
690
|
+
ABSL_NAMESPACE_END
|
691
|
+
} // namespace absl
|
692
|
+
|
693
|
+
#endif // ABSL_STRINGS_SUBSTITUTE_H_
|