mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#4918 Improve detection of numbers when parsing std string
This commit is contained in:
parent
72e3ddc35e
commit
12478a6eef
@ -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)
|
||||
|
25
ApplicationCode/UnitTests/RiaStdStringTools-Test.cpp
Normal file
25
ApplicationCode/UnitTests/RiaStdStringTools-Test.cpp
Normal file
@ -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 ) );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user