@@ -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
130121namespace 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-
370213size_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
403246std::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
0 commit comments