grpc 1.26.0 → 1.27.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1654 -1519
  3. data/etc/roots.pem +44 -0
  4. data/include/grpc/grpc_security.h +37 -15
  5. data/include/grpc/grpc_security_constants.h +27 -0
  6. data/include/grpc/impl/codegen/grpc_types.h +14 -0
  7. data/include/grpc/impl/codegen/port_platform.h +1 -1
  8. data/src/core/ext/filters/client_channel/client_channel.cc +0 -20
  9. data/src/core/ext/filters/client_channel/http_proxy.cc +4 -4
  10. data/src/core/ext/filters/client_channel/lb_policy.cc +4 -3
  11. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +191 -201
  12. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.cc +89 -0
  13. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h +40 -0
  14. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +3 -2
  15. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +88 -121
  16. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +28 -57
  17. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +0 -7
  18. data/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +8 -9
  19. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +53 -34
  20. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +18 -5
  21. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +24 -19
  22. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +2 -1
  23. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +4 -2
  24. data/src/core/ext/filters/client_channel/server_address.cc +6 -9
  25. data/src/core/ext/filters/client_channel/server_address.h +3 -10
  26. data/src/core/ext/filters/client_channel/xds/xds_api.cc +394 -150
  27. data/src/core/ext/filters/client_channel/xds/xds_api.h +75 -35
  28. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc +59 -22
  29. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.h +13 -9
  30. data/src/core/ext/filters/client_channel/xds/xds_channel_secure.cc +8 -6
  31. data/src/core/ext/filters/client_channel/xds/xds_client.cc +456 -175
  32. data/src/core/ext/filters/client_channel/xds/xds_client.h +33 -21
  33. data/src/core/ext/filters/client_channel/xds/xds_client_stats.cc +5 -8
  34. data/src/core/ext/filters/client_channel/xds/xds_client_stats.h +18 -24
  35. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +2 -2
  36. data/src/core/ext/upb-generated/src/proto/grpc/lb/v1/load_balancer.upb.c +13 -5
  37. data/src/core/ext/upb-generated/src/proto/grpc/lb/v1/load_balancer.upb.h +34 -0
  38. data/src/core/lib/channel/channelz.h +11 -1
  39. data/src/core/lib/gpr/time_precise.cc +1 -1
  40. data/src/core/lib/gprpp/optional.h +26 -0
  41. data/src/core/lib/gprpp/string_view.h +14 -10
  42. data/src/core/lib/iomgr/executor.cc +1 -1
  43. data/src/core/lib/iomgr/fork_posix.cc +4 -0
  44. data/src/core/lib/iomgr/poller/eventmanager_libuv.cc +87 -0
  45. data/src/core/lib/iomgr/poller/eventmanager_libuv.h +88 -0
  46. data/src/core/lib/iomgr/socket_utils_common_posix.cc +14 -0
  47. data/src/core/lib/iomgr/socket_utils_posix.h +12 -0
  48. data/src/core/lib/iomgr/tcp_custom.h +3 -0
  49. data/src/core/lib/iomgr/tcp_posix.cc +607 -56
  50. data/src/core/lib/iomgr/tcp_server_custom.cc +15 -2
  51. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +8 -0
  52. data/src/core/lib/json/json.h +11 -1
  53. data/src/core/lib/json/json_reader.cc +206 -28
  54. data/src/core/lib/json/json_writer.cc +111 -24
  55. data/src/core/lib/security/credentials/composite/composite_credentials.cc +7 -0
  56. data/src/core/lib/security/credentials/composite/composite_credentials.h +5 -1
  57. data/src/core/lib/security/credentials/credentials.h +10 -1
  58. data/src/core/lib/security/credentials/fake/fake_credentials.h +2 -1
  59. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +1 -1
  60. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +6 -4
  61. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -1
  62. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.cc +20 -0
  63. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +8 -0
  64. data/src/core/lib/security/credentials/tls/{spiffe_credentials.cc → tls_credentials.cc} +23 -24
  65. data/src/core/lib/security/credentials/tls/{spiffe_credentials.h → tls_credentials.h} +9 -9
  66. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +13 -0
  67. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +22 -2
  68. data/src/core/lib/security/security_connector/load_system_roots_fallback.cc +2 -2
  69. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +2 -2
  70. data/src/core/lib/security/security_connector/local/local_security_connector.cc +30 -3
  71. data/src/core/lib/security/security_connector/ssl_utils.cc +45 -3
  72. data/src/core/lib/security/security_connector/ssl_utils.h +12 -0
  73. data/src/core/lib/security/security_connector/tls/{spiffe_security_connector.cc → tls_security_connector.cc} +82 -69
  74. data/src/core/lib/security/security_connector/tls/{spiffe_security_connector.h → tls_security_connector.h} +17 -18
  75. data/src/core/lib/security/transport/client_auth_filter.cc +33 -0
  76. data/src/core/lib/surface/completion_queue.cc +22 -1
  77. data/src/core/lib/surface/version.cc +1 -1
  78. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +11 -1
  79. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.h +1 -1
  80. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +3 -3
  81. data/src/core/tsi/fake_transport_security.cc +7 -3
  82. data/src/core/tsi/fake_transport_security.h +2 -0
  83. data/src/core/tsi/ssl_transport_security.cc +144 -8
  84. data/src/core/tsi/ssl_transport_security.h +15 -1
  85. data/src/core/tsi/transport_security.cc +13 -0
  86. data/src/core/tsi/transport_security_grpc.cc +2 -2
  87. data/src/core/tsi/transport_security_grpc.h +2 -2
  88. data/src/core/tsi/transport_security_interface.h +12 -0
  89. data/src/ruby/bin/math_pb.rb +5 -5
  90. data/src/ruby/ext/grpc/rb_call_credentials.c +4 -1
  91. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +2 -0
  92. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +4 -1
  93. data/src/ruby/lib/grpc/version.rb +1 -1
  94. data/src/ruby/pb/grpc/health/v1/health_pb.rb +3 -3
  95. data/src/ruby/pb/src/proto/grpc/testing/empty_pb.rb +1 -1
  96. data/src/ruby/pb/src/proto/grpc/testing/messages_pb.rb +23 -13
  97. data/third_party/abseil-cpp/absl/algorithm/algorithm.h +159 -0
  98. data/third_party/abseil-cpp/absl/base/attributes.h +609 -0
  99. data/third_party/abseil-cpp/absl/base/call_once.h +226 -0
  100. data/third_party/abseil-cpp/absl/base/casts.h +184 -0
  101. data/third_party/abseil-cpp/absl/base/config.h +622 -0
  102. data/third_party/abseil-cpp/absl/base/const_init.h +76 -0
  103. data/third_party/abseil-cpp/absl/base/dynamic_annotations.cc +129 -0
  104. data/third_party/abseil-cpp/absl/base/dynamic_annotations.h +389 -0
  105. data/third_party/abseil-cpp/absl/base/internal/atomic_hook.h +179 -0
  106. data/third_party/abseil-cpp/absl/base/internal/bits.h +218 -0
  107. data/third_party/abseil-cpp/absl/base/internal/cycleclock.cc +107 -0
  108. data/third_party/abseil-cpp/absl/base/internal/cycleclock.h +94 -0
  109. data/third_party/abseil-cpp/absl/base/internal/endian.h +266 -0
  110. data/third_party/abseil-cpp/absl/base/internal/hide_ptr.h +51 -0
  111. data/third_party/abseil-cpp/absl/base/internal/identity.h +37 -0
  112. data/third_party/abseil-cpp/absl/base/internal/inline_variable.h +107 -0
  113. data/third_party/abseil-cpp/absl/base/internal/invoke.h +187 -0
  114. data/third_party/abseil-cpp/absl/base/internal/low_level_scheduling.h +107 -0
  115. data/third_party/abseil-cpp/absl/base/internal/per_thread_tls.h +52 -0
  116. data/third_party/abseil-cpp/absl/base/internal/raw_logging.cc +237 -0
  117. data/third_party/abseil-cpp/absl/base/internal/raw_logging.h +179 -0
  118. data/third_party/abseil-cpp/absl/base/internal/scheduling_mode.h +58 -0
  119. data/third_party/abseil-cpp/absl/base/internal/spinlock.cc +233 -0
  120. data/third_party/abseil-cpp/absl/base/internal/spinlock.h +243 -0
  121. data/third_party/abseil-cpp/absl/base/internal/spinlock_akaros.inc +35 -0
  122. data/third_party/abseil-cpp/absl/base/internal/spinlock_linux.inc +67 -0
  123. data/third_party/abseil-cpp/absl/base/internal/spinlock_posix.inc +46 -0
  124. data/third_party/abseil-cpp/absl/base/internal/spinlock_wait.cc +81 -0
  125. data/third_party/abseil-cpp/absl/base/internal/spinlock_wait.h +93 -0
  126. data/third_party/abseil-cpp/absl/base/internal/spinlock_win32.inc +37 -0
  127. data/third_party/abseil-cpp/absl/base/internal/sysinfo.cc +414 -0
  128. data/third_party/abseil-cpp/absl/base/internal/sysinfo.h +66 -0
  129. data/third_party/abseil-cpp/absl/base/internal/thread_annotations.h +271 -0
  130. data/third_party/abseil-cpp/absl/base/internal/thread_identity.cc +140 -0
  131. data/third_party/abseil-cpp/absl/base/internal/thread_identity.h +250 -0
  132. data/third_party/abseil-cpp/absl/base/internal/throw_delegate.cc +108 -0
  133. data/third_party/abseil-cpp/absl/base/internal/throw_delegate.h +75 -0
  134. data/third_party/abseil-cpp/absl/base/internal/tsan_mutex_interface.h +66 -0
  135. data/third_party/abseil-cpp/absl/base/internal/unaligned_access.h +158 -0
  136. data/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.cc +103 -0
  137. data/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.h +124 -0
  138. data/third_party/abseil-cpp/absl/base/log_severity.cc +27 -0
  139. data/third_party/abseil-cpp/absl/base/log_severity.h +121 -0
  140. data/third_party/abseil-cpp/absl/base/macros.h +220 -0
  141. data/third_party/abseil-cpp/absl/base/optimization.h +181 -0
  142. data/third_party/abseil-cpp/absl/base/options.h +214 -0
  143. data/third_party/abseil-cpp/absl/base/policy_checks.h +111 -0
  144. data/third_party/abseil-cpp/absl/base/port.h +26 -0
  145. data/third_party/abseil-cpp/absl/base/thread_annotations.h +280 -0
  146. data/third_party/abseil-cpp/absl/container/inlined_vector.h +848 -0
  147. data/third_party/abseil-cpp/absl/container/internal/compressed_tuple.h +265 -0
  148. data/third_party/abseil-cpp/absl/container/internal/inlined_vector.h +892 -0
  149. data/third_party/abseil-cpp/absl/memory/memory.h +695 -0
  150. data/third_party/abseil-cpp/absl/meta/type_traits.h +759 -0
  151. data/third_party/abseil-cpp/absl/numeric/int128.cc +404 -0
  152. data/third_party/abseil-cpp/absl/numeric/int128.h +1091 -0
  153. data/third_party/abseil-cpp/absl/numeric/int128_have_intrinsic.inc +302 -0
  154. data/third_party/abseil-cpp/absl/numeric/int128_no_intrinsic.inc +308 -0
  155. data/third_party/abseil-cpp/absl/strings/ascii.cc +200 -0
  156. data/third_party/abseil-cpp/absl/strings/ascii.h +241 -0
  157. data/third_party/abseil-cpp/absl/strings/charconv.cc +985 -0
  158. data/third_party/abseil-cpp/absl/strings/charconv.h +119 -0
  159. data/third_party/abseil-cpp/absl/strings/escaping.cc +949 -0
  160. data/third_party/abseil-cpp/absl/strings/escaping.h +164 -0
  161. data/third_party/abseil-cpp/absl/strings/internal/char_map.h +156 -0
  162. data/third_party/abseil-cpp/absl/strings/internal/charconv_bigint.cc +359 -0
  163. data/third_party/abseil-cpp/absl/strings/internal/charconv_bigint.h +421 -0
  164. data/third_party/abseil-cpp/absl/strings/internal/charconv_parse.cc +504 -0
  165. data/third_party/abseil-cpp/absl/strings/internal/charconv_parse.h +99 -0
  166. data/third_party/abseil-cpp/absl/strings/internal/escaping.cc +180 -0
  167. data/third_party/abseil-cpp/absl/strings/internal/escaping.h +58 -0
  168. data/third_party/abseil-cpp/absl/strings/internal/memutil.cc +112 -0
  169. data/third_party/abseil-cpp/absl/strings/internal/memutil.h +148 -0
  170. data/third_party/abseil-cpp/absl/strings/internal/ostringstream.cc +36 -0
  171. data/third_party/abseil-cpp/absl/strings/internal/ostringstream.h +89 -0
  172. data/third_party/abseil-cpp/absl/strings/internal/resize_uninitialized.h +73 -0
  173. data/third_party/abseil-cpp/absl/strings/internal/stl_type_traits.h +248 -0
  174. data/third_party/abseil-cpp/absl/strings/internal/str_join_internal.h +314 -0
  175. data/third_party/abseil-cpp/absl/strings/internal/str_split_internal.h +455 -0
  176. data/third_party/abseil-cpp/absl/strings/internal/utf8.cc +53 -0
  177. data/third_party/abseil-cpp/absl/strings/internal/utf8.h +50 -0
  178. data/third_party/abseil-cpp/absl/strings/match.cc +40 -0
  179. data/third_party/abseil-cpp/absl/strings/match.h +90 -0
  180. data/third_party/abseil-cpp/absl/strings/numbers.cc +916 -0
  181. data/third_party/abseil-cpp/absl/strings/numbers.h +263 -0
  182. data/third_party/abseil-cpp/absl/strings/str_cat.cc +246 -0
  183. data/third_party/abseil-cpp/absl/strings/str_cat.h +408 -0
  184. data/third_party/abseil-cpp/absl/strings/str_join.h +293 -0
  185. data/third_party/abseil-cpp/absl/strings/str_replace.cc +82 -0
  186. data/third_party/abseil-cpp/absl/strings/str_replace.h +219 -0
  187. data/third_party/abseil-cpp/absl/strings/str_split.cc +139 -0
  188. data/third_party/abseil-cpp/absl/strings/str_split.h +513 -0
  189. data/third_party/abseil-cpp/absl/strings/string_view.cc +235 -0
  190. data/third_party/abseil-cpp/absl/strings/string_view.h +615 -0
  191. data/third_party/abseil-cpp/absl/strings/strip.h +91 -0
  192. data/third_party/abseil-cpp/absl/strings/substitute.cc +171 -0
  193. data/third_party/abseil-cpp/absl/strings/substitute.h +693 -0
  194. data/third_party/abseil-cpp/absl/types/bad_optional_access.cc +48 -0
  195. data/third_party/abseil-cpp/absl/types/bad_optional_access.h +78 -0
  196. data/third_party/abseil-cpp/absl/types/internal/optional.h +396 -0
  197. data/third_party/abseil-cpp/absl/types/internal/span.h +128 -0
  198. data/third_party/abseil-cpp/absl/types/optional.h +776 -0
  199. data/third_party/abseil-cpp/absl/types/span.h +713 -0
  200. data/third_party/abseil-cpp/absl/utility/utility.h +350 -0
  201. data/third_party/upb/upb/decode.c +4 -0
  202. data/third_party/upb/upb/port.c +0 -1
  203. data/third_party/upb/upb/port_def.inc +1 -3
  204. data/third_party/upb/upb/table.c +2 -1
  205. metadata +147 -43
  206. data/src/core/lib/json/json_common.h +0 -34
  207. data/src/core/lib/json/json_reader.h +0 -146
  208. data/src/core/lib/json/json_string.cc +0 -367
  209. data/src/core/lib/json/json_writer.h +0 -84
@@ -0,0 +1,164 @@
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: escaping.h
18
+ // -----------------------------------------------------------------------------
19
+ //
20
+ // This header file contains string utilities involved in escaping and
21
+ // unescaping strings in various ways.
22
+
23
+ #ifndef ABSL_STRINGS_ESCAPING_H_
24
+ #define ABSL_STRINGS_ESCAPING_H_
25
+
26
+ #include <cstddef>
27
+ #include <string>
28
+ #include <vector>
29
+
30
+ #include "absl/base/macros.h"
31
+ #include "absl/strings/ascii.h"
32
+ #include "absl/strings/str_join.h"
33
+ #include "absl/strings/string_view.h"
34
+
35
+ namespace absl {
36
+ ABSL_NAMESPACE_BEGIN
37
+
38
+ // CUnescape()
39
+ //
40
+ // Unescapes a `source` string and copies it into `dest`, rewriting C-style
41
+ // escape sequences (https://en.cppreference.com/w/cpp/language/escape) into
42
+ // their proper code point equivalents, returning `true` if successful.
43
+ //
44
+ // The following unescape sequences can be handled:
45
+ //
46
+ // * ASCII escape sequences ('\n','\r','\\', etc.) to their ASCII equivalents
47
+ // * Octal escape sequences ('\nnn') to byte nnn. The unescaped value must
48
+ // resolve to a single byte or an error will occur. E.g. values greater than
49
+ // 0xff will produce an error.
50
+ // * Hexadecimal escape sequences ('\xnn') to byte nn. While an arbitrary
51
+ // number of following digits are allowed, the unescaped value must resolve
52
+ // to a single byte or an error will occur. E.g. '\x0045' is equivalent to
53
+ // '\x45', but '\x1234' will produce an error.
54
+ // * Unicode escape sequences ('\unnnn' for exactly four hex digits or
55
+ // '\Unnnnnnnn' for exactly eight hex digits, which will be encoded in
56
+ // UTF-8. (E.g., `\u2019` unescapes to the three bytes 0xE2, 0x80, and
57
+ // 0x99).
58
+ //
59
+ // If any errors are encountered, this function returns `false`, leaving the
60
+ // `dest` output parameter in an unspecified state, and stores the first
61
+ // encountered error in `error`. To disable error reporting, set `error` to
62
+ // `nullptr` or use the overload with no error reporting below.
63
+ //
64
+ // Example:
65
+ //
66
+ // std::string s = "foo\\rbar\\nbaz\\t";
67
+ // std::string unescaped_s;
68
+ // if (!absl::CUnescape(s, &unescaped_s) {
69
+ // ...
70
+ // }
71
+ // EXPECT_EQ(unescaped_s, "foo\rbar\nbaz\t");
72
+ bool CUnescape(absl::string_view source, std::string* dest, std::string* error);
73
+
74
+ // Overload of `CUnescape()` with no error reporting.
75
+ inline bool CUnescape(absl::string_view source, std::string* dest) {
76
+ return CUnescape(source, dest, nullptr);
77
+ }
78
+
79
+ // CEscape()
80
+ //
81
+ // Escapes a 'src' string using C-style escapes sequences
82
+ // (https://en.cppreference.com/w/cpp/language/escape), escaping other
83
+ // non-printable/non-whitespace bytes as octal sequences (e.g. "\377").
84
+ //
85
+ // Example:
86
+ //
87
+ // std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n";
88
+ // std::string escaped_s = absl::CEscape(s);
89
+ // EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\010\\t\\n\\013\\014\\r\\n");
90
+ std::string CEscape(absl::string_view src);
91
+
92
+ // CHexEscape()
93
+ //
94
+ // Escapes a 'src' string using C-style escape sequences, escaping
95
+ // other non-printable/non-whitespace bytes as hexadecimal sequences (e.g.
96
+ // "\xFF").
97
+ //
98
+ // Example:
99
+ //
100
+ // std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n";
101
+ // std::string escaped_s = absl::CHexEscape(s);
102
+ // EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\x08\\t\\n\\x0b\\x0c\\r\\n");
103
+ std::string CHexEscape(absl::string_view src);
104
+
105
+ // Utf8SafeCEscape()
106
+ //
107
+ // Escapes a 'src' string using C-style escape sequences, escaping bytes as
108
+ // octal sequences, and passing through UTF-8 characters without conversion.
109
+ // I.e., when encountering any bytes with their high bit set, this function
110
+ // will not escape those values, whether or not they are valid UTF-8.
111
+ std::string Utf8SafeCEscape(absl::string_view src);
112
+
113
+ // Utf8SafeCHexEscape()
114
+ //
115
+ // Escapes a 'src' string using C-style escape sequences, escaping bytes as
116
+ // hexadecimal sequences, and passing through UTF-8 characters without
117
+ // conversion.
118
+ std::string Utf8SafeCHexEscape(absl::string_view src);
119
+
120
+ // Base64Unescape()
121
+ //
122
+ // Converts a `src` string encoded in Base64 to its binary equivalent, writing
123
+ // it to a `dest` buffer, returning `true` on success. If `src` contains invalid
124
+ // characters, `dest` is cleared and returns `false`.
125
+ bool Base64Unescape(absl::string_view src, std::string* dest);
126
+
127
+ // WebSafeBase64Unescape()
128
+ //
129
+ // Converts a `src` string encoded in Base64 to its binary equivalent, writing
130
+ // it to a `dest` buffer, but using '-' instead of '+', and '_' instead of '/'.
131
+ // If `src` contains invalid characters, `dest` is cleared and returns `false`.
132
+ bool WebSafeBase64Unescape(absl::string_view src, std::string* dest);
133
+
134
+ // Base64Escape()
135
+ //
136
+ // Encodes a `src` string into a base64-encoded string, with padding characters.
137
+ // This function conforms with RFC 4648 section 4 (base64).
138
+ void Base64Escape(absl::string_view src, std::string* dest);
139
+ std::string Base64Escape(absl::string_view src);
140
+
141
+ // WebSafeBase64Escape()
142
+ //
143
+ // Encodes a `src` string into a base64-like string, using '-' instead of '+'
144
+ // and '_' instead of '/', and without padding. This function conforms with RFC
145
+ // 4648 section 5 (base64url).
146
+ void WebSafeBase64Escape(absl::string_view src, std::string* dest);
147
+ std::string WebSafeBase64Escape(absl::string_view src);
148
+
149
+ // HexStringToBytes()
150
+ //
151
+ // Converts an ASCII hex string into bytes, returning binary data of length
152
+ // `from.size()/2`.
153
+ std::string HexStringToBytes(absl::string_view from);
154
+
155
+ // BytesToHexString()
156
+ //
157
+ // Converts binary data into an ASCII text string, returning a string of size
158
+ // `2*from.size()`.
159
+ std::string BytesToHexString(absl::string_view from);
160
+
161
+ ABSL_NAMESPACE_END
162
+ } // namespace absl
163
+
164
+ #endif // ABSL_STRINGS_ESCAPING_H_
@@ -0,0 +1,156 @@
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
+ // Character Map Class
16
+ //
17
+ // A fast, bit-vector map for 8-bit unsigned characters.
18
+ // This class is useful for non-character purposes as well.
19
+
20
+ #ifndef ABSL_STRINGS_INTERNAL_CHAR_MAP_H_
21
+ #define ABSL_STRINGS_INTERNAL_CHAR_MAP_H_
22
+
23
+ #include <cstddef>
24
+ #include <cstdint>
25
+ #include <cstring>
26
+
27
+ #include "absl/base/macros.h"
28
+ #include "absl/base/port.h"
29
+
30
+ namespace absl {
31
+ ABSL_NAMESPACE_BEGIN
32
+ namespace strings_internal {
33
+
34
+ class Charmap {
35
+ public:
36
+ constexpr Charmap() : m_() {}
37
+
38
+ // Initializes with a given char*. Note that NUL is not treated as
39
+ // a terminator, but rather a char to be flicked.
40
+ Charmap(const char* str, int len) : m_() {
41
+ while (len--) SetChar(*str++);
42
+ }
43
+
44
+ // Initializes with a given char*. NUL is treated as a terminator
45
+ // and will not be in the charmap.
46
+ explicit Charmap(const char* str) : m_() {
47
+ while (*str) SetChar(*str++);
48
+ }
49
+
50
+ constexpr bool contains(unsigned char c) const {
51
+ return (m_[c / 64] >> (c % 64)) & 0x1;
52
+ }
53
+
54
+ // Returns true if and only if a character exists in both maps.
55
+ bool IntersectsWith(const Charmap& c) const {
56
+ for (size_t i = 0; i < ABSL_ARRAYSIZE(m_); ++i) {
57
+ if ((m_[i] & c.m_[i]) != 0) return true;
58
+ }
59
+ return false;
60
+ }
61
+
62
+ bool IsZero() const {
63
+ for (uint64_t c : m_) {
64
+ if (c != 0) return false;
65
+ }
66
+ return true;
67
+ }
68
+
69
+ // Containing only a single specified char.
70
+ static constexpr Charmap Char(char x) {
71
+ return Charmap(CharMaskForWord(x, 0), CharMaskForWord(x, 1),
72
+ CharMaskForWord(x, 2), CharMaskForWord(x, 3));
73
+ }
74
+
75
+ // Containing all the chars in the C-std::string 's'.
76
+ // Note that this is expensively recursive because of the C++11 constexpr
77
+ // formulation. Use only in constexpr initializers.
78
+ static constexpr Charmap FromString(const char* s) {
79
+ return *s == 0 ? Charmap() : (Char(*s) | FromString(s + 1));
80
+ }
81
+
82
+ // Containing all the chars in the closed interval [lo,hi].
83
+ static constexpr Charmap Range(char lo, char hi) {
84
+ return Charmap(RangeForWord(lo, hi, 0), RangeForWord(lo, hi, 1),
85
+ RangeForWord(lo, hi, 2), RangeForWord(lo, hi, 3));
86
+ }
87
+
88
+ friend constexpr Charmap operator&(const Charmap& a, const Charmap& b) {
89
+ return Charmap(a.m_[0] & b.m_[0], a.m_[1] & b.m_[1], a.m_[2] & b.m_[2],
90
+ a.m_[3] & b.m_[3]);
91
+ }
92
+
93
+ friend constexpr Charmap operator|(const Charmap& a, const Charmap& b) {
94
+ return Charmap(a.m_[0] | b.m_[0], a.m_[1] | b.m_[1], a.m_[2] | b.m_[2],
95
+ a.m_[3] | b.m_[3]);
96
+ }
97
+
98
+ friend constexpr Charmap operator~(const Charmap& a) {
99
+ return Charmap(~a.m_[0], ~a.m_[1], ~a.m_[2], ~a.m_[3]);
100
+ }
101
+
102
+ private:
103
+ constexpr Charmap(uint64_t b0, uint64_t b1, uint64_t b2, uint64_t b3)
104
+ : m_{b0, b1, b2, b3} {}
105
+
106
+ static constexpr uint64_t RangeForWord(unsigned char lo, unsigned char hi,
107
+ uint64_t word) {
108
+ return OpenRangeFromZeroForWord(hi + 1, word) &
109
+ ~OpenRangeFromZeroForWord(lo, word);
110
+ }
111
+
112
+ // All the chars in the specified word of the range [0, upper).
113
+ static constexpr uint64_t OpenRangeFromZeroForWord(uint64_t upper,
114
+ uint64_t word) {
115
+ return (upper <= 64 * word)
116
+ ? 0
117
+ : (upper >= 64 * (word + 1))
118
+ ? ~static_cast<uint64_t>(0)
119
+ : (~static_cast<uint64_t>(0) >> (64 - upper % 64));
120
+ }
121
+
122
+ static constexpr uint64_t CharMaskForWord(unsigned char x, uint64_t word) {
123
+ return (x / 64 == word) ? (static_cast<uint64_t>(1) << (x % 64)) : 0;
124
+ }
125
+
126
+ private:
127
+ void SetChar(unsigned char c) {
128
+ m_[c / 64] |= static_cast<uint64_t>(1) << (c % 64);
129
+ }
130
+
131
+ uint64_t m_[4];
132
+ };
133
+
134
+ // Mirror the char-classifying predicates in <cctype>
135
+ constexpr Charmap UpperCharmap() { return Charmap::Range('A', 'Z'); }
136
+ constexpr Charmap LowerCharmap() { return Charmap::Range('a', 'z'); }
137
+ constexpr Charmap DigitCharmap() { return Charmap::Range('0', '9'); }
138
+ constexpr Charmap AlphaCharmap() { return LowerCharmap() | UpperCharmap(); }
139
+ constexpr Charmap AlnumCharmap() { return DigitCharmap() | AlphaCharmap(); }
140
+ constexpr Charmap XDigitCharmap() {
141
+ return DigitCharmap() | Charmap::Range('A', 'F') | Charmap::Range('a', 'f');
142
+ }
143
+ constexpr Charmap PrintCharmap() { return Charmap::Range(0x20, 0x7e); }
144
+ constexpr Charmap SpaceCharmap() { return Charmap::FromString("\t\n\v\f\r "); }
145
+ constexpr Charmap CntrlCharmap() {
146
+ return Charmap::Range(0, 0x7f) & ~PrintCharmap();
147
+ }
148
+ constexpr Charmap BlankCharmap() { return Charmap::FromString("\t "); }
149
+ constexpr Charmap GraphCharmap() { return PrintCharmap() & ~SpaceCharmap(); }
150
+ constexpr Charmap PunctCharmap() { return GraphCharmap() & ~AlnumCharmap(); }
151
+
152
+ } // namespace strings_internal
153
+ ABSL_NAMESPACE_END
154
+ } // namespace absl
155
+
156
+ #endif // ABSL_STRINGS_INTERNAL_CHAR_MAP_H_
@@ -0,0 +1,359 @@
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_bigint.h"
16
+
17
+ #include <algorithm>
18
+ #include <cassert>
19
+ #include <string>
20
+
21
+ namespace absl {
22
+ ABSL_NAMESPACE_BEGIN
23
+ namespace strings_internal {
24
+
25
+ namespace {
26
+
27
+ // Table containing some large powers of 5, for fast computation.
28
+
29
+ // Constant step size for entries in the kLargePowersOfFive table. Each entry
30
+ // is larger than the previous entry by a factor of 5**kLargePowerOfFiveStep
31
+ // (or 5**27).
32
+ //
33
+ // In other words, the Nth entry in the table is 5**(27*N).
34
+ //
35
+ // 5**27 is the largest power of 5 that fits in 64 bits.
36
+ constexpr int kLargePowerOfFiveStep = 27;
37
+
38
+ // The largest legal index into the kLargePowersOfFive table.
39
+ //
40
+ // In other words, the largest precomputed power of 5 is 5**(27*20).
41
+ constexpr int kLargestPowerOfFiveIndex = 20;
42
+
43
+ // Table of powers of (5**27), up to (5**27)**20 == 5**540.
44
+ //
45
+ // Used to generate large powers of 5 while limiting the number of repeated
46
+ // multiplications required.
47
+ //
48
+ // clang-format off
49
+ const uint32_t kLargePowersOfFive[] = {
50
+ // 5**27 (i=1), start=0, end=2
51
+ 0xfa10079dU, 0x6765c793U,
52
+ // 5**54 (i=2), start=2, end=6
53
+ 0x97d9f649U, 0x6664242dU, 0x29939b14U, 0x29c30f10U,
54
+ // 5**81 (i=3), start=6, end=12
55
+ 0xc4f809c5U, 0x7bf3f22aU, 0x67bdae34U, 0xad340517U, 0x369d1b5fU, 0x10de1593U,
56
+ // 5**108 (i=4), start=12, end=20
57
+ 0x92b260d1U, 0x9efff7c7U, 0x81de0ec6U, 0xaeba5d56U, 0x410664a4U, 0x4f40737aU,
58
+ 0x20d3846fU, 0x06d00f73U,
59
+ // 5**135 (i=5), start=20, end=30
60
+ 0xff1b172dU, 0x13a1d71cU, 0xefa07617U, 0x7f682d3dU, 0xff8c90c0U, 0x3f0131e7U,
61
+ 0x3fdcb9feU, 0x917b0177U, 0x16c407a7U, 0x02c06b9dU,
62
+ // 5**162 (i=6), start=30, end=42
63
+ 0x960f7199U, 0x056667ecU, 0xe07aefd8U, 0x80f2b9ccU, 0x8273f5e3U, 0xeb9a214aU,
64
+ 0x40b38005U, 0x0e477ad4U, 0x277d08e6U, 0xfa28b11eU, 0xd3f7d784U, 0x011c835bU,
65
+ // 5**189 (i=7), start=42, end=56
66
+ 0xf723d9d5U, 0x3282d3f3U, 0xe00857d1U, 0x69659d25U, 0x2cf117cfU, 0x24da6d07U,
67
+ 0x954d1417U, 0x3e5d8cedU, 0x7a8bb766U, 0xfd785ae6U, 0x645436d2U, 0x40c78b34U,
68
+ 0x94151217U, 0x0072e9f7U,
69
+ // 5**216 (i=8), start=56, end=72
70
+ 0x2b416aa1U, 0x7893c5a7U, 0xe37dc6d4U, 0x2bad2beaU, 0xf0fc846cU, 0x7575ae4bU,
71
+ 0x62587b14U, 0x83b67a34U, 0x02110cdbU, 0xf7992f55U, 0x00deb022U, 0xa4a23becU,
72
+ 0x8af5c5cdU, 0xb85b654fU, 0x818df38bU, 0x002e69d2U,
73
+ // 5**243 (i=9), start=72, end=90
74
+ 0x3518cbbdU, 0x20b0c15fU, 0x38756c2fU, 0xfb5dc3ddU, 0x22ad2d94U, 0xbf35a952U,
75
+ 0xa699192aU, 0x9a613326U, 0xad2a9cedU, 0xd7f48968U, 0xe87dfb54U, 0xc8f05db6U,
76
+ 0x5ef67531U, 0x31c1ab49U, 0xe202ac9fU, 0x9b2957b5U, 0xa143f6d3U, 0x0012bf07U,
77
+ // 5**270 (i=10), start=90, end=110
78
+ 0x8b971de9U, 0x21aba2e1U, 0x63944362U, 0x57172336U, 0xd9544225U, 0xfb534166U,
79
+ 0x08c563eeU, 0x14640ee2U, 0x24e40d31U, 0x02b06537U, 0x03887f14U, 0x0285e533U,
80
+ 0xb744ef26U, 0x8be3a6c4U, 0x266979b4U, 0x6761ece2U, 0xd9cb39e4U, 0xe67de319U,
81
+ 0x0d39e796U, 0x00079250U,
82
+ // 5**297 (i=11), start=110, end=132
83
+ 0x260eb6e5U, 0xf414a796U, 0xee1a7491U, 0xdb9368ebU, 0xf50c105bU, 0x59157750U,
84
+ 0x9ed2fb5cU, 0xf6e56d8bU, 0xeaee8d23U, 0x0f319f75U, 0x2aa134d6U, 0xac2908e9U,
85
+ 0xd4413298U, 0x02f02a55U, 0x989d5a7aU, 0x70dde184U, 0xba8040a7U, 0x03200981U,
86
+ 0xbe03b11cU, 0x3c1c2a18U, 0xd60427a1U, 0x00030ee0U,
87
+ // 5**324 (i=12), start=132, end=156
88
+ 0xce566d71U, 0xf1c4aa25U, 0x4e93ca53U, 0xa72283d0U, 0x551a73eaU, 0x3d0538e2U,
89
+ 0x8da4303fU, 0x6a58de60U, 0x0e660221U, 0x49cf61a6U, 0x8d058fc1U, 0xb9d1a14cU,
90
+ 0x4bab157dU, 0xc85c6932U, 0x518c8b9eU, 0x9b92b8d0U, 0x0d8a0e21U, 0xbd855df9U,
91
+ 0xb3ea59a1U, 0x8da29289U, 0x4584d506U, 0x3752d80fU, 0xb72569c6U, 0x00013c33U,
92
+ // 5**351 (i=13), start=156, end=182
93
+ 0x190f354dU, 0x83695cfeU, 0xe5a4d0c7U, 0xb60fb7e8U, 0xee5bbcc4U, 0xb922054cU,
94
+ 0xbb4f0d85U, 0x48394028U, 0x1d8957dbU, 0x0d7edb14U, 0x4ecc7587U, 0x505e9e02U,
95
+ 0x4c87f36bU, 0x99e66bd6U, 0x44b9ed35U, 0x753037d4U, 0xe5fe5f27U, 0x2742c203U,
96
+ 0x13b2ed2bU, 0xdc525d2cU, 0xe6fde59aU, 0x77ffb18fU, 0x13c5752cU, 0x08a84bccU,
97
+ 0x859a4940U, 0x00007fb6U,
98
+ // 5**378 (i=14), start=182, end=210
99
+ 0x4f98cb39U, 0xa60edbbcU, 0x83b5872eU, 0xa501acffU, 0x9cc76f78U, 0xbadd4c73U,
100
+ 0x43e989faU, 0xca7acf80U, 0x2e0c824fU, 0xb19f4ffcU, 0x092fd81cU, 0xe4eb645bU,
101
+ 0xa1ff84c2U, 0x8a5a83baU, 0xa8a1fae9U, 0x1db43609U, 0xb0fed50bU, 0x0dd7d2bdU,
102
+ 0x7d7accd8U, 0x91fa640fU, 0x37dcc6c5U, 0x1c417fd5U, 0xe4d462adU, 0xe8a43399U,
103
+ 0x131bf9a5U, 0x8df54d29U, 0x36547dc1U, 0x00003395U,
104
+ // 5**405 (i=15), start=210, end=240
105
+ 0x5bd330f5U, 0x77d21967U, 0x1ac481b7U, 0x6be2f7ceU, 0x7f4792a9U, 0xe84c2c52U,
106
+ 0x84592228U, 0x9dcaf829U, 0xdab44ce1U, 0x3d0c311bU, 0x532e297dU, 0x4704e8b4U,
107
+ 0x9cdc32beU, 0x41e64d9dU, 0x7717bea1U, 0xa824c00dU, 0x08f50b27U, 0x0f198d77U,
108
+ 0x49bbfdf0U, 0x025c6c69U, 0xd4e55cd3U, 0xf083602bU, 0xb9f0fecdU, 0xc0864aeaU,
109
+ 0x9cb98681U, 0xaaf620e9U, 0xacb6df30U, 0x4faafe66U, 0x8af13c3bU, 0x000014d5U,
110
+ // 5**432 (i=16), start=240, end=272
111
+ 0x682bb941U, 0x89a9f297U, 0xcba75d7bU, 0x404217b1U, 0xb4e519e9U, 0xa1bc162bU,
112
+ 0xf7f5910aU, 0x98715af5U, 0x2ff53e57U, 0xe3ef118cU, 0x490c4543U, 0xbc9b1734U,
113
+ 0x2affbe4dU, 0x4cedcb4cU, 0xfb14e99eU, 0x35e34212U, 0xece39c24U, 0x07673ab3U,
114
+ 0xe73115ddU, 0xd15d38e7U, 0x093eed3bU, 0xf8e7eac5U, 0x78a8cc80U, 0x25227aacU,
115
+ 0x3f590551U, 0x413da1cbU, 0xdf643a55U, 0xab65ad44U, 0xd70b23d7U, 0xc672cd76U,
116
+ 0x3364ea62U, 0x0000086aU,
117
+ // 5**459 (i=17), start=272, end=306
118
+ 0x22f163ddU, 0x23cf07acU, 0xbe2af6c2U, 0xf412f6f6U, 0xc3ff541eU, 0x6eeaf7deU,
119
+ 0xa47047e0U, 0x408cda92U, 0x0f0eeb08U, 0x56deba9dU, 0xcfc6b090U, 0x8bbbdf04U,
120
+ 0x3933cdb3U, 0x9e7bb67dU, 0x9f297035U, 0x38946244U, 0xee1d37bbU, 0xde898174U,
121
+ 0x63f3559dU, 0x705b72fbU, 0x138d27d9U, 0xf8603a78U, 0x735eec44U, 0xe30987d5U,
122
+ 0xc6d38070U, 0x9cfe548eU, 0x9ff01422U, 0x7c564aa8U, 0x91cc60baU, 0xcbc3565dU,
123
+ 0x7550a50bU, 0x6909aeadU, 0x13234c45U, 0x00000366U,
124
+ // 5**486 (i=18), start=306, end=342
125
+ 0x17954989U, 0x3a7d7709U, 0x98042de5U, 0xa9011443U, 0x45e723c2U, 0x269ffd6fU,
126
+ 0x58852a46U, 0xaaa1042aU, 0x2eee8153U, 0xb2b6c39eU, 0xaf845b65U, 0xf6c365d7U,
127
+ 0xe4cffb2bU, 0xc840e90cU, 0xabea8abbU, 0x5c58f8d2U, 0x5c19fa3aU, 0x4670910aU,
128
+ 0x4449f21cU, 0xefa645b3U, 0xcc427decU, 0x083c3d73U, 0x467cb413U, 0x6fe10ae4U,
129
+ 0x3caffc72U, 0x9f8da55eU, 0x5e5c8ea7U, 0x490594bbU, 0xf0871b0bU, 0xdd89816cU,
130
+ 0x8e931df8U, 0xe85ce1c9U, 0xcca090a5U, 0x575fa16bU, 0x6b9f106cU, 0x0000015fU,
131
+ // 5**513 (i=19), start=342, end=380
132
+ 0xee20d805U, 0x57bc3c07U, 0xcdea624eU, 0xd3f0f52dU, 0x9924b4f4U, 0xcf968640U,
133
+ 0x61d41962U, 0xe87fb464U, 0xeaaf51c7U, 0x564c8b60U, 0xccda4028U, 0x529428bbU,
134
+ 0x313a1fa8U, 0x96bd0f94U, 0x7a82ebaaU, 0xad99e7e9U, 0xf2668cd4U, 0xbe33a45eU,
135
+ 0xfd0db669U, 0x87ee369fU, 0xd3ec20edU, 0x9c4d7db7U, 0xdedcf0d8U, 0x7cd2ca64U,
136
+ 0xe25a6577U, 0x61003fd4U, 0xe56f54ccU, 0x10b7c748U, 0x40526e5eU, 0x7300ae87U,
137
+ 0x5c439261U, 0x2c0ff469U, 0xbf723f12U, 0xb2379b61U, 0xbf59b4f5U, 0xc91b1c3fU,
138
+ 0xf0046d27U, 0x0000008dU,
139
+ // 5**540 (i=20), start=380, end=420
140
+ 0x525c9e11U, 0xf4e0eb41U, 0xebb2895dU, 0x5da512f9U, 0x7d9b29d4U, 0x452f4edcU,
141
+ 0x0b90bc37U, 0x341777cbU, 0x63d269afU, 0x1da77929U, 0x0a5c1826U, 0x77991898U,
142
+ 0x5aeddf86U, 0xf853a877U, 0x538c31ccU, 0xe84896daU, 0xb7a0010bU, 0x17ef4de5U,
143
+ 0xa52a2adeU, 0x029fd81cU, 0x987ce701U, 0x27fefd77U, 0xdb46c66fU, 0x5d301900U,
144
+ 0x496998c0U, 0xbb6598b9U, 0x5eebb607U, 0xe547354aU, 0xdf4a2f7eU, 0xf06c4955U,
145
+ 0x96242ffaU, 0x1775fb27U, 0xbecc58ceU, 0xebf2a53bU, 0x3eaad82aU, 0xf41137baU,
146
+ 0x573e6fbaU, 0xfb4866b8U, 0x54002148U, 0x00000039U,
147
+ };
148
+ // clang-format on
149
+
150
+ // Returns a pointer to the big integer data for (5**27)**i. i must be
151
+ // between 1 and 20, inclusive.
152
+ const uint32_t* LargePowerOfFiveData(int i) {
153
+ return kLargePowersOfFive + i * (i - 1);
154
+ }
155
+
156
+ // Returns the size of the big integer data for (5**27)**i, in words. i must be
157
+ // between 1 and 20, inclusive.
158
+ int LargePowerOfFiveSize(int i) { return 2 * i; }
159
+ } // namespace
160
+
161
+ const uint32_t kFiveToNth[14] = {
162
+ 1, 5, 25, 125, 625, 3125, 15625,
163
+ 78125, 390625, 1953125, 9765625, 48828125, 244140625, 1220703125,
164
+ };
165
+
166
+ const uint32_t kTenToNth[10] = {
167
+ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000,
168
+ };
169
+
170
+ template <int max_words>
171
+ int BigUnsigned<max_words>::ReadFloatMantissa(const ParsedFloat& fp,
172
+ int significant_digits) {
173
+ SetToZero();
174
+ assert(fp.type == FloatType::kNumber);
175
+
176
+ if (fp.subrange_begin == nullptr) {
177
+ // We already exactly parsed the mantissa, so no more work is necessary.
178
+ words_[0] = fp.mantissa & 0xffffffffu;
179
+ words_[1] = fp.mantissa >> 32;
180
+ if (words_[1]) {
181
+ size_ = 2;
182
+ } else if (words_[0]) {
183
+ size_ = 1;
184
+ }
185
+ return fp.exponent;
186
+ }
187
+ int exponent_adjust =
188
+ ReadDigits(fp.subrange_begin, fp.subrange_end, significant_digits);
189
+ return fp.literal_exponent + exponent_adjust;
190
+ }
191
+
192
+ template <int max_words>
193
+ int BigUnsigned<max_words>::ReadDigits(const char* begin, const char* end,
194
+ int significant_digits) {
195
+ assert(significant_digits <= Digits10() + 1);
196
+ SetToZero();
197
+
198
+ bool after_decimal_point = false;
199
+ // Discard any leading zeroes before the decimal point
200
+ while (begin < end && *begin == '0') {
201
+ ++begin;
202
+ }
203
+ int dropped_digits = 0;
204
+ // Discard any trailing zeroes. These may or may not be after the decimal
205
+ // point.
206
+ while (begin < end && *std::prev(end) == '0') {
207
+ --end;
208
+ ++dropped_digits;
209
+ }
210
+ if (begin < end && *std::prev(end) == '.') {
211
+ // If the std::string ends in '.', either before or after dropping zeroes, then
212
+ // drop the decimal point and look for more digits to drop.
213
+ dropped_digits = 0;
214
+ --end;
215
+ while (begin < end && *std::prev(end) == '0') {
216
+ --end;
217
+ ++dropped_digits;
218
+ }
219
+ } else if (dropped_digits) {
220
+ // We dropped digits, and aren't sure if they're before or after the decimal
221
+ // point. Figure that out now.
222
+ const char* dp = std::find(begin, end, '.');
223
+ if (dp != end) {
224
+ // The dropped trailing digits were after the decimal point, so don't
225
+ // count them.
226
+ dropped_digits = 0;
227
+ }
228
+ }
229
+ // Any non-fraction digits we dropped need to be accounted for in our exponent
230
+ // adjustment.
231
+ int exponent_adjust = dropped_digits;
232
+
233
+ uint32_t queued = 0;
234
+ int digits_queued = 0;
235
+ for (; begin != end && significant_digits > 0; ++begin) {
236
+ if (*begin == '.') {
237
+ after_decimal_point = true;
238
+ continue;
239
+ }
240
+ if (after_decimal_point) {
241
+ // For each fractional digit we emit in our parsed integer, adjust our
242
+ // decimal exponent to compensate.
243
+ --exponent_adjust;
244
+ }
245
+ int digit = (*begin - '0');
246
+ --significant_digits;
247
+ if (significant_digits == 0 && std::next(begin) != end &&
248
+ (digit == 0 || digit == 5)) {
249
+ // If this is the very last significant digit, but insignificant digits
250
+ // remain, we know that the last of those remaining significant digits is
251
+ // nonzero. (If it wasn't, we would have stripped it before we got here.)
252
+ // So if this final digit is a 0 or 5, adjust it upward by 1.
253
+ //
254
+ // This adjustment is what allows incredibly large mantissas ending in
255
+ // 500000...000000000001 to correctly round up, rather than to nearest.
256
+ ++digit;
257
+ }
258
+ queued = 10 * queued + digit;
259
+ ++digits_queued;
260
+ if (digits_queued == kMaxSmallPowerOfTen) {
261
+ MultiplyBy(kTenToNth[kMaxSmallPowerOfTen]);
262
+ AddWithCarry(0, queued);
263
+ queued = digits_queued = 0;
264
+ }
265
+ }
266
+ // Encode any remaining digits.
267
+ if (digits_queued) {
268
+ MultiplyBy(kTenToNth[digits_queued]);
269
+ AddWithCarry(0, queued);
270
+ }
271
+
272
+ // If any insignificant digits remain, we will drop them. But if we have not
273
+ // yet read the decimal point, then we have to adjust the exponent to account
274
+ // for the dropped digits.
275
+ if (begin < end && !after_decimal_point) {
276
+ // This call to std::find will result in a pointer either to the decimal
277
+ // point, or to the end of our buffer if there was none.
278
+ //
279
+ // Either way, [begin, decimal_point) will contain the set of dropped digits
280
+ // that require an exponent adjustment.
281
+ const char* decimal_point = std::find(begin, end, '.');
282
+ exponent_adjust += (decimal_point - begin);
283
+ }
284
+ return exponent_adjust;
285
+ }
286
+
287
+ template <int max_words>
288
+ /* static */ BigUnsigned<max_words> BigUnsigned<max_words>::FiveToTheNth(
289
+ int n) {
290
+ BigUnsigned answer(1u);
291
+
292
+ // Seed from the table of large powers, if possible.
293
+ bool first_pass = true;
294
+ while (n >= kLargePowerOfFiveStep) {
295
+ int big_power =
296
+ std::min(n / kLargePowerOfFiveStep, kLargestPowerOfFiveIndex);
297
+ if (first_pass) {
298
+ // just copy, rather than multiplying by 1
299
+ std::copy(
300
+ LargePowerOfFiveData(big_power),
301
+ LargePowerOfFiveData(big_power) + LargePowerOfFiveSize(big_power),
302
+ answer.words_);
303
+ answer.size_ = LargePowerOfFiveSize(big_power);
304
+ first_pass = false;
305
+ } else {
306
+ answer.MultiplyBy(LargePowerOfFiveSize(big_power),
307
+ LargePowerOfFiveData(big_power));
308
+ }
309
+ n -= kLargePowerOfFiveStep * big_power;
310
+ }
311
+ answer.MultiplyByFiveToTheNth(n);
312
+ return answer;
313
+ }
314
+
315
+ template <int max_words>
316
+ void BigUnsigned<max_words>::MultiplyStep(int original_size,
317
+ const uint32_t* other_words,
318
+ int other_size, int step) {
319
+ int this_i = std::min(original_size - 1, step);
320
+ int other_i = step - this_i;
321
+
322
+ uint64_t this_word = 0;
323
+ uint64_t carry = 0;
324
+ for (; this_i >= 0 && other_i < other_size; --this_i, ++other_i) {
325
+ uint64_t product = words_[this_i];
326
+ product *= other_words[other_i];
327
+ this_word += product;
328
+ carry += (this_word >> 32);
329
+ this_word &= 0xffffffff;
330
+ }
331
+ AddWithCarry(step + 1, carry);
332
+ words_[step] = this_word & 0xffffffff;
333
+ if (this_word > 0 && size_ <= step) {
334
+ size_ = step + 1;
335
+ }
336
+ }
337
+
338
+ template <int max_words>
339
+ std::string BigUnsigned<max_words>::ToString() const {
340
+ BigUnsigned<max_words> copy = *this;
341
+ std::string result;
342
+ // Build result in reverse order
343
+ while (copy.size() > 0) {
344
+ int next_digit = copy.DivMod<10>();
345
+ result.push_back('0' + next_digit);
346
+ }
347
+ if (result.empty()) {
348
+ result.push_back('0');
349
+ }
350
+ std::reverse(result.begin(), result.end());
351
+ return result;
352
+ }
353
+
354
+ template class BigUnsigned<4>;
355
+ template class BigUnsigned<84>;
356
+
357
+ } // namespace strings_internal
358
+ ABSL_NAMESPACE_END
359
+ } // namespace absl