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,421 @@
|
|
1
|
+
// Copyright 2018 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
|
+
#ifndef ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_
|
16
|
+
#define ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_
|
17
|
+
|
18
|
+
#include <algorithm>
|
19
|
+
#include <cstdint>
|
20
|
+
#include <iostream>
|
21
|
+
#include <string>
|
22
|
+
|
23
|
+
#include "absl/strings/ascii.h"
|
24
|
+
#include "absl/strings/internal/charconv_parse.h"
|
25
|
+
#include "absl/strings/string_view.h"
|
26
|
+
|
27
|
+
namespace absl {
|
28
|
+
ABSL_NAMESPACE_BEGIN
|
29
|
+
namespace strings_internal {
|
30
|
+
|
31
|
+
// The largest power that 5 that can be raised to, and still fit in a uint32_t.
|
32
|
+
constexpr int kMaxSmallPowerOfFive = 13;
|
33
|
+
// The largest power that 10 that can be raised to, and still fit in a uint32_t.
|
34
|
+
constexpr int kMaxSmallPowerOfTen = 9;
|
35
|
+
|
36
|
+
extern const uint32_t kFiveToNth[kMaxSmallPowerOfFive + 1];
|
37
|
+
extern const uint32_t kTenToNth[kMaxSmallPowerOfTen + 1];
|
38
|
+
|
39
|
+
// Large, fixed-width unsigned integer.
|
40
|
+
//
|
41
|
+
// Exact rounding for decimal-to-binary floating point conversion requires very
|
42
|
+
// large integer math, but a design goal of absl::from_chars is to avoid
|
43
|
+
// allocating memory. The integer precision needed for decimal-to-binary
|
44
|
+
// conversions is large but bounded, so a huge fixed-width integer class
|
45
|
+
// suffices.
|
46
|
+
//
|
47
|
+
// This is an intentionally limited big integer class. Only needed operations
|
48
|
+
// are implemented. All storage lives in an array data member, and all
|
49
|
+
// arithmetic is done in-place, to avoid requiring separate storage for operand
|
50
|
+
// and result.
|
51
|
+
//
|
52
|
+
// This is an internal class. Some methods live in the .cc file, and are
|
53
|
+
// instantiated only for the values of max_words we need.
|
54
|
+
template <int max_words>
|
55
|
+
class BigUnsigned {
|
56
|
+
public:
|
57
|
+
static_assert(max_words == 4 || max_words == 84,
|
58
|
+
"unsupported max_words value");
|
59
|
+
|
60
|
+
BigUnsigned() : size_(0), words_{} {}
|
61
|
+
explicit constexpr BigUnsigned(uint64_t v)
|
62
|
+
: size_((v >> 32) ? 2 : v ? 1 : 0),
|
63
|
+
words_{static_cast<uint32_t>(v & 0xffffffffu),
|
64
|
+
static_cast<uint32_t>(v >> 32)} {}
|
65
|
+
|
66
|
+
// Constructs a BigUnsigned from the given string_view containing a decimal
|
67
|
+
// value. If the input std::string is not a decimal integer, constructs a 0
|
68
|
+
// instead.
|
69
|
+
explicit BigUnsigned(absl::string_view sv) : size_(0), words_{} {
|
70
|
+
// Check for valid input, returning a 0 otherwise. This is reasonable
|
71
|
+
// behavior only because this constructor is for unit tests.
|
72
|
+
if (std::find_if_not(sv.begin(), sv.end(), ascii_isdigit) != sv.end() ||
|
73
|
+
sv.empty()) {
|
74
|
+
return;
|
75
|
+
}
|
76
|
+
int exponent_adjust =
|
77
|
+
ReadDigits(sv.data(), sv.data() + sv.size(), Digits10() + 1);
|
78
|
+
if (exponent_adjust > 0) {
|
79
|
+
MultiplyByTenToTheNth(exponent_adjust);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
// Loads the mantissa value of a previously-parsed float.
|
84
|
+
//
|
85
|
+
// Returns the associated decimal exponent. The value of the parsed float is
|
86
|
+
// exactly *this * 10**exponent.
|
87
|
+
int ReadFloatMantissa(const ParsedFloat& fp, int significant_digits);
|
88
|
+
|
89
|
+
// Returns the number of decimal digits of precision this type provides. All
|
90
|
+
// numbers with this many decimal digits or fewer are representable by this
|
91
|
+
// type.
|
92
|
+
//
|
93
|
+
// Analagous to std::numeric_limits<BigUnsigned>::digits10.
|
94
|
+
static constexpr int Digits10() {
|
95
|
+
// 9975007/1035508 is very slightly less than log10(2**32).
|
96
|
+
return static_cast<uint64_t>(max_words) * 9975007 / 1035508;
|
97
|
+
}
|
98
|
+
|
99
|
+
// Shifts left by the given number of bits.
|
100
|
+
void ShiftLeft(int count) {
|
101
|
+
if (count > 0) {
|
102
|
+
const int word_shift = count / 32;
|
103
|
+
if (word_shift >= max_words) {
|
104
|
+
SetToZero();
|
105
|
+
return;
|
106
|
+
}
|
107
|
+
size_ = (std::min)(size_ + word_shift, max_words);
|
108
|
+
count %= 32;
|
109
|
+
if (count == 0) {
|
110
|
+
std::copy_backward(words_, words_ + size_ - word_shift, words_ + size_);
|
111
|
+
} else {
|
112
|
+
for (int i = (std::min)(size_, max_words - 1); i > word_shift; --i) {
|
113
|
+
words_[i] = (words_[i - word_shift] << count) |
|
114
|
+
(words_[i - word_shift - 1] >> (32 - count));
|
115
|
+
}
|
116
|
+
words_[word_shift] = words_[0] << count;
|
117
|
+
// Grow size_ if necessary.
|
118
|
+
if (size_ < max_words && words_[size_]) {
|
119
|
+
++size_;
|
120
|
+
}
|
121
|
+
}
|
122
|
+
std::fill(words_, words_ + word_shift, 0u);
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
|
127
|
+
// Multiplies by v in-place.
|
128
|
+
void MultiplyBy(uint32_t v) {
|
129
|
+
if (size_ == 0 || v == 1) {
|
130
|
+
return;
|
131
|
+
}
|
132
|
+
if (v == 0) {
|
133
|
+
SetToZero();
|
134
|
+
return;
|
135
|
+
}
|
136
|
+
const uint64_t factor = v;
|
137
|
+
uint64_t window = 0;
|
138
|
+
for (int i = 0; i < size_; ++i) {
|
139
|
+
window += factor * words_[i];
|
140
|
+
words_[i] = window & 0xffffffff;
|
141
|
+
window >>= 32;
|
142
|
+
}
|
143
|
+
// If carry bits remain and there's space for them, grow size_.
|
144
|
+
if (window && size_ < max_words) {
|
145
|
+
words_[size_] = window & 0xffffffff;
|
146
|
+
++size_;
|
147
|
+
}
|
148
|
+
}
|
149
|
+
|
150
|
+
void MultiplyBy(uint64_t v) {
|
151
|
+
uint32_t words[2];
|
152
|
+
words[0] = static_cast<uint32_t>(v);
|
153
|
+
words[1] = static_cast<uint32_t>(v >> 32);
|
154
|
+
if (words[1] == 0) {
|
155
|
+
MultiplyBy(words[0]);
|
156
|
+
} else {
|
157
|
+
MultiplyBy(2, words);
|
158
|
+
}
|
159
|
+
}
|
160
|
+
|
161
|
+
// Multiplies in place by 5 to the power of n. n must be non-negative.
|
162
|
+
void MultiplyByFiveToTheNth(int n) {
|
163
|
+
while (n >= kMaxSmallPowerOfFive) {
|
164
|
+
MultiplyBy(kFiveToNth[kMaxSmallPowerOfFive]);
|
165
|
+
n -= kMaxSmallPowerOfFive;
|
166
|
+
}
|
167
|
+
if (n > 0) {
|
168
|
+
MultiplyBy(kFiveToNth[n]);
|
169
|
+
}
|
170
|
+
}
|
171
|
+
|
172
|
+
// Multiplies in place by 10 to the power of n. n must be non-negative.
|
173
|
+
void MultiplyByTenToTheNth(int n) {
|
174
|
+
if (n > kMaxSmallPowerOfTen) {
|
175
|
+
// For large n, raise to a power of 5, then shift left by the same amount.
|
176
|
+
// (10**n == 5**n * 2**n.) This requires fewer multiplications overall.
|
177
|
+
MultiplyByFiveToTheNth(n);
|
178
|
+
ShiftLeft(n);
|
179
|
+
} else if (n > 0) {
|
180
|
+
// We can do this more quickly for very small N by using a single
|
181
|
+
// multiplication.
|
182
|
+
MultiplyBy(kTenToNth[n]);
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
// Returns the value of 5**n, for non-negative n. This implementation uses
|
187
|
+
// a lookup table, and is faster then seeding a BigUnsigned with 1 and calling
|
188
|
+
// MultiplyByFiveToTheNth().
|
189
|
+
static BigUnsigned FiveToTheNth(int n);
|
190
|
+
|
191
|
+
// Multiplies by another BigUnsigned, in-place.
|
192
|
+
template <int M>
|
193
|
+
void MultiplyBy(const BigUnsigned<M>& other) {
|
194
|
+
MultiplyBy(other.size(), other.words());
|
195
|
+
}
|
196
|
+
|
197
|
+
void SetToZero() {
|
198
|
+
std::fill(words_, words_ + size_, 0u);
|
199
|
+
size_ = 0;
|
200
|
+
}
|
201
|
+
|
202
|
+
// Returns the value of the nth word of this BigUnsigned. This is
|
203
|
+
// range-checked, and returns 0 on out-of-bounds accesses.
|
204
|
+
uint32_t GetWord(int index) const {
|
205
|
+
if (index < 0 || index >= size_) {
|
206
|
+
return 0;
|
207
|
+
}
|
208
|
+
return words_[index];
|
209
|
+
}
|
210
|
+
|
211
|
+
// Returns this integer as a decimal std::string. This is not used in the decimal-
|
212
|
+
// to-binary conversion; it is intended to aid in testing.
|
213
|
+
std::string ToString() const;
|
214
|
+
|
215
|
+
int size() const { return size_; }
|
216
|
+
const uint32_t* words() const { return words_; }
|
217
|
+
|
218
|
+
private:
|
219
|
+
// Reads the number between [begin, end), possibly containing a decimal point,
|
220
|
+
// into this BigUnsigned.
|
221
|
+
//
|
222
|
+
// Callers are required to ensure [begin, end) contains a valid number, with
|
223
|
+
// one or more decimal digits and at most one decimal point. This routine
|
224
|
+
// will behave unpredictably if these preconditions are not met.
|
225
|
+
//
|
226
|
+
// Only the first `significant_digits` digits are read. Digits beyond this
|
227
|
+
// limit are "sticky": If the final significant digit is 0 or 5, and if any
|
228
|
+
// dropped digit is nonzero, then that final significant digit is adjusted up
|
229
|
+
// to 1 or 6. This adjustment allows for precise rounding.
|
230
|
+
//
|
231
|
+
// Returns `exponent_adjustment`, a power-of-ten exponent adjustment to
|
232
|
+
// account for the decimal point and for dropped significant digits. After
|
233
|
+
// this function returns,
|
234
|
+
// actual_value_of_parsed_string ~= *this * 10**exponent_adjustment.
|
235
|
+
int ReadDigits(const char* begin, const char* end, int significant_digits);
|
236
|
+
|
237
|
+
// Performs a step of big integer multiplication. This computes the full
|
238
|
+
// (64-bit-wide) values that should be added at the given index (step), and
|
239
|
+
// adds to that location in-place.
|
240
|
+
//
|
241
|
+
// Because our math all occurs in place, we must multiply starting from the
|
242
|
+
// highest word working downward. (This is a bit more expensive due to the
|
243
|
+
// extra carries involved.)
|
244
|
+
//
|
245
|
+
// This must be called in steps, for each word to be calculated, starting from
|
246
|
+
// the high end and working down to 0. The first value of `step` should be
|
247
|
+
// `std::min(original_size + other.size_ - 2, max_words - 1)`.
|
248
|
+
// The reason for this expression is that multiplying the i'th word from one
|
249
|
+
// multiplicand and the j'th word of another multiplicand creates a
|
250
|
+
// two-word-wide value to be stored at the (i+j)'th element. The highest
|
251
|
+
// word indices we will access are `original_size - 1` from this object, and
|
252
|
+
// `other.size_ - 1` from our operand. Therefore,
|
253
|
+
// `original_size + other.size_ - 2` is the first step we should calculate,
|
254
|
+
// but limited on an upper bound by max_words.
|
255
|
+
|
256
|
+
// Working from high-to-low ensures that we do not overwrite the portions of
|
257
|
+
// the initial value of *this which are still needed for later steps.
|
258
|
+
//
|
259
|
+
// Once called with step == 0, *this contains the result of the
|
260
|
+
// multiplication.
|
261
|
+
//
|
262
|
+
// `original_size` is the size_ of *this before the first call to
|
263
|
+
// MultiplyStep(). `other_words` and `other_size` are the contents of our
|
264
|
+
// operand. `step` is the step to perform, as described above.
|
265
|
+
void MultiplyStep(int original_size, const uint32_t* other_words,
|
266
|
+
int other_size, int step);
|
267
|
+
|
268
|
+
void MultiplyBy(int other_size, const uint32_t* other_words) {
|
269
|
+
const int original_size = size_;
|
270
|
+
const int first_step =
|
271
|
+
(std::min)(original_size + other_size - 2, max_words - 1);
|
272
|
+
for (int step = first_step; step >= 0; --step) {
|
273
|
+
MultiplyStep(original_size, other_words, other_size, step);
|
274
|
+
}
|
275
|
+
}
|
276
|
+
|
277
|
+
// Adds a 32-bit value to the index'th word, with carry.
|
278
|
+
void AddWithCarry(int index, uint32_t value) {
|
279
|
+
if (value) {
|
280
|
+
while (index < max_words && value > 0) {
|
281
|
+
words_[index] += value;
|
282
|
+
// carry if we overflowed in this word:
|
283
|
+
if (value > words_[index]) {
|
284
|
+
value = 1;
|
285
|
+
++index;
|
286
|
+
} else {
|
287
|
+
value = 0;
|
288
|
+
}
|
289
|
+
}
|
290
|
+
size_ = (std::min)(max_words, (std::max)(index + 1, size_));
|
291
|
+
}
|
292
|
+
}
|
293
|
+
|
294
|
+
void AddWithCarry(int index, uint64_t value) {
|
295
|
+
if (value && index < max_words) {
|
296
|
+
uint32_t high = value >> 32;
|
297
|
+
uint32_t low = value & 0xffffffff;
|
298
|
+
words_[index] += low;
|
299
|
+
if (words_[index] < low) {
|
300
|
+
++high;
|
301
|
+
if (high == 0) {
|
302
|
+
// Carry from the low word caused our high word to overflow.
|
303
|
+
// Short circuit here to do the right thing.
|
304
|
+
AddWithCarry(index + 2, static_cast<uint32_t>(1));
|
305
|
+
return;
|
306
|
+
}
|
307
|
+
}
|
308
|
+
if (high > 0) {
|
309
|
+
AddWithCarry(index + 1, high);
|
310
|
+
} else {
|
311
|
+
// Normally 32-bit AddWithCarry() sets size_, but since we don't call
|
312
|
+
// it when `high` is 0, do it ourselves here.
|
313
|
+
size_ = (std::min)(max_words, (std::max)(index + 1, size_));
|
314
|
+
}
|
315
|
+
}
|
316
|
+
}
|
317
|
+
|
318
|
+
// Divide this in place by a constant divisor. Returns the remainder of the
|
319
|
+
// division.
|
320
|
+
template <uint32_t divisor>
|
321
|
+
uint32_t DivMod() {
|
322
|
+
uint64_t accumulator = 0;
|
323
|
+
for (int i = size_ - 1; i >= 0; --i) {
|
324
|
+
accumulator <<= 32;
|
325
|
+
accumulator += words_[i];
|
326
|
+
// accumulator / divisor will never overflow an int32_t in this loop
|
327
|
+
words_[i] = static_cast<uint32_t>(accumulator / divisor);
|
328
|
+
accumulator = accumulator % divisor;
|
329
|
+
}
|
330
|
+
while (size_ > 0 && words_[size_ - 1] == 0) {
|
331
|
+
--size_;
|
332
|
+
}
|
333
|
+
return static_cast<uint32_t>(accumulator);
|
334
|
+
}
|
335
|
+
|
336
|
+
// The number of elements in words_ that may carry significant values.
|
337
|
+
// All elements beyond this point are 0.
|
338
|
+
//
|
339
|
+
// When size_ is 0, this BigUnsigned stores the value 0.
|
340
|
+
// When size_ is nonzero, is *not* guaranteed that words_[size_ - 1] is
|
341
|
+
// nonzero. This can occur due to overflow truncation.
|
342
|
+
// In particular, x.size_ != y.size_ does *not* imply x != y.
|
343
|
+
int size_;
|
344
|
+
uint32_t words_[max_words];
|
345
|
+
};
|
346
|
+
|
347
|
+
// Compares two big integer instances.
|
348
|
+
//
|
349
|
+
// Returns -1 if lhs < rhs, 0 if lhs == rhs, and 1 if lhs > rhs.
|
350
|
+
template <int N, int M>
|
351
|
+
int Compare(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
|
352
|
+
int limit = (std::max)(lhs.size(), rhs.size());
|
353
|
+
for (int i = limit - 1; i >= 0; --i) {
|
354
|
+
const uint32_t lhs_word = lhs.GetWord(i);
|
355
|
+
const uint32_t rhs_word = rhs.GetWord(i);
|
356
|
+
if (lhs_word < rhs_word) {
|
357
|
+
return -1;
|
358
|
+
} else if (lhs_word > rhs_word) {
|
359
|
+
return 1;
|
360
|
+
}
|
361
|
+
}
|
362
|
+
return 0;
|
363
|
+
}
|
364
|
+
|
365
|
+
template <int N, int M>
|
366
|
+
bool operator==(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
|
367
|
+
int limit = (std::max)(lhs.size(), rhs.size());
|
368
|
+
for (int i = 0; i < limit; ++i) {
|
369
|
+
if (lhs.GetWord(i) != rhs.GetWord(i)) {
|
370
|
+
return false;
|
371
|
+
}
|
372
|
+
}
|
373
|
+
return true;
|
374
|
+
}
|
375
|
+
|
376
|
+
template <int N, int M>
|
377
|
+
bool operator!=(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
|
378
|
+
return !(lhs == rhs);
|
379
|
+
}
|
380
|
+
|
381
|
+
template <int N, int M>
|
382
|
+
bool operator<(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
|
383
|
+
return Compare(lhs, rhs) == -1;
|
384
|
+
}
|
385
|
+
|
386
|
+
template <int N, int M>
|
387
|
+
bool operator>(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
|
388
|
+
return rhs < lhs;
|
389
|
+
}
|
390
|
+
template <int N, int M>
|
391
|
+
bool operator<=(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
|
392
|
+
return !(rhs < lhs);
|
393
|
+
}
|
394
|
+
template <int N, int M>
|
395
|
+
bool operator>=(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
|
396
|
+
return !(lhs < rhs);
|
397
|
+
}
|
398
|
+
|
399
|
+
// Output operator for BigUnsigned, for testing purposes only.
|
400
|
+
template <int N>
|
401
|
+
std::ostream& operator<<(std::ostream& os, const BigUnsigned<N>& num) {
|
402
|
+
return os << num.ToString();
|
403
|
+
}
|
404
|
+
|
405
|
+
// Explicit instantiation declarations for the sizes of BigUnsigned that we
|
406
|
+
// are using.
|
407
|
+
//
|
408
|
+
// For now, the choices of 4 and 84 are arbitrary; 4 is a small value that is
|
409
|
+
// still bigger than an int128, and 84 is a large value we will want to use
|
410
|
+
// in the from_chars implementation.
|
411
|
+
//
|
412
|
+
// Comments justifying the use of 84 belong in the from_chars implementation,
|
413
|
+
// and will be added in a follow-up CL.
|
414
|
+
extern template class BigUnsigned<4>;
|
415
|
+
extern template class BigUnsigned<84>;
|
416
|
+
|
417
|
+
} // namespace strings_internal
|
418
|
+
ABSL_NAMESPACE_END
|
419
|
+
} // namespace absl
|
420
|
+
|
421
|
+
#endif // ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_
|
@@ -0,0 +1,504 @@
|
|
1
|
+
// Copyright 2018 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/internal/charconv_parse.h"
|
16
|
+
#include "absl/strings/charconv.h"
|
17
|
+
|
18
|
+
#include <cassert>
|
19
|
+
#include <cstdint>
|
20
|
+
#include <limits>
|
21
|
+
|
22
|
+
#include "absl/strings/internal/memutil.h"
|
23
|
+
|
24
|
+
namespace absl {
|
25
|
+
ABSL_NAMESPACE_BEGIN
|
26
|
+
namespace {
|
27
|
+
|
28
|
+
// ParseFloat<10> will read the first 19 significant digits of the mantissa.
|
29
|
+
// This number was chosen for multiple reasons.
|
30
|
+
//
|
31
|
+
// (a) First, for whatever integer type we choose to represent the mantissa, we
|
32
|
+
// want to choose the largest possible number of decimal digits for that integer
|
33
|
+
// type. We are using uint64_t, which can express any 19-digit unsigned
|
34
|
+
// integer.
|
35
|
+
//
|
36
|
+
// (b) Second, we need to parse enough digits that the binary value of any
|
37
|
+
// mantissa we capture has more bits of resolution than the mantissa
|
38
|
+
// representation in the target float. Our algorithm requires at least 3 bits
|
39
|
+
// of headway, but 19 decimal digits give a little more than that.
|
40
|
+
//
|
41
|
+
// The following static assertions verify the above comments:
|
42
|
+
constexpr int kDecimalMantissaDigitsMax = 19;
|
43
|
+
|
44
|
+
static_assert(std::numeric_limits<uint64_t>::digits10 ==
|
45
|
+
kDecimalMantissaDigitsMax,
|
46
|
+
"(a) above");
|
47
|
+
|
48
|
+
// IEEE doubles, which we assume in Abseil, have 53 binary bits of mantissa.
|
49
|
+
static_assert(std::numeric_limits<double>::is_iec559, "IEEE double assumed");
|
50
|
+
static_assert(std::numeric_limits<double>::radix == 2, "IEEE double fact");
|
51
|
+
static_assert(std::numeric_limits<double>::digits == 53, "IEEE double fact");
|
52
|
+
|
53
|
+
// The lowest valued 19-digit decimal mantissa we can read still contains
|
54
|
+
// sufficient information to reconstruct a binary mantissa.
|
55
|
+
static_assert(1000000000000000000u > (uint64_t(1) << (53 + 3)), "(b) above");
|
56
|
+
|
57
|
+
// ParseFloat<16> will read the first 15 significant digits of the mantissa.
|
58
|
+
//
|
59
|
+
// Because a base-16-to-base-2 conversion can be done exactly, we do not need
|
60
|
+
// to maximize the number of scanned hex digits to improve our conversion. What
|
61
|
+
// is required is to scan two more bits than the mantissa can represent, so that
|
62
|
+
// we always round correctly.
|
63
|
+
//
|
64
|
+
// (One extra bit does not suffice to perform correct rounding, since a number
|
65
|
+
// exactly halfway between two representable floats has unique rounding rules,
|
66
|
+
// so we need to differentiate between a "halfway between" number and a "closer
|
67
|
+
// to the larger value" number.)
|
68
|
+
constexpr int kHexadecimalMantissaDigitsMax = 15;
|
69
|
+
|
70
|
+
// The minimum number of significant bits that will be read from
|
71
|
+
// kHexadecimalMantissaDigitsMax hex digits. We must subtract by three, since
|
72
|
+
// the most significant digit can be a "1", which only contributes a single
|
73
|
+
// significant bit.
|
74
|
+
constexpr int kGuaranteedHexadecimalMantissaBitPrecision =
|
75
|
+
4 * kHexadecimalMantissaDigitsMax - 3;
|
76
|
+
|
77
|
+
static_assert(kGuaranteedHexadecimalMantissaBitPrecision >
|
78
|
+
std::numeric_limits<double>::digits + 2,
|
79
|
+
"kHexadecimalMantissaDigitsMax too small");
|
80
|
+
|
81
|
+
// We also impose a limit on the number of significant digits we will read from
|
82
|
+
// an exponent, to avoid having to deal with integer overflow. We use 9 for
|
83
|
+
// this purpose.
|
84
|
+
//
|
85
|
+
// If we read a 9 digit exponent, the end result of the conversion will
|
86
|
+
// necessarily be infinity or zero, depending on the sign of the exponent.
|
87
|
+
// Therefore we can just drop extra digits on the floor without any extra
|
88
|
+
// logic.
|
89
|
+
constexpr int kDecimalExponentDigitsMax = 9;
|
90
|
+
static_assert(std::numeric_limits<int>::digits10 >= kDecimalExponentDigitsMax,
|
91
|
+
"int type too small");
|
92
|
+
|
93
|
+
// To avoid incredibly large inputs causing integer overflow for our exponent,
|
94
|
+
// we impose an arbitrary but very large limit on the number of significant
|
95
|
+
// digits we will accept. The implementation refuses to match a string with
|
96
|
+
// more consecutive significant mantissa digits than this.
|
97
|
+
constexpr int kDecimalDigitLimit = 50000000;
|
98
|
+
|
99
|
+
// Corresponding limit for hexadecimal digit inputs. This is one fourth the
|
100
|
+
// amount of kDecimalDigitLimit, since each dropped hexadecimal digit requires
|
101
|
+
// a binary exponent adjustment of 4.
|
102
|
+
constexpr int kHexadecimalDigitLimit = kDecimalDigitLimit / 4;
|
103
|
+
|
104
|
+
// The largest exponent we can read is 999999999 (per
|
105
|
+
// kDecimalExponentDigitsMax), and the largest exponent adjustment we can get
|
106
|
+
// from dropped mantissa digits is 2 * kDecimalDigitLimit, and the sum of these
|
107
|
+
// comfortably fits in an integer.
|
108
|
+
//
|
109
|
+
// We count kDecimalDigitLimit twice because there are independent limits for
|
110
|
+
// numbers before and after the decimal point. (In the case where there are no
|
111
|
+
// significant digits before the decimal point, there are independent limits for
|
112
|
+
// post-decimal-point leading zeroes and for significant digits.)
|
113
|
+
static_assert(999999999 + 2 * kDecimalDigitLimit <
|
114
|
+
std::numeric_limits<int>::max(),
|
115
|
+
"int type too small");
|
116
|
+
static_assert(999999999 + 2 * (4 * kHexadecimalDigitLimit) <
|
117
|
+
std::numeric_limits<int>::max(),
|
118
|
+
"int type too small");
|
119
|
+
|
120
|
+
// Returns true if the provided bitfield allows parsing an exponent value
|
121
|
+
// (e.g., "1.5e100").
|
122
|
+
bool AllowExponent(chars_format flags) {
|
123
|
+
bool fixed = (flags & chars_format::fixed) == chars_format::fixed;
|
124
|
+
bool scientific =
|
125
|
+
(flags & chars_format::scientific) == chars_format::scientific;
|
126
|
+
return scientific || !fixed;
|
127
|
+
}
|
128
|
+
|
129
|
+
// Returns true if the provided bitfield requires an exponent value be present.
|
130
|
+
bool RequireExponent(chars_format flags) {
|
131
|
+
bool fixed = (flags & chars_format::fixed) == chars_format::fixed;
|
132
|
+
bool scientific =
|
133
|
+
(flags & chars_format::scientific) == chars_format::scientific;
|
134
|
+
return scientific && !fixed;
|
135
|
+
}
|
136
|
+
|
137
|
+
const int8_t kAsciiToInt[256] = {
|
138
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
139
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
140
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
|
141
|
+
9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1,
|
142
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
143
|
+
-1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
144
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
145
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
146
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
147
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
148
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
149
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
150
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
151
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1};
|
152
|
+
|
153
|
+
// Returns true if `ch` is a digit in the given base
|
154
|
+
template <int base>
|
155
|
+
bool IsDigit(char ch);
|
156
|
+
|
157
|
+
// Converts a valid `ch` to its digit value in the given base.
|
158
|
+
template <int base>
|
159
|
+
unsigned ToDigit(char ch);
|
160
|
+
|
161
|
+
// Returns true if `ch` is the exponent delimiter for the given base.
|
162
|
+
template <int base>
|
163
|
+
bool IsExponentCharacter(char ch);
|
164
|
+
|
165
|
+
// Returns the maximum number of significant digits we will read for a float
|
166
|
+
// in the given base.
|
167
|
+
template <int base>
|
168
|
+
constexpr int MantissaDigitsMax();
|
169
|
+
|
170
|
+
// Returns the largest consecutive run of digits we will accept when parsing a
|
171
|
+
// number in the given base.
|
172
|
+
template <int base>
|
173
|
+
constexpr int DigitLimit();
|
174
|
+
|
175
|
+
// Returns the amount the exponent must be adjusted by for each dropped digit.
|
176
|
+
// (For decimal this is 1, since the digits are in base 10 and the exponent base
|
177
|
+
// is also 10, but for hexadecimal this is 4, since the digits are base 16 but
|
178
|
+
// the exponent base is 2.)
|
179
|
+
template <int base>
|
180
|
+
constexpr int DigitMagnitude();
|
181
|
+
|
182
|
+
template <>
|
183
|
+
bool IsDigit<10>(char ch) {
|
184
|
+
return ch >= '0' && ch <= '9';
|
185
|
+
}
|
186
|
+
template <>
|
187
|
+
bool IsDigit<16>(char ch) {
|
188
|
+
return kAsciiToInt[static_cast<unsigned char>(ch)] >= 0;
|
189
|
+
}
|
190
|
+
|
191
|
+
template <>
|
192
|
+
unsigned ToDigit<10>(char ch) {
|
193
|
+
return ch - '0';
|
194
|
+
}
|
195
|
+
template <>
|
196
|
+
unsigned ToDigit<16>(char ch) {
|
197
|
+
return kAsciiToInt[static_cast<unsigned char>(ch)];
|
198
|
+
}
|
199
|
+
|
200
|
+
template <>
|
201
|
+
bool IsExponentCharacter<10>(char ch) {
|
202
|
+
return ch == 'e' || ch == 'E';
|
203
|
+
}
|
204
|
+
|
205
|
+
template <>
|
206
|
+
bool IsExponentCharacter<16>(char ch) {
|
207
|
+
return ch == 'p' || ch == 'P';
|
208
|
+
}
|
209
|
+
|
210
|
+
template <>
|
211
|
+
constexpr int MantissaDigitsMax<10>() {
|
212
|
+
return kDecimalMantissaDigitsMax;
|
213
|
+
}
|
214
|
+
template <>
|
215
|
+
constexpr int MantissaDigitsMax<16>() {
|
216
|
+
return kHexadecimalMantissaDigitsMax;
|
217
|
+
}
|
218
|
+
|
219
|
+
template <>
|
220
|
+
constexpr int DigitLimit<10>() {
|
221
|
+
return kDecimalDigitLimit;
|
222
|
+
}
|
223
|
+
template <>
|
224
|
+
constexpr int DigitLimit<16>() {
|
225
|
+
return kHexadecimalDigitLimit;
|
226
|
+
}
|
227
|
+
|
228
|
+
template <>
|
229
|
+
constexpr int DigitMagnitude<10>() {
|
230
|
+
return 1;
|
231
|
+
}
|
232
|
+
template <>
|
233
|
+
constexpr int DigitMagnitude<16>() {
|
234
|
+
return 4;
|
235
|
+
}
|
236
|
+
|
237
|
+
// Reads decimal digits from [begin, end) into *out. Returns the number of
|
238
|
+
// digits consumed.
|
239
|
+
//
|
240
|
+
// After max_digits has been read, keeps consuming characters, but no longer
|
241
|
+
// adjusts *out. If a nonzero digit is dropped this way, *dropped_nonzero_digit
|
242
|
+
// is set; otherwise, it is left unmodified.
|
243
|
+
//
|
244
|
+
// If no digits are matched, returns 0 and leaves *out unchanged.
|
245
|
+
//
|
246
|
+
// ConsumeDigits does not protect against overflow on *out; max_digits must
|
247
|
+
// be chosen with respect to type T to avoid the possibility of overflow.
|
248
|
+
template <int base, typename T>
|
249
|
+
std::size_t ConsumeDigits(const char* begin, const char* end, int max_digits,
|
250
|
+
T* out, bool* dropped_nonzero_digit) {
|
251
|
+
if (base == 10) {
|
252
|
+
assert(max_digits <= std::numeric_limits<T>::digits10);
|
253
|
+
} else if (base == 16) {
|
254
|
+
assert(max_digits * 4 <= std::numeric_limits<T>::digits);
|
255
|
+
}
|
256
|
+
const char* const original_begin = begin;
|
257
|
+
|
258
|
+
// Skip leading zeros, but only if *out is zero.
|
259
|
+
// They don't cause an overflow so we don't have to count them for
|
260
|
+
// `max_digits`.
|
261
|
+
while (!*out && end != begin && *begin == '0') ++begin;
|
262
|
+
|
263
|
+
T accumulator = *out;
|
264
|
+
const char* significant_digits_end =
|
265
|
+
(end - begin > max_digits) ? begin + max_digits : end;
|
266
|
+
while (begin < significant_digits_end && IsDigit<base>(*begin)) {
|
267
|
+
// Do not guard against *out overflow; max_digits was chosen to avoid this.
|
268
|
+
// Do assert against it, to detect problems in debug builds.
|
269
|
+
auto digit = static_cast<T>(ToDigit<base>(*begin));
|
270
|
+
assert(accumulator * base >= accumulator);
|
271
|
+
accumulator *= base;
|
272
|
+
assert(accumulator + digit >= accumulator);
|
273
|
+
accumulator += digit;
|
274
|
+
++begin;
|
275
|
+
}
|
276
|
+
bool dropped_nonzero = false;
|
277
|
+
while (begin < end && IsDigit<base>(*begin)) {
|
278
|
+
dropped_nonzero = dropped_nonzero || (*begin != '0');
|
279
|
+
++begin;
|
280
|
+
}
|
281
|
+
if (dropped_nonzero && dropped_nonzero_digit != nullptr) {
|
282
|
+
*dropped_nonzero_digit = true;
|
283
|
+
}
|
284
|
+
*out = accumulator;
|
285
|
+
return begin - original_begin;
|
286
|
+
}
|
287
|
+
|
288
|
+
// Returns true if `v` is one of the chars allowed inside parentheses following
|
289
|
+
// a NaN.
|
290
|
+
bool IsNanChar(char v) {
|
291
|
+
return (v == '_') || (v >= '0' && v <= '9') || (v >= 'a' && v <= 'z') ||
|
292
|
+
(v >= 'A' && v <= 'Z');
|
293
|
+
}
|
294
|
+
|
295
|
+
// Checks the range [begin, end) for a strtod()-formatted infinity or NaN. If
|
296
|
+
// one is found, sets `out` appropriately and returns true.
|
297
|
+
bool ParseInfinityOrNan(const char* begin, const char* end,
|
298
|
+
strings_internal::ParsedFloat* out) {
|
299
|
+
if (end - begin < 3) {
|
300
|
+
return false;
|
301
|
+
}
|
302
|
+
switch (*begin) {
|
303
|
+
case 'i':
|
304
|
+
case 'I': {
|
305
|
+
// An infinity std::string consists of the characters "inf" or "infinity",
|
306
|
+
// case insensitive.
|
307
|
+
if (strings_internal::memcasecmp(begin + 1, "nf", 2) != 0) {
|
308
|
+
return false;
|
309
|
+
}
|
310
|
+
out->type = strings_internal::FloatType::kInfinity;
|
311
|
+
if (end - begin >= 8 &&
|
312
|
+
strings_internal::memcasecmp(begin + 3, "inity", 5) == 0) {
|
313
|
+
out->end = begin + 8;
|
314
|
+
} else {
|
315
|
+
out->end = begin + 3;
|
316
|
+
}
|
317
|
+
return true;
|
318
|
+
}
|
319
|
+
case 'n':
|
320
|
+
case 'N': {
|
321
|
+
// A NaN consists of the characters "nan", case insensitive, optionally
|
322
|
+
// followed by a parenthesized sequence of zero or more alphanumeric
|
323
|
+
// characters and/or underscores.
|
324
|
+
if (strings_internal::memcasecmp(begin + 1, "an", 2) != 0) {
|
325
|
+
return false;
|
326
|
+
}
|
327
|
+
out->type = strings_internal::FloatType::kNan;
|
328
|
+
out->end = begin + 3;
|
329
|
+
// NaN is allowed to be followed by a parenthesized std::string, consisting of
|
330
|
+
// only the characters [a-zA-Z0-9_]. Match that if it's present.
|
331
|
+
begin += 3;
|
332
|
+
if (begin < end && *begin == '(') {
|
333
|
+
const char* nan_begin = begin + 1;
|
334
|
+
while (nan_begin < end && IsNanChar(*nan_begin)) {
|
335
|
+
++nan_begin;
|
336
|
+
}
|
337
|
+
if (nan_begin < end && *nan_begin == ')') {
|
338
|
+
// We found an extra NaN specifier range
|
339
|
+
out->subrange_begin = begin + 1;
|
340
|
+
out->subrange_end = nan_begin;
|
341
|
+
out->end = nan_begin + 1;
|
342
|
+
}
|
343
|
+
}
|
344
|
+
return true;
|
345
|
+
}
|
346
|
+
default:
|
347
|
+
return false;
|
348
|
+
}
|
349
|
+
}
|
350
|
+
} // namespace
|
351
|
+
|
352
|
+
namespace strings_internal {
|
353
|
+
|
354
|
+
template <int base>
|
355
|
+
strings_internal::ParsedFloat ParseFloat(const char* begin, const char* end,
|
356
|
+
chars_format format_flags) {
|
357
|
+
strings_internal::ParsedFloat result;
|
358
|
+
|
359
|
+
// Exit early if we're given an empty range.
|
360
|
+
if (begin == end) return result;
|
361
|
+
|
362
|
+
// Handle the infinity and NaN cases.
|
363
|
+
if (ParseInfinityOrNan(begin, end, &result)) {
|
364
|
+
return result;
|
365
|
+
}
|
366
|
+
|
367
|
+
const char* const mantissa_begin = begin;
|
368
|
+
while (begin < end && *begin == '0') {
|
369
|
+
++begin; // skip leading zeros
|
370
|
+
}
|
371
|
+
uint64_t mantissa = 0;
|
372
|
+
|
373
|
+
int exponent_adjustment = 0;
|
374
|
+
bool mantissa_is_inexact = false;
|
375
|
+
std::size_t pre_decimal_digits = ConsumeDigits<base>(
|
376
|
+
begin, end, MantissaDigitsMax<base>(), &mantissa, &mantissa_is_inexact);
|
377
|
+
begin += pre_decimal_digits;
|
378
|
+
int digits_left;
|
379
|
+
if (pre_decimal_digits >= DigitLimit<base>()) {
|
380
|
+
// refuse to parse pathological inputs
|
381
|
+
return result;
|
382
|
+
} else if (pre_decimal_digits > MantissaDigitsMax<base>()) {
|
383
|
+
// We dropped some non-fraction digits on the floor. Adjust our exponent
|
384
|
+
// to compensate.
|
385
|
+
exponent_adjustment =
|
386
|
+
static_cast<int>(pre_decimal_digits - MantissaDigitsMax<base>());
|
387
|
+
digits_left = 0;
|
388
|
+
} else {
|
389
|
+
digits_left =
|
390
|
+
static_cast<int>(MantissaDigitsMax<base>() - pre_decimal_digits);
|
391
|
+
}
|
392
|
+
if (begin < end && *begin == '.') {
|
393
|
+
++begin;
|
394
|
+
if (mantissa == 0) {
|
395
|
+
// If we haven't seen any nonzero digits yet, keep skipping zeros. We
|
396
|
+
// have to adjust the exponent to reflect the changed place value.
|
397
|
+
const char* begin_zeros = begin;
|
398
|
+
while (begin < end && *begin == '0') {
|
399
|
+
++begin;
|
400
|
+
}
|
401
|
+
std::size_t zeros_skipped = begin - begin_zeros;
|
402
|
+
if (zeros_skipped >= DigitLimit<base>()) {
|
403
|
+
// refuse to parse pathological inputs
|
404
|
+
return result;
|
405
|
+
}
|
406
|
+
exponent_adjustment -= static_cast<int>(zeros_skipped);
|
407
|
+
}
|
408
|
+
std::size_t post_decimal_digits = ConsumeDigits<base>(
|
409
|
+
begin, end, digits_left, &mantissa, &mantissa_is_inexact);
|
410
|
+
begin += post_decimal_digits;
|
411
|
+
|
412
|
+
// Since `mantissa` is an integer, each significant digit we read after
|
413
|
+
// the decimal point requires an adjustment to the exponent. "1.23e0" will
|
414
|
+
// be stored as `mantissa` == 123 and `exponent` == -2 (that is,
|
415
|
+
// "123e-2").
|
416
|
+
if (post_decimal_digits >= DigitLimit<base>()) {
|
417
|
+
// refuse to parse pathological inputs
|
418
|
+
return result;
|
419
|
+
} else if (post_decimal_digits > digits_left) {
|
420
|
+
exponent_adjustment -= digits_left;
|
421
|
+
} else {
|
422
|
+
exponent_adjustment -= post_decimal_digits;
|
423
|
+
}
|
424
|
+
}
|
425
|
+
// If we've found no mantissa whatsoever, this isn't a number.
|
426
|
+
if (mantissa_begin == begin) {
|
427
|
+
return result;
|
428
|
+
}
|
429
|
+
// A bare "." doesn't count as a mantissa either.
|
430
|
+
if (begin - mantissa_begin == 1 && *mantissa_begin == '.') {
|
431
|
+
return result;
|
432
|
+
}
|
433
|
+
|
434
|
+
if (mantissa_is_inexact) {
|
435
|
+
// We dropped significant digits on the floor. Handle this appropriately.
|
436
|
+
if (base == 10) {
|
437
|
+
// If we truncated significant decimal digits, store the full range of the
|
438
|
+
// mantissa for future big integer math for exact rounding.
|
439
|
+
result.subrange_begin = mantissa_begin;
|
440
|
+
result.subrange_end = begin;
|
441
|
+
} else if (base == 16) {
|
442
|
+
// If we truncated hex digits, reflect this fact by setting the low
|
443
|
+
// ("sticky") bit. This allows for correct rounding in all cases.
|
444
|
+
mantissa |= 1;
|
445
|
+
}
|
446
|
+
}
|
447
|
+
result.mantissa = mantissa;
|
448
|
+
|
449
|
+
const char* const exponent_begin = begin;
|
450
|
+
result.literal_exponent = 0;
|
451
|
+
bool found_exponent = false;
|
452
|
+
if (AllowExponent(format_flags) && begin < end &&
|
453
|
+
IsExponentCharacter<base>(*begin)) {
|
454
|
+
bool negative_exponent = false;
|
455
|
+
++begin;
|
456
|
+
if (begin < end && *begin == '-') {
|
457
|
+
negative_exponent = true;
|
458
|
+
++begin;
|
459
|
+
} else if (begin < end && *begin == '+') {
|
460
|
+
++begin;
|
461
|
+
}
|
462
|
+
const char* const exponent_digits_begin = begin;
|
463
|
+
// Exponent is always expressed in decimal, even for hexadecimal floats.
|
464
|
+
begin += ConsumeDigits<10>(begin, end, kDecimalExponentDigitsMax,
|
465
|
+
&result.literal_exponent, nullptr);
|
466
|
+
if (begin == exponent_digits_begin) {
|
467
|
+
// there were no digits where we expected an exponent. We failed to read
|
468
|
+
// an exponent and should not consume the 'e' after all. Rewind 'begin'.
|
469
|
+
found_exponent = false;
|
470
|
+
begin = exponent_begin;
|
471
|
+
} else {
|
472
|
+
found_exponent = true;
|
473
|
+
if (negative_exponent) {
|
474
|
+
result.literal_exponent = -result.literal_exponent;
|
475
|
+
}
|
476
|
+
}
|
477
|
+
}
|
478
|
+
|
479
|
+
if (!found_exponent && RequireExponent(format_flags)) {
|
480
|
+
// Provided flags required an exponent, but none was found. This results
|
481
|
+
// in a failure to scan.
|
482
|
+
return result;
|
483
|
+
}
|
484
|
+
|
485
|
+
// Success!
|
486
|
+
result.type = strings_internal::FloatType::kNumber;
|
487
|
+
if (result.mantissa > 0) {
|
488
|
+
result.exponent = result.literal_exponent +
|
489
|
+
(DigitMagnitude<base>() * exponent_adjustment);
|
490
|
+
} else {
|
491
|
+
result.exponent = 0;
|
492
|
+
}
|
493
|
+
result.end = begin;
|
494
|
+
return result;
|
495
|
+
}
|
496
|
+
|
497
|
+
template ParsedFloat ParseFloat<10>(const char* begin, const char* end,
|
498
|
+
chars_format format_flags);
|
499
|
+
template ParsedFloat ParseFloat<16>(const char* begin, const char* end,
|
500
|
+
chars_format format_flags);
|
501
|
+
|
502
|
+
} // namespace strings_internal
|
503
|
+
ABSL_NAMESPACE_END
|
504
|
+
} // namespace absl
|