Speed up strings::to_<integer> conversions
This commit is contained in:
parent
7adbf8bb7b
commit
df220cf7cd
2 changed files with 111 additions and 17 deletions
|
@ -152,6 +152,97 @@ UNIT_TEST(to_double)
|
|||
s = "-2";
|
||||
TEST(strings::to_double(s, d), ());
|
||||
TEST_ALMOST_EQUAL(-2.0, d, ());
|
||||
|
||||
s = "labuda";
|
||||
TEST(!strings::to_double(s, d), ());
|
||||
}
|
||||
|
||||
UNIT_TEST(to_float)
|
||||
{
|
||||
string s;
|
||||
float f;
|
||||
|
||||
s = "0.123";
|
||||
TEST(strings::to_float(s, f), ());
|
||||
TEST_ALMOST_EQUAL(0.123f, f, ());
|
||||
|
||||
s = "1.";
|
||||
TEST(strings::to_float(s, f), ());
|
||||
TEST_ALMOST_EQUAL(1.0f, f, ());
|
||||
|
||||
s = "0";
|
||||
TEST(strings::to_float(s, f), ());
|
||||
TEST_ALMOST_EQUAL(0.f, f, ());
|
||||
|
||||
s = "5.68434e-14";
|
||||
TEST(strings::to_float(s, f), ());
|
||||
TEST_ALMOST_EQUAL(5.68434e-14f, f, ());
|
||||
|
||||
s = "-179.654321";
|
||||
TEST(strings::to_float(s, f), ());
|
||||
TEST_ALMOST_EQUAL(-179.654321f, f, ());
|
||||
|
||||
s = "labuda";
|
||||
TEST(!strings::to_float(s, f), ());
|
||||
}
|
||||
|
||||
UNIT_TEST(to_int)
|
||||
{
|
||||
int i;
|
||||
string s;
|
||||
|
||||
s = "-2";
|
||||
TEST(strings::to_int(s, i), ());
|
||||
TEST_EQUAL(-2, i, ());
|
||||
|
||||
s = "0";
|
||||
TEST(strings::to_int(s, i), ());
|
||||
TEST_EQUAL(0, i, ());
|
||||
|
||||
s = "123456789";
|
||||
TEST(strings::to_int(s, i), ());
|
||||
TEST_EQUAL(123456789, i, ());
|
||||
|
||||
s = "labuda";
|
||||
TEST(!strings::to_int(s, i), ());
|
||||
}
|
||||
|
||||
UNIT_TEST(to_uint64)
|
||||
{
|
||||
uint64_t i;
|
||||
string s;
|
||||
|
||||
s = "0";
|
||||
TEST(strings::to_uint64(s, i), ());
|
||||
TEST_EQUAL(0, i, ());
|
||||
|
||||
s = "123456789101112";
|
||||
TEST(strings::to_uint64(s, i), ());
|
||||
TEST_EQUAL(123456789101112UL, i, ());
|
||||
|
||||
s = "labuda";
|
||||
TEST(!strings::to_uint64(s, i), ());
|
||||
}
|
||||
|
||||
UNIT_TEST(to_int64)
|
||||
{
|
||||
int64_t i;
|
||||
string s;
|
||||
|
||||
s = "-24567";
|
||||
TEST(strings::to_int64(s, i), ());
|
||||
TEST_EQUAL(-24567, i, ());
|
||||
|
||||
s = "0";
|
||||
TEST(strings::to_int64(s, i), ());
|
||||
TEST_EQUAL(0, i, ());
|
||||
|
||||
s = "12345678911212";
|
||||
TEST(strings::to_int64(s, i), ());
|
||||
TEST_EQUAL(12345678911212L, i, ());
|
||||
|
||||
s = "labuda";
|
||||
TEST(!strings::to_int64(s, i), ());
|
||||
}
|
||||
|
||||
UNIT_TEST(to_string)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "string_utils.hpp"
|
||||
#include "assert.hpp"
|
||||
|
||||
#include "../std/target_os.hpp"
|
||||
#include "../std/iterator.hpp"
|
||||
|
||||
namespace strings
|
||||
|
@ -34,10 +35,11 @@ UniChar LastUniChar(string const & s)
|
|||
bool to_int(char const * s, int & i)
|
||||
{
|
||||
char * stop;
|
||||
int x = strtol(s, &stop, 10);
|
||||
long const x = strtol(s, &stop, 10);
|
||||
if (stop && *stop == 0)
|
||||
{
|
||||
i = x;
|
||||
i = static_cast<int>(x);
|
||||
ASSERT_EQUAL(static_cast<long>(i), x, ());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -45,30 +47,31 @@ bool to_int(char const * s, int & i)
|
|||
|
||||
bool to_uint64(char const * s, uint64_t & i)
|
||||
{
|
||||
istringstream ss;
|
||||
ss.str(s);
|
||||
ss >> i;
|
||||
return !ss.fail();
|
||||
char * stop;
|
||||
#ifdef OMIM_OS_WINDOWS
|
||||
i = _strtoui64(s, &stop, 10);
|
||||
#else
|
||||
i = strtoull(s, &stop, 10);
|
||||
#endif
|
||||
return stop && *stop == 0;
|
||||
}
|
||||
|
||||
bool to_int64(char const * s, int64_t & i)
|
||||
{
|
||||
istringstream ss;
|
||||
ss.str(s);
|
||||
ss >> i;
|
||||
return !ss.fail();
|
||||
char * stop;
|
||||
#ifdef OMIM_OS_WINDOWS
|
||||
i = _strtoi64(s, &stop, 10);
|
||||
#else
|
||||
i = strtoll(s, &stop, 10);
|
||||
#endif
|
||||
return stop && *stop == 0;
|
||||
}
|
||||
|
||||
bool to_double(char const * s, double & d)
|
||||
{
|
||||
char * stop;
|
||||
double x = strtod(s, &stop);
|
||||
if (stop && *stop == 0)
|
||||
{
|
||||
d = x;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
d = strtod(s, &stop);
|
||||
return stop && *stop == 0;
|
||||
}
|
||||
|
||||
UniString MakeLowerCase(UniString const & s)
|
||||
|
|
Reference in a new issue