#2003 Move conversion from string to integer into RiaStdStringTools

This commit is contained in:
Magne Sjaastad
2017-11-03 14:36:59 +01:00
parent 265c0ebb70
commit 713998fb38
5 changed files with 82 additions and 56 deletions

View File

@@ -18,9 +18,11 @@
#include "RiaDateStringParser.h" #include "RiaDateStringParser.h"
#include <algorithm> #include "RiaStdStringTools.h"
#include "RiaQDateTimeTools.h" #include "RiaQDateTimeTools.h"
#include <algorithm>
const std::string MONTH_NAMES[] = const std::string MONTH_NAMES[] =
{ {
"january", "january",
@@ -138,19 +140,16 @@ bool RiaDateStringParser::tryParseMonthFirst(const std::string& s, int& year, in
bool RiaDateStringParser::tryParseYear(const std::string& s, int &year) bool RiaDateStringParser::tryParseYear(const std::string& s, int &year)
{ {
if (containsAlphabetic(s)) return false; if (containsAlphabetic(s)) return false;
try
{ auto today = QDate::currentDate();
auto today = QDate::currentDate(); int y = RiaStdStringTools::toInt(s);
int y = std::stoi(s); if (y > 1970 && y <= today.year())
if (y > 1970 && y <= today.year())
{
year = y;
return true;
}
}
catch (...)
{ {
year = y;
return true;
} }
return false; return false;
} }
@@ -174,19 +173,15 @@ bool RiaDateStringParser::tryParseMonth(const std::string& s, int &month)
} }
else else
{ {
try int m = RiaStdStringTools::toInt(s);
{ if (m >= 1 && m <= 12)
int m = std::stoi(s);
if (m >= 1 && m <= 12)
{
month = m;
return true;
}
}
catch (...)
{ {
month = m;
return true;
} }
} }
return false; return false;
} }
@@ -196,18 +191,15 @@ bool RiaDateStringParser::tryParseMonth(const std::string& s, int &month)
bool RiaDateStringParser::tryParseDay(const std::string& s, int &day) bool RiaDateStringParser::tryParseDay(const std::string& s, int &day)
{ {
if (containsAlphabetic(s)) return false; if (containsAlphabetic(s)) return false;
try
{ int d = RiaStdStringTools::toInt(s);
int d = std::stoi(s); if (d >= 1 && d <= 31)
if (d >= 1 && d <= 31)
{
day = d;
return true;
}
}
catch (...)
{ {
day = d;
return true;
} }
return false; return false;
} }
@@ -231,4 +223,3 @@ std::string RiaDateStringParser::trimString(const std::string& s)
return sCopy; return sCopy;
} }

View File

@@ -28,3 +28,29 @@ std::string RiaStdStringTools::trimString(const std::string& s)
return sCopy; return sCopy;
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaStdStringTools::isNumber(const std::string& s)
{
return (s.find_first_not_of("0123456789.eE-") != std::string::npos);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaStdStringTools::toInt(const std::string& s)
{
int intValue = -1;
try
{
intValue = std::stoi(s);
}
catch (...)
{
}
return intValue;
}

View File

@@ -26,6 +26,9 @@
class RiaStdStringTools class RiaStdStringTools
{ {
public: public:
static std::string trimString(const std::string& s); static std::string trimString(const std::string& s);
static bool isNumber(const std::string& s);
static int toInt(const std::string& s);
}; };

View File

@@ -17,6 +17,9 @@
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
#include "RifEclipseSummaryAddress.h" #include "RifEclipseSummaryAddress.h"
#include "RiaStdStringTools.h"
#include "cvfAssert.h" #include "cvfAssert.h"
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@@ -37,7 +40,7 @@ RifEclipseSummaryAddress::RifEclipseSummaryAddress(SummaryVarCategory category,
switch (category) switch (category)
{ {
case SUMMARY_REGION: case SUMMARY_REGION:
m_regionNumber = std::stoi(identifiers[INPUT_REGION_NUMBER]); m_regionNumber = RiaStdStringTools::toInt(identifiers[INPUT_REGION_NUMBER]);
break; break;
case SUMMARY_REGION_2_REGION: case SUMMARY_REGION_2_REGION:
reg2regPair = regionToRegionPairFromUiText(identifiers[INPUT_REGION_2_REGION]); reg2regPair = regionToRegionPairFromUiText(identifiers[INPUT_REGION_2_REGION]);
@@ -71,7 +74,7 @@ RifEclipseSummaryAddress::RifEclipseSummaryAddress(SummaryVarCategory category,
break; break;
case SUMMARY_WELL_SEGMENT: case SUMMARY_WELL_SEGMENT:
m_wellName = identifiers[INPUT_WELL_NAME]; m_wellName = identifiers[INPUT_WELL_NAME];
m_wellSegmentNumber = std::stoi(identifiers[INPUT_SEGMENT_NUMBER]); m_wellSegmentNumber = RiaStdStringTools::toInt(identifiers[INPUT_SEGMENT_NUMBER]);
case SUMMARY_BLOCK: case SUMMARY_BLOCK:
ijkTuple = ijkTupleFromUiText(identifiers[INPUT_CELL_IJK]); ijkTuple = ijkTupleFromUiText(identifiers[INPUT_CELL_IJK]);
m_cellI = std::get<0>(ijkTuple); m_cellI = std::get<0>(ijkTuple);
@@ -303,7 +306,8 @@ std::tuple<int, int, int> RifEclipseSummaryAddress::ijkTupleFromUiText(const std
auto textI = s.substr(0, firstSep); auto textI = s.substr(0, firstSep);
auto textJ = s.substr(firstSep + 1, lastSep - firstSep - 1); auto textJ = s.substr(firstSep + 1, lastSep - firstSep - 1);
auto textK = s.substr(lastSep + 1); auto textK = s.substr(lastSep + 1);
return std::make_tuple(std::stoi(textI), std::stoi(textJ), std::stoi(textK));
return std::make_tuple(RiaStdStringTools::toInt(textI), RiaStdStringTools::toInt(textJ), RiaStdStringTools::toInt(textK));
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@@ -324,7 +328,8 @@ std::pair<int, int> RifEclipseSummaryAddress::regionToRegionPairFromUiText(const
CVF_ASSERT(sep != std::string::npos ); CVF_ASSERT(sep != std::string::npos );
auto textReg = s.substr(0, sep); auto textReg = s.substr(0, sep);
auto textReg2 = s.substr(sep + 2); auto textReg2 = s.substr(sep + 2);
return std::make_pair(std::stoi(textReg), std::stoi(textReg2));
return std::make_pair(RiaStdStringTools::toInt(textReg), RiaStdStringTools::toInt(textReg2));
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View File

@@ -19,6 +19,7 @@
#include "RifEclipseUserDataKeywordTools.h" #include "RifEclipseUserDataKeywordTools.h"
#include "RiaLogging.h" #include "RiaLogging.h"
#include "RiaStdStringTools.h"
#include "RifEclipseUserDataParserTools.h" #include "RifEclipseUserDataParserTools.h"
@@ -151,7 +152,7 @@ RifEclipseSummaryAddress RifEclipseUserDataKeywordTools::makeAndFillAddress(cons
{ {
if (columnHeaderText.size() > 0) if (columnHeaderText.size() > 0)
{ {
regionNumber = std::stoi(columnHeaderText[0]); regionNumber = RiaStdStringTools::toInt(columnHeaderText[0]);
} }
break; break;
} }
@@ -178,9 +179,9 @@ RifEclipseSummaryAddress RifEclipseUserDataKeywordTools::makeAndFillAddress(cons
if (columnHeaderText.size() > 3) if (columnHeaderText.size() > 3)
{ {
wellName = columnHeaderText[0]; wellName = columnHeaderText[0];
cellI = std::stoi(columnHeaderText[1]); cellI = RiaStdStringTools::toInt(columnHeaderText[1]);
cellJ = std::stoi(columnHeaderText[2]); cellJ = RiaStdStringTools::toInt(columnHeaderText[2]);
cellK = std::stoi(columnHeaderText[3]); cellK = RiaStdStringTools::toInt(columnHeaderText[3]);
} }
break; break;
} }
@@ -195,35 +196,35 @@ RifEclipseSummaryAddress RifEclipseUserDataKeywordTools::makeAndFillAddress(cons
case RifEclipseSummaryAddress::SUMMARY_WELL_COMPLETION_LGR: case RifEclipseSummaryAddress::SUMMARY_WELL_COMPLETION_LGR:
if (columnHeaderText.size() > 4) if (columnHeaderText.size() > 4)
{ {
wellName = columnHeaderText[0]; wellName = columnHeaderText[0];
lgrName = columnHeaderText[1]; lgrName = columnHeaderText[1];
cellI = std::stoi(columnHeaderText[2]); cellI = RiaStdStringTools::toInt(columnHeaderText[2]);
cellJ = std::stoi(columnHeaderText[3]); cellJ = RiaStdStringTools::toInt(columnHeaderText[3]);
cellK = std::stoi(columnHeaderText[4]); cellK = RiaStdStringTools::toInt(columnHeaderText[4]);
} }
break; break;
case RifEclipseSummaryAddress::SUMMARY_WELL_SEGMENT: case RifEclipseSummaryAddress::SUMMARY_WELL_SEGMENT:
if (columnHeaderText.size() > 1) if (columnHeaderText.size() > 1)
{ {
wellName = columnHeaderText[0]; wellName = columnHeaderText[0];
wellSegmentNumber = std::stoi(columnHeaderText[1]); wellSegmentNumber = RiaStdStringTools::toInt(columnHeaderText[1]);
} }
break; break;
case RifEclipseSummaryAddress::SUMMARY_BLOCK: case RifEclipseSummaryAddress::SUMMARY_BLOCK:
if (columnHeaderText.size() > 2) if (columnHeaderText.size() > 2)
{ {
cellI = std::stoi(columnHeaderText[0]); cellI = RiaStdStringTools::toInt(columnHeaderText[0]);
cellJ = std::stoi(columnHeaderText[1]); cellJ = RiaStdStringTools::toInt(columnHeaderText[1]);
cellK = std::stoi(columnHeaderText[2]); cellK = RiaStdStringTools::toInt(columnHeaderText[2]);
} }
break; break;
case RifEclipseSummaryAddress::SUMMARY_BLOCK_LGR: case RifEclipseSummaryAddress::SUMMARY_BLOCK_LGR:
if (columnHeaderText.size() > 3) if (columnHeaderText.size() > 3)
{ {
lgrName = columnHeaderText[0]; lgrName = columnHeaderText[0];
cellI = std::stoi(columnHeaderText[1]); cellI = RiaStdStringTools::toInt(columnHeaderText[1]);
cellJ = std::stoi(columnHeaderText[2]); cellJ = RiaStdStringTools::toInt(columnHeaderText[2]);
cellK = std::stoi(columnHeaderText[3]); cellK = RiaStdStringTools::toInt(columnHeaderText[3]);
} }
break; break;
case RifEclipseSummaryAddress::SUMMARY_CALCULATED: case RifEclipseSummaryAddress::SUMMARY_CALCULATED: