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,53 @@
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
+ // UTF8 utilities, implemented to reduce dependencies.
16
+
17
+ #include "absl/strings/internal/utf8.h"
18
+
19
+ namespace absl {
20
+ ABSL_NAMESPACE_BEGIN
21
+ namespace strings_internal {
22
+
23
+ size_t EncodeUTF8Char(char *buffer, char32_t utf8_char) {
24
+ if (utf8_char <= 0x7F) {
25
+ *buffer = static_cast<char>(utf8_char);
26
+ return 1;
27
+ } else if (utf8_char <= 0x7FF) {
28
+ buffer[1] = 0x80 | (utf8_char & 0x3F);
29
+ utf8_char >>= 6;
30
+ buffer[0] = 0xC0 | utf8_char;
31
+ return 2;
32
+ } else if (utf8_char <= 0xFFFF) {
33
+ buffer[2] = 0x80 | (utf8_char & 0x3F);
34
+ utf8_char >>= 6;
35
+ buffer[1] = 0x80 | (utf8_char & 0x3F);
36
+ utf8_char >>= 6;
37
+ buffer[0] = 0xE0 | utf8_char;
38
+ return 3;
39
+ } else {
40
+ buffer[3] = 0x80 | (utf8_char & 0x3F);
41
+ utf8_char >>= 6;
42
+ buffer[2] = 0x80 | (utf8_char & 0x3F);
43
+ utf8_char >>= 6;
44
+ buffer[1] = 0x80 | (utf8_char & 0x3F);
45
+ utf8_char >>= 6;
46
+ buffer[0] = 0xF0 | utf8_char;
47
+ return 4;
48
+ }
49
+ }
50
+
51
+ } // namespace strings_internal
52
+ ABSL_NAMESPACE_END
53
+ } // namespace absl
@@ -0,0 +1,50 @@
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
+ // UTF8 utilities, implemented to reduce dependencies.
16
+
17
+ #ifndef ABSL_STRINGS_INTERNAL_UTF8_H_
18
+ #define ABSL_STRINGS_INTERNAL_UTF8_H_
19
+
20
+ #include <cstddef>
21
+ #include <cstdint>
22
+
23
+ #include "absl/base/config.h"
24
+
25
+ namespace absl {
26
+ ABSL_NAMESPACE_BEGIN
27
+ namespace strings_internal {
28
+
29
+ // For Unicode code points 0 through 0x10FFFF, EncodeUTF8Char writes
30
+ // out the UTF-8 encoding into buffer, and returns the number of chars
31
+ // it wrote.
32
+ //
33
+ // As described in https://tools.ietf.org/html/rfc3629#section-3 , the encodings
34
+ // are:
35
+ // 00 - 7F : 0xxxxxxx
36
+ // 80 - 7FF : 110xxxxx 10xxxxxx
37
+ // 800 - FFFF : 1110xxxx 10xxxxxx 10xxxxxx
38
+ // 10000 - 10FFFF : 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
39
+ //
40
+ // Values greater than 0x10FFFF are not supported and may or may not write
41
+ // characters into buffer, however never will more than kMaxEncodedUTF8Size
42
+ // bytes be written, regardless of the value of utf8_char.
43
+ enum { kMaxEncodedUTF8Size = 4 };
44
+ size_t EncodeUTF8Char(char *buffer, char32_t utf8_char);
45
+
46
+ } // namespace strings_internal
47
+ ABSL_NAMESPACE_END
48
+ } // namespace absl
49
+
50
+ #endif // ABSL_STRINGS_INTERNAL_UTF8_H_
@@ -0,0 +1,40 @@
1
+ // Copyright 2017 The Abseil Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #include "absl/strings/match.h"
16
+
17
+ #include "absl/strings/internal/memutil.h"
18
+
19
+ namespace absl {
20
+ ABSL_NAMESPACE_BEGIN
21
+
22
+ bool EqualsIgnoreCase(absl::string_view piece1, absl::string_view piece2) {
23
+ return (piece1.size() == piece2.size() &&
24
+ 0 == absl::strings_internal::memcasecmp(piece1.data(), piece2.data(),
25
+ piece1.size()));
26
+ // memcasecmp uses absl::ascii_tolower().
27
+ }
28
+
29
+ bool StartsWithIgnoreCase(absl::string_view text, absl::string_view prefix) {
30
+ return (text.size() >= prefix.size()) &&
31
+ EqualsIgnoreCase(text.substr(0, prefix.size()), prefix);
32
+ }
33
+
34
+ bool EndsWithIgnoreCase(absl::string_view text, absl::string_view suffix) {
35
+ return (text.size() >= suffix.size()) &&
36
+ EqualsIgnoreCase(text.substr(text.size() - suffix.size()), suffix);
37
+ }
38
+
39
+ ABSL_NAMESPACE_END
40
+ } // namespace absl
@@ -0,0 +1,90 @@
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: match.h
18
+ // -----------------------------------------------------------------------------
19
+ //
20
+ // This file contains simple utilities for performing string matching checks.
21
+ // All of these function parameters are specified as `absl::string_view`,
22
+ // meaning that these functions can accept `std::string`, `absl::string_view` or
23
+ // NUL-terminated C-style strings.
24
+ //
25
+ // Examples:
26
+ // std::string s = "foo";
27
+ // absl::string_view sv = "f";
28
+ // assert(absl::StrContains(s, sv));
29
+ //
30
+ // Note: The order of parameters in these functions is designed to mimic the
31
+ // order an equivalent member function would exhibit;
32
+ // e.g. `s.Contains(x)` ==> `absl::StrContains(s, x).
33
+ #ifndef ABSL_STRINGS_MATCH_H_
34
+ #define ABSL_STRINGS_MATCH_H_
35
+
36
+ #include <cstring>
37
+
38
+ #include "absl/strings/string_view.h"
39
+
40
+ namespace absl {
41
+ ABSL_NAMESPACE_BEGIN
42
+
43
+ // StrContains()
44
+ //
45
+ // Returns whether a given string `haystack` contains the substring `needle`.
46
+ inline bool StrContains(absl::string_view haystack, absl::string_view needle) {
47
+ return haystack.find(needle, 0) != haystack.npos;
48
+ }
49
+
50
+ // StartsWith()
51
+ //
52
+ // Returns whether a given string `text` begins with `prefix`.
53
+ inline bool StartsWith(absl::string_view text, absl::string_view prefix) {
54
+ return prefix.empty() ||
55
+ (text.size() >= prefix.size() &&
56
+ memcmp(text.data(), prefix.data(), prefix.size()) == 0);
57
+ }
58
+
59
+ // EndsWith()
60
+ //
61
+ // Returns whether a given string `text` ends with `suffix`.
62
+ inline bool EndsWith(absl::string_view text, absl::string_view suffix) {
63
+ return suffix.empty() ||
64
+ (text.size() >= suffix.size() &&
65
+ memcmp(text.data() + (text.size() - suffix.size()), suffix.data(),
66
+ suffix.size()) == 0);
67
+ }
68
+
69
+ // EqualsIgnoreCase()
70
+ //
71
+ // Returns whether given ASCII strings `piece1` and `piece2` are equal, ignoring
72
+ // case in the comparison.
73
+ bool EqualsIgnoreCase(absl::string_view piece1, absl::string_view piece2);
74
+
75
+ // StartsWithIgnoreCase()
76
+ //
77
+ // Returns whether a given ASCII string `text` starts with `prefix`,
78
+ // ignoring case in the comparison.
79
+ bool StartsWithIgnoreCase(absl::string_view text, absl::string_view prefix);
80
+
81
+ // EndsWithIgnoreCase()
82
+ //
83
+ // Returns whether a given ASCII string `text` ends with `suffix`, ignoring
84
+ // case in the comparison.
85
+ bool EndsWithIgnoreCase(absl::string_view text, absl::string_view suffix);
86
+
87
+ ABSL_NAMESPACE_END
88
+ } // namespace absl
89
+
90
+ #endif // ABSL_STRINGS_MATCH_H_
@@ -0,0 +1,916 @@
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
+ // This file contains string processing functions related to
16
+ // numeric values.
17
+
18
+ #include "absl/strings/numbers.h"
19
+
20
+ #include <algorithm>
21
+ #include <cassert>
22
+ #include <cfloat> // for DBL_DIG and FLT_DIG
23
+ #include <cmath> // for HUGE_VAL
24
+ #include <cstdint>
25
+ #include <cstdio>
26
+ #include <cstdlib>
27
+ #include <cstring>
28
+ #include <iterator>
29
+ #include <limits>
30
+ #include <memory>
31
+ #include <utility>
32
+
33
+ #include "absl/base/internal/bits.h"
34
+ #include "absl/base/internal/raw_logging.h"
35
+ #include "absl/strings/ascii.h"
36
+ #include "absl/strings/charconv.h"
37
+ #include "absl/strings/escaping.h"
38
+ #include "absl/strings/internal/memutil.h"
39
+ #include "absl/strings/match.h"
40
+ #include "absl/strings/str_cat.h"
41
+
42
+ namespace absl {
43
+ ABSL_NAMESPACE_BEGIN
44
+
45
+ bool SimpleAtof(absl::string_view str, float* out) {
46
+ *out = 0.0;
47
+ str = StripAsciiWhitespace(str);
48
+ if (!str.empty() && str[0] == '+') {
49
+ str.remove_prefix(1);
50
+ }
51
+ auto result = absl::from_chars(str.data(), str.data() + str.size(), *out);
52
+ if (result.ec == std::errc::invalid_argument) {
53
+ return false;
54
+ }
55
+ if (result.ptr != str.data() + str.size()) {
56
+ // not all non-whitespace characters consumed
57
+ return false;
58
+ }
59
+ // from_chars() with DR 3081's current wording will return max() on
60
+ // overflow. SimpleAtof returns infinity instead.
61
+ if (result.ec == std::errc::result_out_of_range) {
62
+ if (*out > 1.0) {
63
+ *out = std::numeric_limits<float>::infinity();
64
+ } else if (*out < -1.0) {
65
+ *out = -std::numeric_limits<float>::infinity();
66
+ }
67
+ }
68
+ return true;
69
+ }
70
+
71
+ bool SimpleAtod(absl::string_view str, double* out) {
72
+ *out = 0.0;
73
+ str = StripAsciiWhitespace(str);
74
+ if (!str.empty() && str[0] == '+') {
75
+ str.remove_prefix(1);
76
+ }
77
+ auto result = absl::from_chars(str.data(), str.data() + str.size(), *out);
78
+ if (result.ec == std::errc::invalid_argument) {
79
+ return false;
80
+ }
81
+ if (result.ptr != str.data() + str.size()) {
82
+ // not all non-whitespace characters consumed
83
+ return false;
84
+ }
85
+ // from_chars() with DR 3081's current wording will return max() on
86
+ // overflow. SimpleAtod returns infinity instead.
87
+ if (result.ec == std::errc::result_out_of_range) {
88
+ if (*out > 1.0) {
89
+ *out = std::numeric_limits<double>::infinity();
90
+ } else if (*out < -1.0) {
91
+ *out = -std::numeric_limits<double>::infinity();
92
+ }
93
+ }
94
+ return true;
95
+ }
96
+
97
+ bool SimpleAtob(absl::string_view str, bool* out) {
98
+ ABSL_RAW_CHECK(out != nullptr, "Output pointer must not be nullptr.");
99
+ if (EqualsIgnoreCase(str, "true") || EqualsIgnoreCase(str, "t") ||
100
+ EqualsIgnoreCase(str, "yes") || EqualsIgnoreCase(str, "y") ||
101
+ EqualsIgnoreCase(str, "1")) {
102
+ *out = true;
103
+ return true;
104
+ }
105
+ if (EqualsIgnoreCase(str, "false") || EqualsIgnoreCase(str, "f") ||
106
+ EqualsIgnoreCase(str, "no") || EqualsIgnoreCase(str, "n") ||
107
+ EqualsIgnoreCase(str, "0")) {
108
+ *out = false;
109
+ return true;
110
+ }
111
+ return false;
112
+ }
113
+
114
+ // ----------------------------------------------------------------------
115
+ // FastIntToBuffer() overloads
116
+ //
117
+ // Like the Fast*ToBuffer() functions above, these are intended for speed.
118
+ // Unlike the Fast*ToBuffer() functions, however, these functions write
119
+ // their output to the beginning of the buffer. The caller is responsible
120
+ // for ensuring that the buffer has enough space to hold the output.
121
+ //
122
+ // Returns a pointer to the end of the string (i.e. the null character
123
+ // terminating the string).
124
+ // ----------------------------------------------------------------------
125
+
126
+ namespace {
127
+
128
+ // Used to optimize printing a decimal number's final digit.
129
+ const char one_ASCII_final_digits[10][2] {
130
+ {'0', 0}, {'1', 0}, {'2', 0}, {'3', 0}, {'4', 0},
131
+ {'5', 0}, {'6', 0}, {'7', 0}, {'8', 0}, {'9', 0},
132
+ };
133
+
134
+ } // namespace
135
+
136
+ char* numbers_internal::FastIntToBuffer(uint32_t i, char* buffer) {
137
+ uint32_t digits;
138
+ // The idea of this implementation is to trim the number of divides to as few
139
+ // as possible, and also reducing memory stores and branches, by going in
140
+ // steps of two digits at a time rather than one whenever possible.
141
+ // The huge-number case is first, in the hopes that the compiler will output
142
+ // that case in one branch-free block of code, and only output conditional
143
+ // branches into it from below.
144
+ if (i >= 1000000000) { // >= 1,000,000,000
145
+ digits = i / 100000000; // 100,000,000
146
+ i -= digits * 100000000;
147
+ PutTwoDigits(digits, buffer);
148
+ buffer += 2;
149
+ lt100_000_000:
150
+ digits = i / 1000000; // 1,000,000
151
+ i -= digits * 1000000;
152
+ PutTwoDigits(digits, buffer);
153
+ buffer += 2;
154
+ lt1_000_000:
155
+ digits = i / 10000; // 10,000
156
+ i -= digits * 10000;
157
+ PutTwoDigits(digits, buffer);
158
+ buffer += 2;
159
+ lt10_000:
160
+ digits = i / 100;
161
+ i -= digits * 100;
162
+ PutTwoDigits(digits, buffer);
163
+ buffer += 2;
164
+ lt100:
165
+ digits = i;
166
+ PutTwoDigits(digits, buffer);
167
+ buffer += 2;
168
+ *buffer = 0;
169
+ return buffer;
170
+ }
171
+
172
+ if (i < 100) {
173
+ digits = i;
174
+ if (i >= 10) goto lt100;
175
+ memcpy(buffer, one_ASCII_final_digits[i], 2);
176
+ return buffer + 1;
177
+ }
178
+ if (i < 10000) { // 10,000
179
+ if (i >= 1000) goto lt10_000;
180
+ digits = i / 100;
181
+ i -= digits * 100;
182
+ *buffer++ = '0' + digits;
183
+ goto lt100;
184
+ }
185
+ if (i < 1000000) { // 1,000,000
186
+ if (i >= 100000) goto lt1_000_000;
187
+ digits = i / 10000; // 10,000
188
+ i -= digits * 10000;
189
+ *buffer++ = '0' + digits;
190
+ goto lt10_000;
191
+ }
192
+ if (i < 100000000) { // 100,000,000
193
+ if (i >= 10000000) goto lt100_000_000;
194
+ digits = i / 1000000; // 1,000,000
195
+ i -= digits * 1000000;
196
+ *buffer++ = '0' + digits;
197
+ goto lt1_000_000;
198
+ }
199
+ // we already know that i < 1,000,000,000
200
+ digits = i / 100000000; // 100,000,000
201
+ i -= digits * 100000000;
202
+ *buffer++ = '0' + digits;
203
+ goto lt100_000_000;
204
+ }
205
+
206
+ char* numbers_internal::FastIntToBuffer(int32_t i, char* buffer) {
207
+ uint32_t u = i;
208
+ if (i < 0) {
209
+ *buffer++ = '-';
210
+ // We need to do the negation in modular (i.e., "unsigned")
211
+ // arithmetic; MSVC++ apprently warns for plain "-u", so
212
+ // we write the equivalent expression "0 - u" instead.
213
+ u = 0 - u;
214
+ }
215
+ return numbers_internal::FastIntToBuffer(u, buffer);
216
+ }
217
+
218
+ char* numbers_internal::FastIntToBuffer(uint64_t i, char* buffer) {
219
+ uint32_t u32 = static_cast<uint32_t>(i);
220
+ if (u32 == i) return numbers_internal::FastIntToBuffer(u32, buffer);
221
+
222
+ // Here we know i has at least 10 decimal digits.
223
+ uint64_t top_1to11 = i / 1000000000;
224
+ u32 = static_cast<uint32_t>(i - top_1to11 * 1000000000);
225
+ uint32_t top_1to11_32 = static_cast<uint32_t>(top_1to11);
226
+
227
+ if (top_1to11_32 == top_1to11) {
228
+ buffer = numbers_internal::FastIntToBuffer(top_1to11_32, buffer);
229
+ } else {
230
+ // top_1to11 has more than 32 bits too; print it in two steps.
231
+ uint32_t top_8to9 = static_cast<uint32_t>(top_1to11 / 100);
232
+ uint32_t mid_2 = static_cast<uint32_t>(top_1to11 - top_8to9 * 100);
233
+ buffer = numbers_internal::FastIntToBuffer(top_8to9, buffer);
234
+ PutTwoDigits(mid_2, buffer);
235
+ buffer += 2;
236
+ }
237
+
238
+ // We have only 9 digits now, again the maximum uint32_t can handle fully.
239
+ uint32_t digits = u32 / 10000000; // 10,000,000
240
+ u32 -= digits * 10000000;
241
+ PutTwoDigits(digits, buffer);
242
+ buffer += 2;
243
+ digits = u32 / 100000; // 100,000
244
+ u32 -= digits * 100000;
245
+ PutTwoDigits(digits, buffer);
246
+ buffer += 2;
247
+ digits = u32 / 1000; // 1,000
248
+ u32 -= digits * 1000;
249
+ PutTwoDigits(digits, buffer);
250
+ buffer += 2;
251
+ digits = u32 / 10;
252
+ u32 -= digits * 10;
253
+ PutTwoDigits(digits, buffer);
254
+ buffer += 2;
255
+ memcpy(buffer, one_ASCII_final_digits[u32], 2);
256
+ return buffer + 1;
257
+ }
258
+
259
+ char* numbers_internal::FastIntToBuffer(int64_t i, char* buffer) {
260
+ uint64_t u = i;
261
+ if (i < 0) {
262
+ *buffer++ = '-';
263
+ u = 0 - u;
264
+ }
265
+ return numbers_internal::FastIntToBuffer(u, buffer);
266
+ }
267
+
268
+ // Given a 128-bit number expressed as a pair of uint64_t, high half first,
269
+ // return that number multiplied by the given 32-bit value. If the result is
270
+ // too large to fit in a 128-bit number, divide it by 2 until it fits.
271
+ static std::pair<uint64_t, uint64_t> Mul32(std::pair<uint64_t, uint64_t> num,
272
+ uint32_t mul) {
273
+ uint64_t bits0_31 = num.second & 0xFFFFFFFF;
274
+ uint64_t bits32_63 = num.second >> 32;
275
+ uint64_t bits64_95 = num.first & 0xFFFFFFFF;
276
+ uint64_t bits96_127 = num.first >> 32;
277
+
278
+ // The picture so far: each of these 64-bit values has only the lower 32 bits
279
+ // filled in.
280
+ // bits96_127: [ 00000000 xxxxxxxx ]
281
+ // bits64_95: [ 00000000 xxxxxxxx ]
282
+ // bits32_63: [ 00000000 xxxxxxxx ]
283
+ // bits0_31: [ 00000000 xxxxxxxx ]
284
+
285
+ bits0_31 *= mul;
286
+ bits32_63 *= mul;
287
+ bits64_95 *= mul;
288
+ bits96_127 *= mul;
289
+
290
+ // Now the top halves may also have value, though all 64 of their bits will
291
+ // never be set at the same time, since they are a result of a 32x32 bit
292
+ // multiply. This makes the carry calculation slightly easier.
293
+ // bits96_127: [ mmmmmmmm | mmmmmmmm ]
294
+ // bits64_95: [ | mmmmmmmm mmmmmmmm | ]
295
+ // bits32_63: | [ mmmmmmmm | mmmmmmmm ]
296
+ // bits0_31: | [ | mmmmmmmm mmmmmmmm ]
297
+ // eventually: [ bits128_up | ...bits64_127.... | ..bits0_63... ]
298
+
299
+ uint64_t bits0_63 = bits0_31 + (bits32_63 << 32);
300
+ uint64_t bits64_127 = bits64_95 + (bits96_127 << 32) + (bits32_63 >> 32) +
301
+ (bits0_63 < bits0_31);
302
+ uint64_t bits128_up = (bits96_127 >> 32) + (bits64_127 < bits64_95);
303
+ if (bits128_up == 0) return {bits64_127, bits0_63};
304
+
305
+ int shift = 64 - base_internal::CountLeadingZeros64(bits128_up);
306
+ uint64_t lo = (bits0_63 >> shift) + (bits64_127 << (64 - shift));
307
+ uint64_t hi = (bits64_127 >> shift) + (bits128_up << (64 - shift));
308
+ return {hi, lo};
309
+ }
310
+
311
+ // Compute num * 5 ^ expfive, and return the first 128 bits of the result,
312
+ // where the first bit is always a one. So PowFive(1, 0) starts 0b100000,
313
+ // PowFive(1, 1) starts 0b101000, PowFive(1, 2) starts 0b110010, etc.
314
+ static std::pair<uint64_t, uint64_t> PowFive(uint64_t num, int expfive) {
315
+ std::pair<uint64_t, uint64_t> result = {num, 0};
316
+ while (expfive >= 13) {
317
+ // 5^13 is the highest power of five that will fit in a 32-bit integer.
318
+ result = Mul32(result, 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5);
319
+ expfive -= 13;
320
+ }
321
+ constexpr int powers_of_five[13] = {
322
+ 1,
323
+ 5,
324
+ 5 * 5,
325
+ 5 * 5 * 5,
326
+ 5 * 5 * 5 * 5,
327
+ 5 * 5 * 5 * 5 * 5,
328
+ 5 * 5 * 5 * 5 * 5 * 5,
329
+ 5 * 5 * 5 * 5 * 5 * 5 * 5,
330
+ 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
331
+ 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
332
+ 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
333
+ 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
334
+ 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5};
335
+ result = Mul32(result, powers_of_five[expfive & 15]);
336
+ int shift = base_internal::CountLeadingZeros64(result.first);
337
+ if (shift != 0) {
338
+ result.first = (result.first << shift) + (result.second >> (64 - shift));
339
+ result.second = (result.second << shift);
340
+ }
341
+ return result;
342
+ }
343
+
344
+ struct ExpDigits {
345
+ int32_t exponent;
346
+ char digits[6];
347
+ };
348
+
349
+ // SplitToSix converts value, a positive double-precision floating-point number,
350
+ // into a base-10 exponent and 6 ASCII digits, where the first digit is never
351
+ // zero. For example, SplitToSix(1) returns an exponent of zero and a digits
352
+ // array of {'1', '0', '0', '0', '0', '0'}. If value is exactly halfway between
353
+ // two possible representations, e.g. value = 100000.5, then "round to even" is
354
+ // performed.
355
+ static ExpDigits SplitToSix(const double value) {
356
+ ExpDigits exp_dig;
357
+ int exp = 5;
358
+ double d = value;
359
+ // First step: calculate a close approximation of the output, where the
360
+ // value d will be between 100,000 and 999,999, representing the digits
361
+ // in the output ASCII array, and exp is the base-10 exponent. It would be
362
+ // faster to use a table here, and to look up the base-2 exponent of value,
363
+ // however value is an IEEE-754 64-bit number, so the table would have 2,000
364
+ // entries, which is not cache-friendly.
365
+ if (d >= 999999.5) {
366
+ if (d >= 1e+261) exp += 256, d *= 1e-256;
367
+ if (d >= 1e+133) exp += 128, d *= 1e-128;
368
+ if (d >= 1e+69) exp += 64, d *= 1e-64;
369
+ if (d >= 1e+37) exp += 32, d *= 1e-32;
370
+ if (d >= 1e+21) exp += 16, d *= 1e-16;
371
+ if (d >= 1e+13) exp += 8, d *= 1e-8;
372
+ if (d >= 1e+9) exp += 4, d *= 1e-4;
373
+ if (d >= 1e+7) exp += 2, d *= 1e-2;
374
+ if (d >= 1e+6) exp += 1, d *= 1e-1;
375
+ } else {
376
+ if (d < 1e-250) exp -= 256, d *= 1e256;
377
+ if (d < 1e-122) exp -= 128, d *= 1e128;
378
+ if (d < 1e-58) exp -= 64, d *= 1e64;
379
+ if (d < 1e-26) exp -= 32, d *= 1e32;
380
+ if (d < 1e-10) exp -= 16, d *= 1e16;
381
+ if (d < 1e-2) exp -= 8, d *= 1e8;
382
+ if (d < 1e+2) exp -= 4, d *= 1e4;
383
+ if (d < 1e+4) exp -= 2, d *= 1e2;
384
+ if (d < 1e+5) exp -= 1, d *= 1e1;
385
+ }
386
+ // At this point, d is in the range [99999.5..999999.5) and exp is in the
387
+ // range [-324..308]. Since we need to round d up, we want to add a half
388
+ // and truncate.
389
+ // However, the technique above may have lost some precision, due to its
390
+ // repeated multiplication by constants that each may be off by half a bit
391
+ // of precision. This only matters if we're close to the edge though.
392
+ // Since we'd like to know if the fractional part of d is close to a half,
393
+ // we multiply it by 65536 and see if the fractional part is close to 32768.
394
+ // (The number doesn't have to be a power of two,but powers of two are faster)
395
+ uint64_t d64k = d * 65536;
396
+ int dddddd; // A 6-digit decimal integer.
397
+ if ((d64k % 65536) == 32767 || (d64k % 65536) == 32768) {
398
+ // OK, it's fairly likely that precision was lost above, which is
399
+ // not a surprise given only 52 mantissa bits are available. Therefore
400
+ // redo the calculation using 128-bit numbers. (64 bits are not enough).
401
+
402
+ // Start out with digits rounded down; maybe add one below.
403
+ dddddd = static_cast<int>(d64k / 65536);
404
+
405
+ // mantissa is a 64-bit integer representing M.mmm... * 2^63. The actual
406
+ // value we're representing, of course, is M.mmm... * 2^exp2.
407
+ int exp2;
408
+ double m = std::frexp(value, &exp2);
409
+ uint64_t mantissa = m * (32768.0 * 65536.0 * 65536.0 * 65536.0);
410
+ // std::frexp returns an m value in the range [0.5, 1.0), however we
411
+ // can't multiply it by 2^64 and convert to an integer because some FPUs
412
+ // throw an exception when converting an number higher than 2^63 into an
413
+ // integer - even an unsigned 64-bit integer! Fortunately it doesn't matter
414
+ // since m only has 52 significant bits anyway.
415
+ mantissa <<= 1;
416
+ exp2 -= 64; // not needed, but nice for debugging
417
+
418
+ // OK, we are here to compare:
419
+ // (dddddd + 0.5) * 10^(exp-5) vs. mantissa * 2^exp2
420
+ // so we can round up dddddd if appropriate. Those values span the full
421
+ // range of 600 orders of magnitude of IEE 64-bit floating-point.
422
+ // Fortunately, we already know they are very close, so we don't need to
423
+ // track the base-2 exponent of both sides. This greatly simplifies the
424
+ // the math since the 2^exp2 calculation is unnecessary and the power-of-10
425
+ // calculation can become a power-of-5 instead.
426
+
427
+ std::pair<uint64_t, uint64_t> edge, val;
428
+ if (exp >= 6) {
429
+ // Compare (dddddd + 0.5) * 5 ^ (exp - 5) to mantissa
430
+ // Since we're tossing powers of two, 2 * dddddd + 1 is the
431
+ // same as dddddd + 0.5
432
+ edge = PowFive(2 * dddddd + 1, exp - 5);
433
+
434
+ val.first = mantissa;
435
+ val.second = 0;
436
+ } else {
437
+ // We can't compare (dddddd + 0.5) * 5 ^ (exp - 5) to mantissa as we did
438
+ // above because (exp - 5) is negative. So we compare (dddddd + 0.5) to
439
+ // mantissa * 5 ^ (5 - exp)
440
+ edge = PowFive(2 * dddddd + 1, 0);
441
+
442
+ val = PowFive(mantissa, 5 - exp);
443
+ }
444
+ // printf("exp=%d %016lx %016lx vs %016lx %016lx\n", exp, val.first,
445
+ // val.second, edge.first, edge.second);
446
+ if (val > edge) {
447
+ dddddd++;
448
+ } else if (val == edge) {
449
+ dddddd += (dddddd & 1);
450
+ }
451
+ } else {
452
+ // Here, we are not close to the edge.
453
+ dddddd = static_cast<int>((d64k + 32768) / 65536);
454
+ }
455
+ if (dddddd == 1000000) {
456
+ dddddd = 100000;
457
+ exp += 1;
458
+ }
459
+ exp_dig.exponent = exp;
460
+
461
+ int two_digits = dddddd / 10000;
462
+ dddddd -= two_digits * 10000;
463
+ numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[0]);
464
+
465
+ two_digits = dddddd / 100;
466
+ dddddd -= two_digits * 100;
467
+ numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[2]);
468
+
469
+ numbers_internal::PutTwoDigits(dddddd, &exp_dig.digits[4]);
470
+ return exp_dig;
471
+ }
472
+
473
+ // Helper function for fast formatting of floating-point.
474
+ // The result is the same as "%g", a.k.a. "%.6g".
475
+ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
476
+ static_assert(std::numeric_limits<float>::is_iec559,
477
+ "IEEE-754/IEC-559 support only");
478
+
479
+ char* out = buffer; // we write data to out, incrementing as we go, but
480
+ // FloatToBuffer always returns the address of the buffer
481
+ // passed in.
482
+
483
+ if (std::isnan(d)) {
484
+ strcpy(out, "nan"); // NOLINT(runtime/printf)
485
+ return 3;
486
+ }
487
+ if (d == 0) { // +0 and -0 are handled here
488
+ if (std::signbit(d)) *out++ = '-';
489
+ *out++ = '0';
490
+ *out = 0;
491
+ return out - buffer;
492
+ }
493
+ if (d < 0) {
494
+ *out++ = '-';
495
+ d = -d;
496
+ }
497
+ if (std::isinf(d)) {
498
+ strcpy(out, "inf"); // NOLINT(runtime/printf)
499
+ return out + 3 - buffer;
500
+ }
501
+
502
+ auto exp_dig = SplitToSix(d);
503
+ int exp = exp_dig.exponent;
504
+ const char* digits = exp_dig.digits;
505
+ out[0] = '0';
506
+ out[1] = '.';
507
+ switch (exp) {
508
+ case 5:
509
+ memcpy(out, &digits[0], 6), out += 6;
510
+ *out = 0;
511
+ return out - buffer;
512
+ case 4:
513
+ memcpy(out, &digits[0], 5), out += 5;
514
+ if (digits[5] != '0') {
515
+ *out++ = '.';
516
+ *out++ = digits[5];
517
+ }
518
+ *out = 0;
519
+ return out - buffer;
520
+ case 3:
521
+ memcpy(out, &digits[0], 4), out += 4;
522
+ if ((digits[5] | digits[4]) != '0') {
523
+ *out++ = '.';
524
+ *out++ = digits[4];
525
+ if (digits[5] != '0') *out++ = digits[5];
526
+ }
527
+ *out = 0;
528
+ return out - buffer;
529
+ case 2:
530
+ memcpy(out, &digits[0], 3), out += 3;
531
+ *out++ = '.';
532
+ memcpy(out, &digits[3], 3);
533
+ out += 3;
534
+ while (out[-1] == '0') --out;
535
+ if (out[-1] == '.') --out;
536
+ *out = 0;
537
+ return out - buffer;
538
+ case 1:
539
+ memcpy(out, &digits[0], 2), out += 2;
540
+ *out++ = '.';
541
+ memcpy(out, &digits[2], 4);
542
+ out += 4;
543
+ while (out[-1] == '0') --out;
544
+ if (out[-1] == '.') --out;
545
+ *out = 0;
546
+ return out - buffer;
547
+ case 0:
548
+ memcpy(out, &digits[0], 1), out += 1;
549
+ *out++ = '.';
550
+ memcpy(out, &digits[1], 5);
551
+ out += 5;
552
+ while (out[-1] == '0') --out;
553
+ if (out[-1] == '.') --out;
554
+ *out = 0;
555
+ return out - buffer;
556
+ case -4:
557
+ out[2] = '0';
558
+ ++out;
559
+ ABSL_FALLTHROUGH_INTENDED;
560
+ case -3:
561
+ out[2] = '0';
562
+ ++out;
563
+ ABSL_FALLTHROUGH_INTENDED;
564
+ case -2:
565
+ out[2] = '0';
566
+ ++out;
567
+ ABSL_FALLTHROUGH_INTENDED;
568
+ case -1:
569
+ out += 2;
570
+ memcpy(out, &digits[0], 6);
571
+ out += 6;
572
+ while (out[-1] == '0') --out;
573
+ *out = 0;
574
+ return out - buffer;
575
+ }
576
+ assert(exp < -4 || exp >= 6);
577
+ out[0] = digits[0];
578
+ assert(out[1] == '.');
579
+ out += 2;
580
+ memcpy(out, &digits[1], 5), out += 5;
581
+ while (out[-1] == '0') --out;
582
+ if (out[-1] == '.') --out;
583
+ *out++ = 'e';
584
+ if (exp > 0) {
585
+ *out++ = '+';
586
+ } else {
587
+ *out++ = '-';
588
+ exp = -exp;
589
+ }
590
+ if (exp > 99) {
591
+ int dig1 = exp / 100;
592
+ exp -= dig1 * 100;
593
+ *out++ = '0' + dig1;
594
+ }
595
+ PutTwoDigits(exp, out);
596
+ out += 2;
597
+ *out = 0;
598
+ return out - buffer;
599
+ }
600
+
601
+ namespace {
602
+ // Represents integer values of digits.
603
+ // Uses 36 to indicate an invalid character since we support
604
+ // bases up to 36.
605
+ static const int8_t kAsciiToInt[256] = {
606
+ 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, // 16 36s.
607
+ 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
608
+ 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 0, 1, 2, 3, 4, 5,
609
+ 6, 7, 8, 9, 36, 36, 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17,
610
+ 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
611
+ 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
612
+ 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 36, 36, 36, 36, 36, 36,
613
+ 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
614
+ 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
615
+ 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
616
+ 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
617
+ 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
618
+ 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
619
+ 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36};
620
+
621
+ // Parse the sign and optional hex or oct prefix in text.
622
+ inline bool safe_parse_sign_and_base(absl::string_view* text /*inout*/,
623
+ int* base_ptr /*inout*/,
624
+ bool* negative_ptr /*output*/) {
625
+ if (text->data() == nullptr) {
626
+ return false;
627
+ }
628
+
629
+ const char* start = text->data();
630
+ const char* end = start + text->size();
631
+ int base = *base_ptr;
632
+
633
+ // Consume whitespace.
634
+ while (start < end && absl::ascii_isspace(start[0])) {
635
+ ++start;
636
+ }
637
+ while (start < end && absl::ascii_isspace(end[-1])) {
638
+ --end;
639
+ }
640
+ if (start >= end) {
641
+ return false;
642
+ }
643
+
644
+ // Consume sign.
645
+ *negative_ptr = (start[0] == '-');
646
+ if (*negative_ptr || start[0] == '+') {
647
+ ++start;
648
+ if (start >= end) {
649
+ return false;
650
+ }
651
+ }
652
+
653
+ // Consume base-dependent prefix.
654
+ // base 0: "0x" -> base 16, "0" -> base 8, default -> base 10
655
+ // base 16: "0x" -> base 16
656
+ // Also validate the base.
657
+ if (base == 0) {
658
+ if (end - start >= 2 && start[0] == '0' &&
659
+ (start[1] == 'x' || start[1] == 'X')) {
660
+ base = 16;
661
+ start += 2;
662
+ if (start >= end) {
663
+ // "0x" with no digits after is invalid.
664
+ return false;
665
+ }
666
+ } else if (end - start >= 1 && start[0] == '0') {
667
+ base = 8;
668
+ start += 1;
669
+ } else {
670
+ base = 10;
671
+ }
672
+ } else if (base == 16) {
673
+ if (end - start >= 2 && start[0] == '0' &&
674
+ (start[1] == 'x' || start[1] == 'X')) {
675
+ start += 2;
676
+ if (start >= end) {
677
+ // "0x" with no digits after is invalid.
678
+ return false;
679
+ }
680
+ }
681
+ } else if (base >= 2 && base <= 36) {
682
+ // okay
683
+ } else {
684
+ return false;
685
+ }
686
+ *text = absl::string_view(start, end - start);
687
+ *base_ptr = base;
688
+ return true;
689
+ }
690
+
691
+ // Consume digits.
692
+ //
693
+ // The classic loop:
694
+ //
695
+ // for each digit
696
+ // value = value * base + digit
697
+ // value *= sign
698
+ //
699
+ // The classic loop needs overflow checking. It also fails on the most
700
+ // negative integer, -2147483648 in 32-bit two's complement representation.
701
+ //
702
+ // My improved loop:
703
+ //
704
+ // if (!negative)
705
+ // for each digit
706
+ // value = value * base
707
+ // value = value + digit
708
+ // else
709
+ // for each digit
710
+ // value = value * base
711
+ // value = value - digit
712
+ //
713
+ // Overflow checking becomes simple.
714
+
715
+ // Lookup tables per IntType:
716
+ // vmax/base and vmin/base are precomputed because division costs at least 8ns.
717
+ // TODO(junyer): Doing this per base instead (i.e. an array of structs, not a
718
+ // struct of arrays) would probably be better in terms of d-cache for the most
719
+ // commonly used bases.
720
+ template <typename IntType>
721
+ struct LookupTables {
722
+ static const IntType kVmaxOverBase[];
723
+ static const IntType kVminOverBase[];
724
+ };
725
+
726
+ // An array initializer macro for X/base where base in [0, 36].
727
+ // However, note that lookups for base in [0, 1] should never happen because
728
+ // base has been validated to be in [2, 36] by safe_parse_sign_and_base().
729
+ #define X_OVER_BASE_INITIALIZER(X) \
730
+ { \
731
+ 0, 0, X / 2, X / 3, X / 4, X / 5, X / 6, X / 7, X / 8, X / 9, X / 10, \
732
+ X / 11, X / 12, X / 13, X / 14, X / 15, X / 16, X / 17, X / 18, \
733
+ X / 19, X / 20, X / 21, X / 22, X / 23, X / 24, X / 25, X / 26, \
734
+ X / 27, X / 28, X / 29, X / 30, X / 31, X / 32, X / 33, X / 34, \
735
+ X / 35, X / 36, \
736
+ }
737
+
738
+ template <typename IntType>
739
+ const IntType LookupTables<IntType>::kVmaxOverBase[] =
740
+ X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::max());
741
+
742
+ template <typename IntType>
743
+ const IntType LookupTables<IntType>::kVminOverBase[] =
744
+ X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::min());
745
+
746
+ #undef X_OVER_BASE_INITIALIZER
747
+
748
+ template <typename IntType>
749
+ inline bool safe_parse_positive_int(absl::string_view text, int base,
750
+ IntType* value_p) {
751
+ IntType value = 0;
752
+ const IntType vmax = std::numeric_limits<IntType>::max();
753
+ assert(vmax > 0);
754
+ assert(base >= 0);
755
+ assert(vmax >= static_cast<IntType>(base));
756
+ const IntType vmax_over_base = LookupTables<IntType>::kVmaxOverBase[base];
757
+ const char* start = text.data();
758
+ const char* end = start + text.size();
759
+ // loop over digits
760
+ for (; start < end; ++start) {
761
+ unsigned char c = static_cast<unsigned char>(start[0]);
762
+ int digit = kAsciiToInt[c];
763
+ if (digit >= base) {
764
+ *value_p = value;
765
+ return false;
766
+ }
767
+ if (value > vmax_over_base) {
768
+ *value_p = vmax;
769
+ return false;
770
+ }
771
+ value *= base;
772
+ if (value > vmax - digit) {
773
+ *value_p = vmax;
774
+ return false;
775
+ }
776
+ value += digit;
777
+ }
778
+ *value_p = value;
779
+ return true;
780
+ }
781
+
782
+ template <typename IntType>
783
+ inline bool safe_parse_negative_int(absl::string_view text, int base,
784
+ IntType* value_p) {
785
+ IntType value = 0;
786
+ const IntType vmin = std::numeric_limits<IntType>::min();
787
+ assert(vmin < 0);
788
+ assert(vmin <= 0 - base);
789
+ IntType vmin_over_base = LookupTables<IntType>::kVminOverBase[base];
790
+ // 2003 c++ standard [expr.mul]
791
+ // "... the sign of the remainder is implementation-defined."
792
+ // Although (vmin/base)*base + vmin%base is always vmin.
793
+ // 2011 c++ standard tightens the spec but we cannot rely on it.
794
+ // TODO(junyer): Handle this in the lookup table generation.
795
+ if (vmin % base > 0) {
796
+ vmin_over_base += 1;
797
+ }
798
+ const char* start = text.data();
799
+ const char* end = start + text.size();
800
+ // loop over digits
801
+ for (; start < end; ++start) {
802
+ unsigned char c = static_cast<unsigned char>(start[0]);
803
+ int digit = kAsciiToInt[c];
804
+ if (digit >= base) {
805
+ *value_p = value;
806
+ return false;
807
+ }
808
+ if (value < vmin_over_base) {
809
+ *value_p = vmin;
810
+ return false;
811
+ }
812
+ value *= base;
813
+ if (value < vmin + digit) {
814
+ *value_p = vmin;
815
+ return false;
816
+ }
817
+ value -= digit;
818
+ }
819
+ *value_p = value;
820
+ return true;
821
+ }
822
+
823
+ // Input format based on POSIX.1-2008 strtol
824
+ // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html
825
+ template <typename IntType>
826
+ inline bool safe_int_internal(absl::string_view text, IntType* value_p,
827
+ int base) {
828
+ *value_p = 0;
829
+ bool negative;
830
+ if (!safe_parse_sign_and_base(&text, &base, &negative)) {
831
+ return false;
832
+ }
833
+ if (!negative) {
834
+ return safe_parse_positive_int(text, base, value_p);
835
+ } else {
836
+ return safe_parse_negative_int(text, base, value_p);
837
+ }
838
+ }
839
+
840
+ template <typename IntType>
841
+ inline bool safe_uint_internal(absl::string_view text, IntType* value_p,
842
+ int base) {
843
+ *value_p = 0;
844
+ bool negative;
845
+ if (!safe_parse_sign_and_base(&text, &base, &negative) || negative) {
846
+ return false;
847
+ }
848
+ return safe_parse_positive_int(text, base, value_p);
849
+ }
850
+ } // anonymous namespace
851
+
852
+ namespace numbers_internal {
853
+
854
+ // Digit conversion.
855
+ ABSL_CONST_INIT const char kHexChar[] = "0123456789abcdef";
856
+
857
+ ABSL_CONST_INIT const char kHexTable[513] =
858
+ "000102030405060708090a0b0c0d0e0f"
859
+ "101112131415161718191a1b1c1d1e1f"
860
+ "202122232425262728292a2b2c2d2e2f"
861
+ "303132333435363738393a3b3c3d3e3f"
862
+ "404142434445464748494a4b4c4d4e4f"
863
+ "505152535455565758595a5b5c5d5e5f"
864
+ "606162636465666768696a6b6c6d6e6f"
865
+ "707172737475767778797a7b7c7d7e7f"
866
+ "808182838485868788898a8b8c8d8e8f"
867
+ "909192939495969798999a9b9c9d9e9f"
868
+ "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
869
+ "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
870
+ "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
871
+ "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
872
+ "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
873
+ "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
874
+
875
+ ABSL_CONST_INIT const char two_ASCII_digits[100][2] = {
876
+ {'0', '0'}, {'0', '1'}, {'0', '2'}, {'0', '3'}, {'0', '4'}, {'0', '5'},
877
+ {'0', '6'}, {'0', '7'}, {'0', '8'}, {'0', '9'}, {'1', '0'}, {'1', '1'},
878
+ {'1', '2'}, {'1', '3'}, {'1', '4'}, {'1', '5'}, {'1', '6'}, {'1', '7'},
879
+ {'1', '8'}, {'1', '9'}, {'2', '0'}, {'2', '1'}, {'2', '2'}, {'2', '3'},
880
+ {'2', '4'}, {'2', '5'}, {'2', '6'}, {'2', '7'}, {'2', '8'}, {'2', '9'},
881
+ {'3', '0'}, {'3', '1'}, {'3', '2'}, {'3', '3'}, {'3', '4'}, {'3', '5'},
882
+ {'3', '6'}, {'3', '7'}, {'3', '8'}, {'3', '9'}, {'4', '0'}, {'4', '1'},
883
+ {'4', '2'}, {'4', '3'}, {'4', '4'}, {'4', '5'}, {'4', '6'}, {'4', '7'},
884
+ {'4', '8'}, {'4', '9'}, {'5', '0'}, {'5', '1'}, {'5', '2'}, {'5', '3'},
885
+ {'5', '4'}, {'5', '5'}, {'5', '6'}, {'5', '7'}, {'5', '8'}, {'5', '9'},
886
+ {'6', '0'}, {'6', '1'}, {'6', '2'}, {'6', '3'}, {'6', '4'}, {'6', '5'},
887
+ {'6', '6'}, {'6', '7'}, {'6', '8'}, {'6', '9'}, {'7', '0'}, {'7', '1'},
888
+ {'7', '2'}, {'7', '3'}, {'7', '4'}, {'7', '5'}, {'7', '6'}, {'7', '7'},
889
+ {'7', '8'}, {'7', '9'}, {'8', '0'}, {'8', '1'}, {'8', '2'}, {'8', '3'},
890
+ {'8', '4'}, {'8', '5'}, {'8', '6'}, {'8', '7'}, {'8', '8'}, {'8', '9'},
891
+ {'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'},
892
+ {'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'}};
893
+
894
+ bool safe_strto32_base(absl::string_view text, int32_t* value, int base) {
895
+ return safe_int_internal<int32_t>(text, value, base);
896
+ }
897
+
898
+ bool safe_strto64_base(absl::string_view text, int64_t* value, int base) {
899
+ return safe_int_internal<int64_t>(text, value, base);
900
+ }
901
+
902
+ bool safe_strtou32_base(absl::string_view text, uint32_t* value, int base) {
903
+ return safe_uint_internal<uint32_t>(text, value, base);
904
+ }
905
+
906
+ bool safe_strtou64_base(absl::string_view text, uint64_t* value, int base) {
907
+ return safe_uint_internal<uint64_t>(text, value, base);
908
+ }
909
+
910
+ bool safe_strtou128_base(absl::string_view text, uint128* value, int base) {
911
+ return safe_uint_internal<absl::uint128>(text, value, base);
912
+ }
913
+
914
+ } // namespace numbers_internal
915
+ ABSL_NAMESPACE_END
916
+ } // namespace absl