Merge pull request #4936 from OPM/emsemble-parsing

Improve detection of numbers when parsing std string
This commit is contained in:
Magne Sjaastad authored and GitHub committed 2019-10-28 07:53:06 +01:00
commit a4c78cb7c3
3 files changed
+32 -3

No files matched your search

@@ -34,15 +34,18 @@ std::string RiaStdStringTools::trimString( const std::string& s )
//--------------------------------------------------------------------------------------------------
bool RiaStdStringTools::isNumber( const std::string& s, char decimalPoint )
{
if ( s.size() == 0 ) return false;
if ( s.empty() ) return false;
if ( findCharMatchCount( s, decimalPoint ) > 1 ) return false;
if ( findCharMatchCount( s, '-' ) > 1 ) return false;
if ( findCharMatchCount( s, 'e' ) > 1 ) return false;
if ( findCharMatchCount( s, 'E' ) > 1 ) return false;
std::string matchChars( "0123456789eE-" );
std::string matchChars( "0123456789eE-+" );
matchChars.append( 1, decimalPoint );
return ( s.find_first_not_of( matchChars ) == std::string::npos );
auto it = s.find_first_not_of( matchChars );
return ( it == std::string::npos );
}
//--------------------------------------------------------------------------------------------------
@@ -60,6 +60,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RimSummaryCaseCollection-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifActiveCellsReader-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifCsvDataTableFormatter-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaSummaryCurveAnalyzer-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaStdStringTools-Test.cpp
)
if (RESINSIGHT_ENABLE_GRPC)
@@ -0,0 +1,25 @@
#include "gtest/gtest.h"
#include "RiaStdStringTools.h"
#include <QLocale>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RiaStdStringToolsTest, ParseNumbers )
{
auto decimalPoint = QLocale::c().decimalPoint().toLatin1();
{
std::string text = "8.73705e+06";
EXPECT_TRUE( RiaStdStringTools::isNumber( text, decimalPoint ) );
}
{
std::string text = "-8.73705e-06";
EXPECT_TRUE( RiaStdStringTools::isNumber( text, decimalPoint ) );
}
}