Skip to content

Commit

Permalink
[base] Rewrote to_{u}int32 by using internal::ToInteger.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpimenov authored and tatiana-yan committed Feb 16, 2021
1 parent d62567c commit a644112
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
28 changes: 0 additions & 28 deletions base/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,6 @@ UniChar LastUniChar(std::string const & s)
return *iter;
}

bool to_uint32(char const * start, uint32_t & i, int base)
{
uint64_t num = 0;
if (!to_uint64(start, num, base))
return false;

if (num > static_cast<uint64_t>(std::numeric_limits<uint32_t>::max()))
return false;

i = static_cast<uint32_t>(num);
return true;
}

bool to_int32(char const * start, int32_t & i)
{
int64_t num = 0;
if (!to_int64(start, num))
return false;

if (num > static_cast<int64_t>(std::numeric_limits<int32_t>::max()))
return false;
if (num < static_cast<int64_t>(std::numeric_limits<int32_t>::min()))
return false;

i = static_cast<int32_t>(num);
return true;
}

bool to_size_t(char const * start, size_t & i, int base)
{
uint64_t num = 0;
Expand Down
19 changes: 16 additions & 3 deletions base/string_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,22 @@ WARN_UNUSED_RESULT inline bool to_int64(char const * s, int64_t & i)
return internal::ToInteger(s, i);
}

// Unlike the 64-bit version, to_uint32 will not convert negative values.
WARN_UNUSED_RESULT bool to_uint32(char const * s, uint32_t & i, int base = 10);
WARN_UNUSED_RESULT bool to_int32(char const * s, int32_t & i);
// Unlike the 64-bit version, to_uint32 is not guaranteed to convert negative values.
// Current implementation conflates fixed-width types (uint32, uint64) with types that have no
// guarantees on their exact sizes (unsigned long, unsigned long long) so results of internal
// conversions may differ between platforms.
// Converting strings representing negative numbers to unsigned integers looks like a bad
// idea anyway and it's not worth changing the implementation solely for this reason.
WARN_UNUSED_RESULT inline bool to_uint32(char const * s, uint32_t & i, int base = 10)
{
return internal::ToInteger(s, i, base);
}

WARN_UNUSED_RESULT inline bool to_int32(char const * s, int32_t & i)
{
return internal::ToInteger(s, i);
}

WARN_UNUSED_RESULT bool to_size_t(char const * s, size_t & i, int base = 10);
WARN_UNUSED_RESULT bool to_float(char const * s, float & f);
WARN_UNUSED_RESULT bool to_double(char const * s, double & d);
Expand Down

0 comments on commit a644112

Please sign in to comment.