mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#2003 Move conversion from string to integer into RiaStdStringTools
This commit is contained in:
parent
265c0ebb70
commit
713998fb38
@ -18,9 +18,11 @@
|
||||
|
||||
#include "RiaDateStringParser.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include "RiaStdStringTools.h"
|
||||
#include "RiaQDateTimeTools.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
const std::string MONTH_NAMES[] =
|
||||
{
|
||||
"january",
|
||||
@ -138,19 +140,16 @@ bool RiaDateStringParser::tryParseMonthFirst(const std::string& s, int& year, in
|
||||
bool RiaDateStringParser::tryParseYear(const std::string& s, int &year)
|
||||
{
|
||||
if (containsAlphabetic(s)) return false;
|
||||
try
|
||||
{
|
||||
auto today = QDate::currentDate();
|
||||
int y = std::stoi(s);
|
||||
if (y > 1970 && y <= today.year())
|
||||
{
|
||||
year = y;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
|
||||
auto today = QDate::currentDate();
|
||||
int y = RiaStdStringTools::toInt(s);
|
||||
if (y > 1970 && y <= today.year())
|
||||
{
|
||||
year = y;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -174,19 +173,15 @@ bool RiaDateStringParser::tryParseMonth(const std::string& s, int &month)
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
int m = std::stoi(s);
|
||||
if (m >= 1 && m <= 12)
|
||||
{
|
||||
month = m;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
int m = RiaStdStringTools::toInt(s);
|
||||
if (m >= 1 && m <= 12)
|
||||
{
|
||||
month = m;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -196,18 +191,15 @@ bool RiaDateStringParser::tryParseMonth(const std::string& s, int &month)
|
||||
bool RiaDateStringParser::tryParseDay(const std::string& s, int &day)
|
||||
{
|
||||
if (containsAlphabetic(s)) return false;
|
||||
try
|
||||
{
|
||||
int d = std::stoi(s);
|
||||
if (d >= 1 && d <= 31)
|
||||
{
|
||||
day = d;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
|
||||
int d = RiaStdStringTools::toInt(s);
|
||||
if (d >= 1 && d <= 31)
|
||||
{
|
||||
day = d;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -231,4 +223,3 @@ std::string RiaDateStringParser::trimString(const std::string& s)
|
||||
|
||||
return sCopy;
|
||||
}
|
||||
|
||||
|
@ -28,3 +28,29 @@ std::string RiaStdStringTools::trimString(const std::string& s)
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -26,6 +26,9 @@
|
||||
class RiaStdStringTools
|
||||
{
|
||||
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);
|
||||
};
|
||||
|
||||
|
@ -17,6 +17,9 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "RifEclipseSummaryAddress.h"
|
||||
|
||||
#include "RiaStdStringTools.h"
|
||||
|
||||
#include "cvfAssert.h"
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -37,7 +40,7 @@ RifEclipseSummaryAddress::RifEclipseSummaryAddress(SummaryVarCategory category,
|
||||
switch (category)
|
||||
{
|
||||
case SUMMARY_REGION:
|
||||
m_regionNumber = std::stoi(identifiers[INPUT_REGION_NUMBER]);
|
||||
m_regionNumber = RiaStdStringTools::toInt(identifiers[INPUT_REGION_NUMBER]);
|
||||
break;
|
||||
case SUMMARY_REGION_2_REGION:
|
||||
reg2regPair = regionToRegionPairFromUiText(identifiers[INPUT_REGION_2_REGION]);
|
||||
@ -71,7 +74,7 @@ RifEclipseSummaryAddress::RifEclipseSummaryAddress(SummaryVarCategory category,
|
||||
break;
|
||||
case SUMMARY_WELL_SEGMENT:
|
||||
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:
|
||||
ijkTuple = ijkTupleFromUiText(identifiers[INPUT_CELL_IJK]);
|
||||
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 textJ = s.substr(firstSep + 1, lastSep - firstSep - 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 );
|
||||
auto textReg = s.substr(0, sep);
|
||||
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));
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "RifEclipseUserDataKeywordTools.h"
|
||||
|
||||
#include "RiaLogging.h"
|
||||
#include "RiaStdStringTools.h"
|
||||
|
||||
#include "RifEclipseUserDataParserTools.h"
|
||||
|
||||
@ -151,7 +152,7 @@ RifEclipseSummaryAddress RifEclipseUserDataKeywordTools::makeAndFillAddress(cons
|
||||
{
|
||||
if (columnHeaderText.size() > 0)
|
||||
{
|
||||
regionNumber = std::stoi(columnHeaderText[0]);
|
||||
regionNumber = RiaStdStringTools::toInt(columnHeaderText[0]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -178,9 +179,9 @@ RifEclipseSummaryAddress RifEclipseUserDataKeywordTools::makeAndFillAddress(cons
|
||||
if (columnHeaderText.size() > 3)
|
||||
{
|
||||
wellName = columnHeaderText[0];
|
||||
cellI = std::stoi(columnHeaderText[1]);
|
||||
cellJ = std::stoi(columnHeaderText[2]);
|
||||
cellK = std::stoi(columnHeaderText[3]);
|
||||
cellI = RiaStdStringTools::toInt(columnHeaderText[1]);
|
||||
cellJ = RiaStdStringTools::toInt(columnHeaderText[2]);
|
||||
cellK = RiaStdStringTools::toInt(columnHeaderText[3]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -195,35 +196,35 @@ RifEclipseSummaryAddress RifEclipseUserDataKeywordTools::makeAndFillAddress(cons
|
||||
case RifEclipseSummaryAddress::SUMMARY_WELL_COMPLETION_LGR:
|
||||
if (columnHeaderText.size() > 4)
|
||||
{
|
||||
wellName = columnHeaderText[0];
|
||||
lgrName = columnHeaderText[1];
|
||||
cellI = std::stoi(columnHeaderText[2]);
|
||||
cellJ = std::stoi(columnHeaderText[3]);
|
||||
cellK = std::stoi(columnHeaderText[4]);
|
||||
wellName = columnHeaderText[0];
|
||||
lgrName = columnHeaderText[1];
|
||||
cellI = RiaStdStringTools::toInt(columnHeaderText[2]);
|
||||
cellJ = RiaStdStringTools::toInt(columnHeaderText[3]);
|
||||
cellK = RiaStdStringTools::toInt(columnHeaderText[4]);
|
||||
}
|
||||
break;
|
||||
case RifEclipseSummaryAddress::SUMMARY_WELL_SEGMENT:
|
||||
if (columnHeaderText.size() > 1)
|
||||
{
|
||||
wellName = columnHeaderText[0];
|
||||
wellSegmentNumber = std::stoi(columnHeaderText[1]);
|
||||
wellName = columnHeaderText[0];
|
||||
wellSegmentNumber = RiaStdStringTools::toInt(columnHeaderText[1]);
|
||||
}
|
||||
break;
|
||||
case RifEclipseSummaryAddress::SUMMARY_BLOCK:
|
||||
if (columnHeaderText.size() > 2)
|
||||
{
|
||||
cellI = std::stoi(columnHeaderText[0]);
|
||||
cellJ = std::stoi(columnHeaderText[1]);
|
||||
cellK = std::stoi(columnHeaderText[2]);
|
||||
cellI = RiaStdStringTools::toInt(columnHeaderText[0]);
|
||||
cellJ = RiaStdStringTools::toInt(columnHeaderText[1]);
|
||||
cellK = RiaStdStringTools::toInt(columnHeaderText[2]);
|
||||
}
|
||||
break;
|
||||
case RifEclipseSummaryAddress::SUMMARY_BLOCK_LGR:
|
||||
if (columnHeaderText.size() > 3)
|
||||
{
|
||||
lgrName = columnHeaderText[0];
|
||||
cellI = std::stoi(columnHeaderText[1]);
|
||||
cellJ = std::stoi(columnHeaderText[2]);
|
||||
cellK = std::stoi(columnHeaderText[3]);
|
||||
lgrName = columnHeaderText[0];
|
||||
cellI = RiaStdStringTools::toInt(columnHeaderText[1]);
|
||||
cellJ = RiaStdStringTools::toInt(columnHeaderText[2]);
|
||||
cellK = RiaStdStringTools::toInt(columnHeaderText[3]);
|
||||
}
|
||||
break;
|
||||
case RifEclipseSummaryAddress::SUMMARY_CALCULATED:
|
||||
|
Loading…
Reference in New Issue
Block a user