Improve interface for string operations

Trim incoming string before converting to number
Remove use of std::strtod and std::stoi
Use std::string_view instead of std::string when possible
This commit is contained in:
Magne Sjaastad
2024-07-25 20:11:49 +02:00
parent 33ffa10ec9
commit 6fb74654a1
9 changed files with 108 additions and 46 deletions

View File

@@ -257,3 +257,75 @@ TEST( RiaStdStringToolsTest, TestInvalidRangeStrings )
ASSERT_EQ( expectedValues, actualValues );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RiaStdStringToolsTest, TestToDouble )
{
{
std::string text = " 0.123";
double resultValue = 0.0;
auto isOk = RiaStdStringTools::toDouble( text, resultValue );
EXPECT_TRUE( isOk );
EXPECT_DOUBLE_EQ( resultValue, 0.123 );
}
{
std::string text = " 0.123 2";
double resultValue = 0.0;
auto isOk = RiaStdStringTools::toDouble( text, resultValue );
EXPECT_TRUE( isOk );
EXPECT_DOUBLE_EQ( resultValue, 0.123 );
}
{
std::string text = "0.123 2";
double resultValue = 0.0;
auto isOk = RiaStdStringTools::toDouble( text, resultValue );
EXPECT_TRUE( isOk );
EXPECT_DOUBLE_EQ( resultValue, 0.123 );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RiaStdStringToolsTest, TestToInt )
{
{
std::string text = " 12";
int resultValue = -1;
auto isOk = RiaStdStringTools::toInt( text, resultValue );
EXPECT_TRUE( isOk );
EXPECT_EQ( resultValue, 12 );
}
{
std::string text = " 12 ";
int resultValue = -1;
auto isOk = RiaStdStringTools::toInt( text, resultValue );
EXPECT_TRUE( isOk );
EXPECT_EQ( resultValue, 12 );
}
{
std::string text = "12";
int resultValue = -1;
auto isOk = RiaStdStringTools::toInt( text, resultValue );
EXPECT_TRUE( isOk );
EXPECT_EQ( resultValue, 12 );
}
}