Skip to content

Commit 83c149d

Browse files
majnemercopybara-github
authored andcommitted
Migrate some TSL code over to ABSL equivalents
No functional change is intended. PiperOrigin-RevId: 706010982
1 parent d58ce1b commit 83c149d

File tree

4 files changed

+59
-230
lines changed

4 files changed

+59
-230
lines changed

tsl/platform/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ cc_library(
229229
":stringpiece",
230230
":stringprintf",
231231
":types",
232+
"@com_google_absl//absl/base:core_headers",
233+
"@com_google_absl//absl/strings",
232234
"@double_conversion//:double-conversion",
233235
],
234236
)
@@ -1720,6 +1722,8 @@ tsl_cc_test(
17201722
":numbers",
17211723
":test",
17221724
":test_main",
1725+
":types",
1726+
"@com_google_absl//absl/strings",
17231727
],
17241728
)
17251729

tsl/platform/numbers.cc

Lines changed: 14 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ limitations under the License.
2020
#include <stdlib.h>
2121

2222
#include <algorithm>
23-
#include <cinttypes>
23+
#include <charconv>
2424
#include <cmath>
2525
#include <cstdint>
2626
#include <locale>
27+
#include <string>
28+
#include <system_error> // NOLINT
2729
#include <unordered_map>
2830

29-
#include "double-conversion/double-conversion.h"
30-
#include "tsl/platform/str_util.h"
31+
#include "absl/strings/str_cat.h"
32+
#include "absl/strings/string_view.h"
3133
#include "tsl/platform/logging.h"
3234
#include "tsl/platform/macros.h"
3335
#include "tsl/platform/stringprintf.h"
@@ -114,17 +116,6 @@ T locale_independent_strtonum(const char* str, const char** endptr) {
114116
return result;
115117
}
116118

117-
static inline const double_conversion::StringToDoubleConverter&
118-
StringToFloatConverter() {
119-
static const double_conversion::StringToDoubleConverter converter(
120-
double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES |
121-
double_conversion::StringToDoubleConverter::ALLOW_HEX |
122-
double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES |
123-
double_conversion::StringToDoubleConverter::ALLOW_CASE_INSENSIBILITY,
124-
0., 0., "inf", "nan");
125-
return converter;
126-
}
127-
128119
} // namespace
129120

130121
namespace strings {
@@ -219,154 +210,6 @@ size_t DoubleToBuffer(double value, char* buffer) {
219210
return snprintf_result;
220211
}
221212

222-
namespace {
223-
char SafeFirstChar(absl::string_view str) {
224-
if (str.empty()) return '\0';
225-
return str[0];
226-
}
227-
void SkipSpaces(absl::string_view* str) {
228-
while (isspace(SafeFirstChar(*str))) str->remove_prefix(1);
229-
}
230-
} // namespace
231-
232-
bool safe_strto64(absl::string_view str, int64_t* value) {
233-
SkipSpaces(&str);
234-
235-
int64_t vlimit = kint64max;
236-
int sign = 1;
237-
if (absl::ConsumePrefix(&str, "-")) {
238-
sign = -1;
239-
// Different limit for positive and negative integers.
240-
vlimit = kint64min;
241-
}
242-
243-
if (!isdigit(SafeFirstChar(str))) return false;
244-
245-
int64_t result = 0;
246-
if (sign == 1) {
247-
do {
248-
int digit = SafeFirstChar(str) - '0';
249-
if ((vlimit - digit) / 10 < result) {
250-
return false;
251-
}
252-
result = result * 10 + digit;
253-
str.remove_prefix(1);
254-
} while (isdigit(SafeFirstChar(str)));
255-
} else {
256-
do {
257-
int digit = SafeFirstChar(str) - '0';
258-
if ((vlimit + digit) / 10 > result) {
259-
return false;
260-
}
261-
result = result * 10 - digit;
262-
str.remove_prefix(1);
263-
} while (isdigit(SafeFirstChar(str)));
264-
}
265-
266-
SkipSpaces(&str);
267-
if (!str.empty()) return false;
268-
269-
*value = result;
270-
return true;
271-
}
272-
273-
bool safe_strtou64(absl::string_view str, uint64_t* value) {
274-
SkipSpaces(&str);
275-
if (!isdigit(SafeFirstChar(str))) return false;
276-
277-
uint64_t result = 0;
278-
do {
279-
int digit = SafeFirstChar(str) - '0';
280-
if ((kuint64max - digit) / 10 < result) {
281-
return false;
282-
}
283-
result = result * 10 + digit;
284-
str.remove_prefix(1);
285-
} while (isdigit(SafeFirstChar(str)));
286-
287-
SkipSpaces(&str);
288-
if (!str.empty()) return false;
289-
290-
*value = result;
291-
return true;
292-
}
293-
294-
bool safe_strto32(absl::string_view str, int32_t* value) {
295-
SkipSpaces(&str);
296-
297-
int64_t vmax = kint32max;
298-
int sign = 1;
299-
if (absl::ConsumePrefix(&str, "-")) {
300-
sign = -1;
301-
// Different max for positive and negative integers.
302-
++vmax;
303-
}
304-
305-
if (!isdigit(SafeFirstChar(str))) return false;
306-
307-
int64_t result = 0;
308-
do {
309-
result = result * 10 + SafeFirstChar(str) - '0';
310-
if (result > vmax) {
311-
return false;
312-
}
313-
str.remove_prefix(1);
314-
} while (isdigit(SafeFirstChar(str)));
315-
316-
SkipSpaces(&str);
317-
318-
if (!str.empty()) return false;
319-
320-
*value = static_cast<int32_t>(result * sign);
321-
return true;
322-
}
323-
324-
bool safe_strtou32(absl::string_view str, uint32_t* value) {
325-
SkipSpaces(&str);
326-
if (!isdigit(SafeFirstChar(str))) return false;
327-
328-
int64_t result = 0;
329-
do {
330-
result = result * 10 + SafeFirstChar(str) - '0';
331-
if (result > kuint32max) {
332-
return false;
333-
}
334-
str.remove_prefix(1);
335-
} while (isdigit(SafeFirstChar(str)));
336-
337-
SkipSpaces(&str);
338-
if (!str.empty()) return false;
339-
340-
*value = static_cast<uint32_t>(result);
341-
return true;
342-
}
343-
344-
bool safe_strtof(absl::string_view str, float* value) {
345-
int processed_characters_count = -1;
346-
auto len = str.size();
347-
348-
// If string length exceeds buffer size or int max, fail.
349-
if (len >= kFastToBufferSize) return false;
350-
if (len > std::numeric_limits<int>::max()) return false;
351-
352-
*value = StringToFloatConverter().StringToFloat(
353-
str.data(), static_cast<int>(len), &processed_characters_count);
354-
return processed_characters_count > 0;
355-
}
356-
357-
bool safe_strtod(absl::string_view str, double* value) {
358-
int processed_characters_count = -1;
359-
auto len = str.size();
360-
361-
// If string length exceeds buffer size or int max, fail.
362-
if (len >= kFastToBufferSize) return false;
363-
if (len > std::numeric_limits<int>::max()) return false;
364-
365-
*value = StringToFloatConverter().StringToDouble(
366-
str.data(), static_cast<int>(len), &processed_characters_count);
367-
return processed_characters_count > 0;
368-
}
369-
370213
size_t FloatToBuffer(float value, char* buffer) {
371214
// FLT_DIG is 6 for IEEE-754 floats, which are used on almost all
372215
// platforms these days. Just in case some system exists where FLT_DIG
@@ -401,51 +244,21 @@ size_t FloatToBuffer(float value, char* buffer) {
401244
}
402245

403246
std::string FpToString(Fprint fp) {
404-
char buf[17];
405-
snprintf(buf, sizeof(buf), "%016llx", static_cast<long long>(fp));
406-
return std::string(buf);
247+
return absl::StrCat(absl::Hex(fp, absl::kZeroPad16));
407248
}
408249

409-
bool StringToFp(const std::string& s, Fprint* fp) {
410-
char junk;
411-
uint64_t result;
412-
if (sscanf(s.c_str(), "%" SCNx64 "%c", &result, &junk) == 1) {
413-
*fp = result;
414-
return true;
415-
} else {
250+
bool HexStringToUint64(absl::string_view s, uint64_t* result) {
251+
auto end_ptr = s.data() + s.size();
252+
uint64_t parsed_result;
253+
auto [ptr, ec] =
254+
std::from_chars(s.data(), end_ptr, parsed_result, /*base=*/16);
255+
if (ec != std::errc{}) {
416256
return false;
417257
}
418-
}
419-
420-
absl::string_view Uint64ToHexString(uint64_t v, char* buf) {
421-
static const char* hexdigits = "0123456789abcdef";
422-
const int num_byte = 16;
423-
buf[num_byte] = '\0';
424-
for (int i = num_byte - 1; i >= 0; i--) {
425-
buf[i] = hexdigits[v & 0xf];
426-
v >>= 4;
427-
}
428-
return absl::string_view(buf, num_byte);
429-
}
430-
431-
bool HexStringToUint64(const absl::string_view& s, uint64_t* result) {
432-
uint64_t v = 0;
433-
if (s.empty()) {
258+
if (ptr != end_ptr) {
434259
return false;
435260
}
436-
for (size_t i = 0; i < s.size(); i++) {
437-
char c = s[i];
438-
if (c >= '0' && c <= '9') {
439-
v = (v << 4) + (c - '0');
440-
} else if (c >= 'a' && c <= 'f') {
441-
v = (v << 4) + 10 + (c - 'a');
442-
} else if (c >= 'A' && c <= 'F') {
443-
v = (v << 4) + 10 + (c - 'A');
444-
} else {
445-
return false;
446-
}
447-
}
448-
*result = v;
261+
*result = parsed_result;
449262
return true;
450263
}
451264

tsl/platform/numbers.h

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ limitations under the License.
1616
#ifndef TENSORFLOW_TSL_PLATFORM_NUMBERS_H_
1717
#define TENSORFLOW_TSL_PLATFORM_NUMBERS_H_
1818

19+
#include <cstddef>
1920
#include <cstdint>
2021
#include <string>
2122

23+
#include "absl/base/macros.h"
24+
#include "absl/strings/numbers.h"
2225
#include "tsl/platform/stringpiece.h"
2326
#include "tsl/platform/types.h"
2427

@@ -46,7 +49,7 @@ namespace strings {
4649
// Int64, UInt64, Int, Uint: 22 bytes
4750
// Time: 30 bytes
4851
// Use kFastToBufferSize rather than hardcoding constants.
49-
static const int kFastToBufferSize = 32;
52+
inline constexpr int kFastToBufferSize = 32;
5053

5154
// ----------------------------------------------------------------------
5255
// FastInt32ToBufferLeft()
@@ -77,52 +80,60 @@ size_t FloatToBuffer(float value, char* buffer);
7780
// Convert a 64-bit fingerprint value to an ASCII representation.
7881
std::string FpToString(Fprint fp);
7982

80-
// Attempt to parse a fingerprint in the form encoded by FpToString. If
81-
// successful, stores the fingerprint in *fp and returns true. Otherwise,
82-
// returns false.
83-
bool StringToFp(const std::string& s, Fprint* fp);
84-
85-
// Convert a 64-bit fingerprint value to an ASCII representation that
86-
// is terminated by a '\0'.
87-
// Buf must point to an array of at least kFastToBufferSize characters
88-
absl::string_view Uint64ToHexString(uint64_t v, char* buf);
89-
90-
// Attempt to parse a uint64 in the form encoded by FastUint64ToHexString. If
91-
// successful, stores the value in *v and returns true. Otherwise,
92-
// returns false.
93-
bool HexStringToUint64(const absl::string_view& s, uint64_t* result);
83+
// Attempt to parse a `uint64_t` in the form encoded by
84+
// `absl::StrCat(absl::Hex(*result))`. If successful, stores the value in
85+
// `result` and returns true. Otherwise, returns false.
86+
bool HexStringToUint64(absl::string_view s, uint64_t* result);
9487

9588
// Convert strings to 32bit integer values.
9689
// Leading and trailing spaces are allowed.
9790
// Return false with overflow or invalid input.
98-
bool safe_strto32(absl::string_view str, int32_t* value);
91+
ABSL_DEPRECATE_AND_INLINE()
92+
inline bool safe_strto32(absl::string_view str, int32_t* value) {
93+
return absl::SimpleAtoi(str, value);
94+
}
9995

10096
// Convert strings to unsigned 32bit integer values.
10197
// Leading and trailing spaces are allowed.
10298
// Return false with overflow or invalid input.
103-
bool safe_strtou32(absl::string_view str, uint32_t* value);
99+
ABSL_DEPRECATE_AND_INLINE()
100+
inline bool safe_strtou32(absl::string_view str, uint32_t* value) {
101+
return absl::SimpleAtoi(str, value);
102+
}
104103

105104
// Convert strings to 64bit integer values.
106105
// Leading and trailing spaces are allowed.
107106
// Return false with overflow or invalid input.
108-
bool safe_strto64(absl::string_view str, int64_t* value);
107+
ABSL_DEPRECATE_AND_INLINE()
108+
inline bool safe_strto64(absl::string_view str, int64_t* value) {
109+
return absl::SimpleAtoi(str, value);
110+
}
109111

110112
// Convert strings to unsigned 64bit integer values.
111113
// Leading and trailing spaces are allowed.
112114
// Return false with overflow or invalid input.
113-
bool safe_strtou64(absl::string_view str, uint64_t* value);
115+
ABSL_DEPRECATE_AND_INLINE()
116+
inline bool safe_strtou64(absl::string_view str, uint64_t* value) {
117+
return absl::SimpleAtoi(str, value);
118+
}
114119

115120
// Convert strings to floating point values.
116121
// Leading and trailing spaces are allowed.
117122
// Values may be rounded on over- and underflow.
118123
// Returns false on invalid input or if `strlen(value) >= kFastToBufferSize`.
119-
bool safe_strtof(absl::string_view str, float* value);
124+
ABSL_DEPRECATE_AND_INLINE()
125+
inline bool safe_strtof(absl::string_view str, float* value) {
126+
return absl::SimpleAtof(str, value);
127+
}
120128

121129
// Convert strings to double precision floating point values.
122130
// Leading and trailing spaces are allowed.
123131
// Values may be rounded on over- and underflow.
124132
// Returns false on invalid input or if `strlen(value) >= kFastToBufferSize`.
125-
bool safe_strtod(absl::string_view str, double* value);
133+
ABSL_DEPRECATE_AND_INLINE()
134+
inline bool safe_strtod(absl::string_view str, double* value) {
135+
return absl::SimpleAtod(str, value);
136+
}
126137

127138
inline bool ProtoParseNumeric(absl::string_view s, int32_t* value) {
128139
return safe_strto32(s, value);

0 commit comments

Comments
 (0)