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,263 @@
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: numbers.h
18
+ // -----------------------------------------------------------------------------
19
+ //
20
+ // This package contains functions for converting strings to numbers. For
21
+ // converting numbers to strings, use `StrCat()` or `StrAppend()` in str_cat.h,
22
+ // which automatically detect and convert most number values appropriately.
23
+
24
+ #ifndef ABSL_STRINGS_NUMBERS_H_
25
+ #define ABSL_STRINGS_NUMBERS_H_
26
+
27
+ #ifdef __SSE4_2__
28
+ #include <x86intrin.h>
29
+ #endif
30
+
31
+ #include <cstddef>
32
+ #include <cstdlib>
33
+ #include <cstring>
34
+ #include <ctime>
35
+ #include <limits>
36
+ #include <string>
37
+ #include <type_traits>
38
+
39
+ #include "absl/base/internal/bits.h"
40
+ #ifdef __SSE4_2__
41
+ // TODO(jorg): Remove this when we figure out the right way
42
+ // to swap bytes on SSE 4.2 that works with the compilers
43
+ // we claim to support. Also, add tests for the compiler
44
+ // that doesn't support the Intel _bswap64 intrinsic but
45
+ // does support all the SSE 4.2 intrinsics
46
+ #include "absl/base/internal/endian.h"
47
+ #endif
48
+ #include "absl/base/macros.h"
49
+ #include "absl/base/port.h"
50
+ #include "absl/numeric/int128.h"
51
+ #include "absl/strings/string_view.h"
52
+
53
+ namespace absl {
54
+ ABSL_NAMESPACE_BEGIN
55
+
56
+ // SimpleAtoi()
57
+ //
58
+ // Converts the given string (optionally followed or preceded by ASCII
59
+ // whitespace) into an integer value, returning `true` if successful. The string
60
+ // must reflect a base-10 integer whose value falls within the range of the
61
+ // integer type (optionally preceded by a `+` or `-`). If any errors are
62
+ // encountered, this function returns `false`, leaving `out` in an unspecified
63
+ // state.
64
+ template <typename int_type>
65
+ ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str, int_type* out);
66
+
67
+ // SimpleAtof()
68
+ //
69
+ // Converts the given string (optionally followed or preceded by ASCII
70
+ // whitespace) into a float, which may be rounded on overflow or underflow,
71
+ // returning `true` if successful.
72
+ // See https://en.cppreference.com/w/c/string/byte/strtof for details about the
73
+ // allowed formats for `str`, except SimpleAtof() is locale-independent and will
74
+ // always use the "C" locale. If any errors are encountered, this function
75
+ // returns `false`, leaving `out` in an unspecified state.
76
+ ABSL_MUST_USE_RESULT bool SimpleAtof(absl::string_view str, float* out);
77
+
78
+ // SimpleAtod()
79
+ //
80
+ // Converts the given string (optionally followed or preceded by ASCII
81
+ // whitespace) into a double, which may be rounded on overflow or underflow,
82
+ // returning `true` if successful.
83
+ // See https://en.cppreference.com/w/c/string/byte/strtof for details about the
84
+ // allowed formats for `str`, except SimpleAtod is locale-independent and will
85
+ // always use the "C" locale. If any errors are encountered, this function
86
+ // returns `false`, leaving `out` in an unspecified state.
87
+ ABSL_MUST_USE_RESULT bool SimpleAtod(absl::string_view str, double* out);
88
+
89
+ // SimpleAtob()
90
+ //
91
+ // Converts the given string into a boolean, returning `true` if successful.
92
+ // The following case-insensitive strings are interpreted as boolean `true`:
93
+ // "true", "t", "yes", "y", "1". The following case-insensitive strings
94
+ // are interpreted as boolean `false`: "false", "f", "no", "n", "0". If any
95
+ // errors are encountered, this function returns `false`, leaving `out` in an
96
+ // unspecified state.
97
+ ABSL_MUST_USE_RESULT bool SimpleAtob(absl::string_view str, bool* out);
98
+
99
+ ABSL_NAMESPACE_END
100
+ } // namespace absl
101
+
102
+ // End of public API. Implementation details follow.
103
+
104
+ namespace absl {
105
+ ABSL_NAMESPACE_BEGIN
106
+ namespace numbers_internal {
107
+
108
+ // Digit conversion.
109
+ extern const char kHexChar[17]; // 0123456789abcdef
110
+ extern const char kHexTable[513]; // 000102030405060708090a0b0c0d0e0f1011...
111
+ extern const char two_ASCII_digits[100][2]; // 00, 01, 02, 03...
112
+
113
+ // Writes a two-character representation of 'i' to 'buf'. 'i' must be in the
114
+ // range 0 <= i < 100, and buf must have space for two characters. Example:
115
+ // char buf[2];
116
+ // PutTwoDigits(42, buf);
117
+ // // buf[0] == '4'
118
+ // // buf[1] == '2'
119
+ inline void PutTwoDigits(size_t i, char* buf) {
120
+ assert(i < 100);
121
+ memcpy(buf, two_ASCII_digits[i], 2);
122
+ }
123
+
124
+ // safe_strto?() functions for implementing SimpleAtoi()
125
+ bool safe_strto32_base(absl::string_view text, int32_t* value, int base);
126
+ bool safe_strto64_base(absl::string_view text, int64_t* value, int base);
127
+ bool safe_strtou32_base(absl::string_view text, uint32_t* value, int base);
128
+ bool safe_strtou64_base(absl::string_view text, uint64_t* value, int base);
129
+ bool safe_strtou128_base(absl::string_view text, absl::uint128* value,
130
+ int base);
131
+
132
+ static const int kFastToBufferSize = 32;
133
+ static const int kSixDigitsToBufferSize = 16;
134
+
135
+ // Helper function for fast formatting of floating-point values.
136
+ // The result is the same as printf's "%g", a.k.a. "%.6g"; that is, six
137
+ // significant digits are returned, trailing zeros are removed, and numbers
138
+ // outside the range 0.0001-999999 are output using scientific notation
139
+ // (1.23456e+06). This routine is heavily optimized.
140
+ // Required buffer size is `kSixDigitsToBufferSize`.
141
+ size_t SixDigitsToBuffer(double d, char* buffer);
142
+
143
+ // These functions are intended for speed. All functions take an output buffer
144
+ // as an argument and return a pointer to the last byte they wrote, which is the
145
+ // terminating '\0'. At most `kFastToBufferSize` bytes are written.
146
+ char* FastIntToBuffer(int32_t, char*);
147
+ char* FastIntToBuffer(uint32_t, char*);
148
+ char* FastIntToBuffer(int64_t, char*);
149
+ char* FastIntToBuffer(uint64_t, char*);
150
+
151
+ // For enums and integer types that are not an exact match for the types above,
152
+ // use templates to call the appropriate one of the four overloads above.
153
+ template <typename int_type>
154
+ char* FastIntToBuffer(int_type i, char* buffer) {
155
+ static_assert(sizeof(i) <= 64 / 8,
156
+ "FastIntToBuffer works only with 64-bit-or-less integers.");
157
+ // TODO(jorg): This signed-ness check is used because it works correctly
158
+ // with enums, and it also serves to check that int_type is not a pointer.
159
+ // If one day something like std::is_signed<enum E> works, switch to it.
160
+ if (static_cast<int_type>(1) - 2 < 0) { // Signed
161
+ if (sizeof(i) > 32 / 8) { // 33-bit to 64-bit
162
+ return FastIntToBuffer(static_cast<int64_t>(i), buffer);
163
+ } else { // 32-bit or less
164
+ return FastIntToBuffer(static_cast<int32_t>(i), buffer);
165
+ }
166
+ } else { // Unsigned
167
+ if (sizeof(i) > 32 / 8) { // 33-bit to 64-bit
168
+ return FastIntToBuffer(static_cast<uint64_t>(i), buffer);
169
+ } else { // 32-bit or less
170
+ return FastIntToBuffer(static_cast<uint32_t>(i), buffer);
171
+ }
172
+ }
173
+ }
174
+
175
+ // Implementation of SimpleAtoi, generalized to support arbitrary base (used
176
+ // with base different from 10 elsewhere in Abseil implementation).
177
+ template <typename int_type>
178
+ ABSL_MUST_USE_RESULT bool safe_strtoi_base(absl::string_view s, int_type* out,
179
+ int base) {
180
+ static_assert(sizeof(*out) == 4 || sizeof(*out) == 8,
181
+ "SimpleAtoi works only with 32-bit or 64-bit integers.");
182
+ static_assert(!std::is_floating_point<int_type>::value,
183
+ "Use SimpleAtof or SimpleAtod instead.");
184
+ bool parsed;
185
+ // TODO(jorg): This signed-ness check is used because it works correctly
186
+ // with enums, and it also serves to check that int_type is not a pointer.
187
+ // If one day something like std::is_signed<enum E> works, switch to it.
188
+ if (static_cast<int_type>(1) - 2 < 0) { // Signed
189
+ if (sizeof(*out) == 64 / 8) { // 64-bit
190
+ int64_t val;
191
+ parsed = numbers_internal::safe_strto64_base(s, &val, base);
192
+ *out = static_cast<int_type>(val);
193
+ } else { // 32-bit
194
+ int32_t val;
195
+ parsed = numbers_internal::safe_strto32_base(s, &val, base);
196
+ *out = static_cast<int_type>(val);
197
+ }
198
+ } else { // Unsigned
199
+ if (sizeof(*out) == 64 / 8) { // 64-bit
200
+ uint64_t val;
201
+ parsed = numbers_internal::safe_strtou64_base(s, &val, base);
202
+ *out = static_cast<int_type>(val);
203
+ } else { // 32-bit
204
+ uint32_t val;
205
+ parsed = numbers_internal::safe_strtou32_base(s, &val, base);
206
+ *out = static_cast<int_type>(val);
207
+ }
208
+ }
209
+ return parsed;
210
+ }
211
+
212
+ // FastHexToBufferZeroPad16()
213
+ //
214
+ // Outputs `val` into `out` as if by `snprintf(out, 17, "%016x", val)` but
215
+ // without the terminating null character. Thus `out` must be of length >= 16.
216
+ // Returns the number of non-pad digits of the output (it can never be zero
217
+ // since 0 has one digit).
218
+ inline size_t FastHexToBufferZeroPad16(uint64_t val, char* out) {
219
+ #ifdef __SSE4_2__
220
+ uint64_t be = absl::big_endian::FromHost64(val);
221
+ const auto kNibbleMask = _mm_set1_epi8(0xf);
222
+ const auto kHexDigits = _mm_setr_epi8('0', '1', '2', '3', '4', '5', '6', '7',
223
+ '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
224
+ auto v = _mm_loadl_epi64(reinterpret_cast<__m128i*>(&be)); // load lo dword
225
+ auto v4 = _mm_srli_epi64(v, 4); // shift 4 right
226
+ auto il = _mm_unpacklo_epi8(v4, v); // interleave bytes
227
+ auto m = _mm_and_si128(il, kNibbleMask); // mask out nibbles
228
+ auto hexchars = _mm_shuffle_epi8(kHexDigits, m); // hex chars
229
+ _mm_storeu_si128(reinterpret_cast<__m128i*>(out), hexchars);
230
+ #else
231
+ for (int i = 0; i < 8; ++i) {
232
+ auto byte = (val >> (56 - 8 * i)) & 0xFF;
233
+ auto* hex = &absl::numbers_internal::kHexTable[byte * 2];
234
+ std::memcpy(out + 2 * i, hex, 2);
235
+ }
236
+ #endif
237
+ // | 0x1 so that even 0 has 1 digit.
238
+ return 16 - absl::base_internal::CountLeadingZeros64(val | 0x1) / 4;
239
+ }
240
+
241
+ } // namespace numbers_internal
242
+
243
+ // SimpleAtoi()
244
+ //
245
+ // Converts a string to an integer, using `safe_strto?()` functions for actual
246
+ // parsing, returning `true` if successful. The `safe_strto?()` functions apply
247
+ // strict checking; the string must be a base-10 integer, optionally followed or
248
+ // preceded by ASCII whitespace, with a value in the range of the corresponding
249
+ // integer type.
250
+ template <typename int_type>
251
+ ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str, int_type* out) {
252
+ return numbers_internal::safe_strtoi_base(str, out, 10);
253
+ }
254
+
255
+ ABSL_MUST_USE_RESULT inline bool SimpleAtoi(absl::string_view str,
256
+ absl::uint128* out) {
257
+ return numbers_internal::safe_strtou128_base(str, out, 10);
258
+ }
259
+
260
+ ABSL_NAMESPACE_END
261
+ } // namespace absl
262
+
263
+ #endif // ABSL_STRINGS_NUMBERS_H_
@@ -0,0 +1,246 @@
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/str_cat.h"
16
+
17
+ #include <assert.h>
18
+
19
+ #include <algorithm>
20
+ #include <cstdint>
21
+ #include <cstring>
22
+
23
+ #include "absl/strings/ascii.h"
24
+ #include "absl/strings/internal/resize_uninitialized.h"
25
+ #include "absl/strings/numbers.h"
26
+
27
+ namespace absl {
28
+ ABSL_NAMESPACE_BEGIN
29
+
30
+ AlphaNum::AlphaNum(Hex hex) {
31
+ static_assert(numbers_internal::kFastToBufferSize >= 32,
32
+ "This function only works when output buffer >= 32 bytes long");
33
+ char* const end = &digits_[numbers_internal::kFastToBufferSize];
34
+ auto real_width =
35
+ absl::numbers_internal::FastHexToBufferZeroPad16(hex.value, end - 16);
36
+ if (real_width >= hex.width) {
37
+ piece_ = absl::string_view(end - real_width, real_width);
38
+ } else {
39
+ // Pad first 16 chars because FastHexToBufferZeroPad16 pads only to 16 and
40
+ // max pad width can be up to 20.
41
+ std::memset(end - 32, hex.fill, 16);
42
+ // Patch up everything else up to the real_width.
43
+ std::memset(end - real_width - 16, hex.fill, 16);
44
+ piece_ = absl::string_view(end - hex.width, hex.width);
45
+ }
46
+ }
47
+
48
+ AlphaNum::AlphaNum(Dec dec) {
49
+ assert(dec.width <= numbers_internal::kFastToBufferSize);
50
+ char* const end = &digits_[numbers_internal::kFastToBufferSize];
51
+ char* const minfill = end - dec.width;
52
+ char* writer = end;
53
+ uint64_t value = dec.value;
54
+ bool neg = dec.neg;
55
+ while (value > 9) {
56
+ *--writer = '0' + (value % 10);
57
+ value /= 10;
58
+ }
59
+ *--writer = '0' + value;
60
+ if (neg) *--writer = '-';
61
+
62
+ ptrdiff_t fillers = writer - minfill;
63
+ if (fillers > 0) {
64
+ // Tricky: if the fill character is ' ', then it's <fill><+/-><digits>
65
+ // But...: if the fill character is '0', then it's <+/-><fill><digits>
66
+ bool add_sign_again = false;
67
+ if (neg && dec.fill == '0') { // If filling with '0',
68
+ ++writer; // ignore the sign we just added
69
+ add_sign_again = true; // and re-add the sign later.
70
+ }
71
+ writer -= fillers;
72
+ std::fill_n(writer, fillers, dec.fill);
73
+ if (add_sign_again) *--writer = '-';
74
+ }
75
+
76
+ piece_ = absl::string_view(writer, end - writer);
77
+ }
78
+
79
+ // ----------------------------------------------------------------------
80
+ // StrCat()
81
+ // This merges the given strings or integers, with no delimiter. This
82
+ // is designed to be the fastest possible way to construct a string out
83
+ // of a mix of raw C strings, string_views, strings, and integer values.
84
+ // ----------------------------------------------------------------------
85
+
86
+ // Append is merely a version of memcpy that returns the address of the byte
87
+ // after the area just overwritten.
88
+ static char* Append(char* out, const AlphaNum& x) {
89
+ // memcpy is allowed to overwrite arbitrary memory, so doing this after the
90
+ // call would force an extra fetch of x.size().
91
+ char* after = out + x.size();
92
+ if (x.size() != 0) {
93
+ memcpy(out, x.data(), x.size());
94
+ }
95
+ return after;
96
+ }
97
+
98
+ std::string StrCat(const AlphaNum& a, const AlphaNum& b) {
99
+ std::string result;
100
+ absl::strings_internal::STLStringResizeUninitialized(&result,
101
+ a.size() + b.size());
102
+ char* const begin = &result[0];
103
+ char* out = begin;
104
+ out = Append(out, a);
105
+ out = Append(out, b);
106
+ assert(out == begin + result.size());
107
+ return result;
108
+ }
109
+
110
+ std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c) {
111
+ std::string result;
112
+ strings_internal::STLStringResizeUninitialized(
113
+ &result, a.size() + b.size() + c.size());
114
+ char* const begin = &result[0];
115
+ char* out = begin;
116
+ out = Append(out, a);
117
+ out = Append(out, b);
118
+ out = Append(out, c);
119
+ assert(out == begin + result.size());
120
+ return result;
121
+ }
122
+
123
+ std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c,
124
+ const AlphaNum& d) {
125
+ std::string result;
126
+ strings_internal::STLStringResizeUninitialized(
127
+ &result, a.size() + b.size() + c.size() + d.size());
128
+ char* const begin = &result[0];
129
+ char* out = begin;
130
+ out = Append(out, a);
131
+ out = Append(out, b);
132
+ out = Append(out, c);
133
+ out = Append(out, d);
134
+ assert(out == begin + result.size());
135
+ return result;
136
+ }
137
+
138
+ namespace strings_internal {
139
+
140
+ // Do not call directly - these are not part of the public API.
141
+ std::string CatPieces(std::initializer_list<absl::string_view> pieces) {
142
+ std::string result;
143
+ size_t total_size = 0;
144
+ for (const absl::string_view piece : pieces) total_size += piece.size();
145
+ strings_internal::STLStringResizeUninitialized(&result, total_size);
146
+
147
+ char* const begin = &result[0];
148
+ char* out = begin;
149
+ for (const absl::string_view piece : pieces) {
150
+ const size_t this_size = piece.size();
151
+ if (this_size != 0) {
152
+ memcpy(out, piece.data(), this_size);
153
+ out += this_size;
154
+ }
155
+ }
156
+ assert(out == begin + result.size());
157
+ return result;
158
+ }
159
+
160
+ // It's possible to call StrAppend with an absl::string_view that is itself a
161
+ // fragment of the string we're appending to. However the results of this are
162
+ // random. Therefore, check for this in debug mode. Use unsigned math so we
163
+ // only have to do one comparison. Note, there's an exception case: appending an
164
+ // empty string is always allowed.
165
+ #define ASSERT_NO_OVERLAP(dest, src) \
166
+ assert(((src).size() == 0) || \
167
+ (uintptr_t((src).data() - (dest).data()) > uintptr_t((dest).size())))
168
+
169
+ void AppendPieces(std::string* dest,
170
+ std::initializer_list<absl::string_view> pieces) {
171
+ size_t old_size = dest->size();
172
+ size_t total_size = old_size;
173
+ for (const absl::string_view piece : pieces) {
174
+ ASSERT_NO_OVERLAP(*dest, piece);
175
+ total_size += piece.size();
176
+ }
177
+ strings_internal::STLStringResizeUninitialized(dest, total_size);
178
+
179
+ char* const begin = &(*dest)[0];
180
+ char* out = begin + old_size;
181
+ for (const absl::string_view piece : pieces) {
182
+ const size_t this_size = piece.size();
183
+ if (this_size != 0) {
184
+ memcpy(out, piece.data(), this_size);
185
+ out += this_size;
186
+ }
187
+ }
188
+ assert(out == begin + dest->size());
189
+ }
190
+
191
+ } // namespace strings_internal
192
+
193
+ void StrAppend(std::string* dest, const AlphaNum& a) {
194
+ ASSERT_NO_OVERLAP(*dest, a);
195
+ dest->append(a.data(), a.size());
196
+ }
197
+
198
+ void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b) {
199
+ ASSERT_NO_OVERLAP(*dest, a);
200
+ ASSERT_NO_OVERLAP(*dest, b);
201
+ std::string::size_type old_size = dest->size();
202
+ strings_internal::STLStringResizeUninitialized(
203
+ dest, old_size + a.size() + b.size());
204
+ char* const begin = &(*dest)[0];
205
+ char* out = begin + old_size;
206
+ out = Append(out, a);
207
+ out = Append(out, b);
208
+ assert(out == begin + dest->size());
209
+ }
210
+
211
+ void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b,
212
+ const AlphaNum& c) {
213
+ ASSERT_NO_OVERLAP(*dest, a);
214
+ ASSERT_NO_OVERLAP(*dest, b);
215
+ ASSERT_NO_OVERLAP(*dest, c);
216
+ std::string::size_type old_size = dest->size();
217
+ strings_internal::STLStringResizeUninitialized(
218
+ dest, old_size + a.size() + b.size() + c.size());
219
+ char* const begin = &(*dest)[0];
220
+ char* out = begin + old_size;
221
+ out = Append(out, a);
222
+ out = Append(out, b);
223
+ out = Append(out, c);
224
+ assert(out == begin + dest->size());
225
+ }
226
+
227
+ void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b,
228
+ const AlphaNum& c, const AlphaNum& d) {
229
+ ASSERT_NO_OVERLAP(*dest, a);
230
+ ASSERT_NO_OVERLAP(*dest, b);
231
+ ASSERT_NO_OVERLAP(*dest, c);
232
+ ASSERT_NO_OVERLAP(*dest, d);
233
+ std::string::size_type old_size = dest->size();
234
+ strings_internal::STLStringResizeUninitialized(
235
+ dest, old_size + a.size() + b.size() + c.size() + d.size());
236
+ char* const begin = &(*dest)[0];
237
+ char* out = begin + old_size;
238
+ out = Append(out, a);
239
+ out = Append(out, b);
240
+ out = Append(out, c);
241
+ out = Append(out, d);
242
+ assert(out == begin + dest->size());
243
+ }
244
+
245
+ ABSL_NAMESPACE_END
246
+ } // namespace absl