@@ -20,6 +20,7 @@ limitations under the License.
2020#include < cstddef>
2121#include < cstdint>
2222#include < string>
23+ #include < type_traits>
2324
2425#include " absl/base/macros.h"
2526#include " absl/strings/numbers.h"
@@ -30,21 +31,7 @@ limitations under the License.
3031namespace tsl {
3132namespace strings {
3233
33- // ----------------------------------------------------------------------
34- // FastIntToBufferLeft()
35- // These are intended for speed.
36- //
37- // All functions take the output buffer as an arg. FastInt() uses
38- // at most 22 bytes, FastTime() uses exactly 30 bytes. They all
39- // return a pointer to the beginning of the output, which is the same as
40- // the beginning of the input buffer.
41- //
42- // NOTE: In 64-bit land, sizeof(time_t) is 8, so it is possible
43- // to pass to FastTimeToBuffer() a time whose year cannot be
44- // represented in 4 digits. In this case, the output buffer
45- // will contain the string "Invalid:<value>"
46- // ----------------------------------------------------------------------
47-
34+ namespace strings_internal {
4835// Previously documented minimums -- the buffers provided must be at least this
4936// long, though these numbers are subject to change:
5037// Int32, UInt32: 12 bytes
@@ -53,33 +40,6 @@ namespace strings {
5340// Use kFastToBufferSize rather than hardcoding constants.
5441inline constexpr int kFastToBufferSize = 32 ;
5542
56- // ----------------------------------------------------------------------
57- // FastInt32ToBufferLeft()
58- // FastUInt32ToBufferLeft()
59- // FastInt64ToBufferLeft()
60- // FastUInt64ToBufferLeft()
61- //
62- // These functions convert their numeric argument to an ASCII
63- // representation of the numeric value in base 10, with the
64- // representation being left-aligned in the buffer. The caller is
65- // responsible for ensuring that the buffer has enough space to hold
66- // the output. The buffer should typically be at least kFastToBufferSize
67- // bytes.
68- //
69- // Returns the number of characters written.
70- // ----------------------------------------------------------------------
71-
72- size_t FastInt32ToBufferLeft (int32_t i, char * buffer); // at least 12 bytes
73- size_t FastUInt32ToBufferLeft (uint32_t i, char * buffer); // at least 12 bytes
74- size_t FastInt64ToBufferLeft (int64_t i, char * buffer); // at least 22 bytes
75- size_t FastUInt64ToBufferLeft (uint64_t i, char * buffer); // at least 22 bytes
76-
77- // Required buffer size for DoubleToBuffer is kFastToBufferSize.
78- // Required buffer size for FloatToBuffer is kFastToBufferSize.
79- size_t DoubleToBuffer (double value, char * buffer);
80- size_t FloatToBuffer (float value, char * buffer);
81-
82- namespace strings_internal {
8343// AlphaNumBuffer allows a way to pass a string to absl::StrCat without having
8444// to do memory allocation. It is simply a pair of a fixed-size character
8545// array, and a size. Please don't use outside of the "strings" package.
@@ -173,36 +133,19 @@ inline bool safe_strtod(absl::string_view str, double* value) {
173133 return absl::SimpleAtod (str, value);
174134}
175135
176- inline bool ProtoParseNumeric (absl::string_view s, int32_t * value) {
177- return absl::SimpleAtoi (s, value);
178- }
179-
180- inline bool ProtoParseNumeric (absl::string_view s, uint32_t * value) {
181- return absl::SimpleAtoi (s, value);
182- }
183-
184- inline bool ProtoParseNumeric (absl::string_view s, int64_t * value) {
185- return absl::SimpleAtoi (s, value);
186- }
187-
188- inline bool ProtoParseNumeric (absl::string_view s, uint64_t * value) {
189- return absl::SimpleAtoi (s, value);
190- }
191-
192- inline bool ProtoParseNumeric (absl::string_view s, float * value) {
193- return absl::SimpleAtof (s, value);
194- }
195-
196- inline bool ProtoParseNumeric (absl::string_view s, double * value) {
197- return absl::SimpleAtod (s, value);
198- }
199-
200136// Convert strings to number of type T.
201137// Leading and trailing spaces are allowed.
202138// Values may be rounded on over- and underflow.
203139template <typename T>
204140bool SafeStringToNumeric (absl::string_view s, T* value) {
205- return ProtoParseNumeric (s, value);
141+ if constexpr (std::is_integral_v<T>) {
142+ return absl::SimpleAtoi (s, value);
143+ } else if constexpr (std::is_same_v<T, float >) {
144+ return absl::SimpleAtof (s, value);
145+ } else {
146+ static_assert (std::is_same_v<T, double >);
147+ return absl::SimpleAtod (s, value);
148+ }
206149}
207150
208151// Converts from an int64 to a human readable string representing the
0 commit comments