Skip to content

Commit 87f609e

Browse files
majnemercopybara-github
authored andcommitted
Remove ProtoParseNumeric and inline its logic into SafeStringToNumeric.
The `ProtoParseNumeric` helper functions are removed. The `SafeStringToNumeric` template now uses `if constexpr` to directly call the appropriate `absl::SimpleAto*` function based on the numeric type. PiperOrigin-RevId: 828260928
1 parent c1ae6db commit 87f609e

File tree

3 files changed

+13
-67
lines changed

3 files changed

+13
-67
lines changed

tsl/platform/numbers.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ constexpr int NumDecimalDigits(int n) {
117117

118118
template <typename T>
119119
size_t FpToBuffer(T value, char* buffer) {
120+
using strings_internal::kFastToBufferSize;
120121
// Out of an abundance of caution, we ensure that the buffer is large enough
121122
// to hold the worst-case formatting of any floating-point number.
122123
constexpr size_t kMaxExponentDigits10 =

tsl/platform/numbers.h

Lines changed: 10 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3031
namespace tsl {
3132
namespace 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.
5441
inline 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.
203139
template <typename T>
204140
bool 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

tsl/platform/numbers_test.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ limitations under the License.
2727
namespace tsl {
2828
namespace strings {
2929

30+
using strings_internal::kFastToBufferSize;
31+
3032
// NOTE: most of the routines in numbers.h are tested indirectly through
3133
// strcat_test.cc in this directory.
3234

0 commit comments

Comments
 (0)