Skip to content

Commit

Permalink
Replace use of QRegExp with QRegularExpression
Browse files Browse the repository at this point in the history
Replace use of QRegExp with QRegularExpression
Remove dependency on qt5compat module
  • Loading branch information
magnesj committed Oct 25, 2024
1 parent 0e9dd95 commit a974bb1
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 120 deletions.
1 change: 0 additions & 1 deletion ApplicationLibCode/Application/RiaPreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
#include <QDate>
#include <QDir>
#include <QLocale>
#include <QRegExp>
#include <QStandardPaths>
#include <QValidator>

Expand Down
23 changes: 11 additions & 12 deletions ApplicationLibCode/Application/Tools/RiaTextStringTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ QStringList RiaTextStringTools::splitSkipEmptyParts( const QString& text, const
return splitString( text, sep, skipEmptyParts );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RiaTextStringTools::splitSkipEmptyParts( const QString& text, const QRegularExpression& regularExpression )
{
bool skipEmptyParts = true;
return splitString( text, regularExpression, skipEmptyParts );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand All @@ -144,9 +153,9 @@ QStringList RiaTextStringTools::splitString( const QString& text, const QString&
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RiaTextStringTools::splitString( const QString& text, const QRegExp& regExp, bool skipEmptyParts )
QStringList RiaTextStringTools::splitString( const QString& text, const QRegularExpression& regularExpression, bool skipEmptyParts )
{
return regExp.splitString( text, skipEmptyParts ? Qt::SkipEmptyParts : Qt::KeepEmptyParts );
return text.split( regularExpression, skipEmptyParts ? Qt::SkipEmptyParts : Qt::KeepEmptyParts );
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -197,16 +206,6 @@ bool RiaTextStringTools::isNumber( const QString& text, const QString& decimalPo
return RiaStdStringTools::isNumber( stdString, decimalChar );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RiaTextStringTools::splitSkipEmptyParts( const QString& text, const QRegExp& regExp )
{
bool skipEmptyParts = true;

return splitString( text, regExp, skipEmptyParts );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions ApplicationLibCode/Application/Tools/RiaTextStringTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include <QRegExp>
#include <QRegularExpression>
#include <QString>
#include <QStringList>

Expand All @@ -36,10 +36,10 @@ QString commonSuffix( const QStringList& stringList );
QString trimNonAlphaNumericCharacters( const QString& s );

QStringList splitSkipEmptyParts( const QString& text, const QString& sep = " " );
QStringList splitSkipEmptyParts( const QString& text, const QRegExp& regExp );
QStringList splitSkipEmptyParts( const QString& text, const QRegularExpression& regularExpression );

QStringList splitString( const QString& text, const QString& sep, bool skipEmptyParts );
QStringList splitString( const QString& text, const QRegExp& regExp, bool skipEmptyParts );
QStringList splitString( const QString& text, const QRegularExpression& regularExpression, bool skipEmptyParts );

QString replaceTemplateTextWithValues( const QString& templateText, const std::map<QString, QString>& valueMap );
bool isTextEqual( QStringView text, QStringView compareText );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ bool RiaValidRegExpValidator::isValidCharacter( const QChar& character )
//--------------------------------------------------------------------------------------------------
QValidator::State RiaValidRegExpValidator::validate( QString& inputString, int& position ) const
{
QRegExp inputRe( inputString, Qt::CaseInsensitive, QRegExp::Wildcard );
if ( inputRe.isValid() ) // A valid wildcard pattern is always acceptable
QString regexPattern = QRegularExpression::wildcardToRegularExpression( inputString );
QRegularExpression regex( regexPattern, QRegularExpression::CaseInsensitiveOption );

if ( regex.isValid() )
{
return QValidator::Acceptable;
}
Expand Down Expand Up @@ -70,4 +72,4 @@ QValidator::State RiaValidRegExpValidator::validate( QString& inputString, int&
void RiaValidRegExpValidator::fixup( QString& inputString ) const
{
inputString = m_defaultPattern;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
/////////////////////////////////////////////////////////////////////////////////
#pragma once

#include <QRegExp>
#include <QString>
#include <QValidator>

Expand All @@ -36,4 +35,4 @@ class RiaValidRegExpValidator : public QValidator

private:
QString m_defaultPattern;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ void RiaSummaryStringTools::splitUsingDataSourceNames( const QStringList& filter

bool foundDataSource = false;

QRegExp searcher( pureDataSourceCandidate, Qt::CaseInsensitive, QRegExp::WildcardUnix );
QString regexPattern = QRegularExpression::wildcardToRegularExpression( pureDataSourceCandidate );
QRegularExpression searcher( regexPattern, QRegularExpression::CaseInsensitiveOption );

for ( const auto& ds : dataSourceNames )
{
if ( !foundDataSource && searcher.exactMatch( ds ) )
if ( !foundDataSource && searcher.match( ds ).hasMatch() )
{
dataSourceFilters.push_back( s );
foundDataSource = true;
Expand Down Expand Up @@ -166,13 +167,14 @@ std::pair<std::vector<RimSummaryCase*>, std::vector<RimSummaryEnsemble*>>

for ( const auto& dsFilter : dataSourceFilters )
{
QString searchString = dsFilter.left( dsFilter.indexOf( ':' ) );
QRegExp searcher( searchString, Qt::CaseInsensitive, QRegExp::WildcardUnix );
QString searchString = dsFilter.left( dsFilter.indexOf( ':' ) );
QString regexPattern = QRegularExpression::wildcardToRegularExpression( searchString );
QRegularExpression searcher( regexPattern, QRegularExpression::CaseInsensitiveOption );

for ( const auto& ensemble : allEnsembles )
{
auto ensembleName = ensemble->name();
if ( searcher.exactMatch( ensembleName ) )
if ( searcher.match( ensembleName ).hasMatch() )
{
if ( searchString == dsFilter )
{
Expand All @@ -184,13 +186,14 @@ std::pair<std::vector<RimSummaryCase*>, std::vector<RimSummaryEnsemble*>>
{
// Match on subset of realisations in ensemble

QString realizationSearchString = dsFilter.right( dsFilter.size() - dsFilter.indexOf( ':' ) - 1 );
QRegExp realizationSearcher( realizationSearchString, Qt::CaseInsensitive, QRegExp::WildcardUnix );
QString realizationSearchString = dsFilter.right( dsFilter.size() - dsFilter.indexOf( ':' ) - 1 );
QString regexPattern = QRegularExpression::wildcardToRegularExpression( realizationSearchString );
QRegularExpression realizationSearcher( regexPattern, QRegularExpression::CaseInsensitiveOption );

for ( const auto& summaryCase : ensemble->allSummaryCases() )
{
auto realizationName = summaryCase->displayCaseName();
if ( realizationSearcher.exactMatch( realizationName ) )
if ( realizationSearcher.match( realizationName ).hasMatch() )
{
matchingSummaryCases.push_back( summaryCase );
}
Expand All @@ -202,7 +205,7 @@ std::pair<std::vector<RimSummaryCase*>, std::vector<RimSummaryEnsemble*>>
for ( const auto& summaryCase : allSummaryCases )
{
auto summaryCaseName = summaryCase->displayCaseName();
if ( searcher.exactMatch( summaryCaseName ) )
if ( searcher.match( summaryCaseName ).hasMatch() )
{
matchingSummaryCases.push_back( summaryCase );
}
Expand All @@ -217,7 +220,7 @@ std::pair<std::vector<RimSummaryCase*>, std::vector<RimSummaryEnsemble*>>
//--------------------------------------------------------------------------------------------------
QStringList RiaSummaryStringTools::splitIntoWords( const QString& text )
{
return RiaTextStringTools::splitSkipEmptyParts( text, QRegExp( "\\s+" ) );
return RiaTextStringTools::splitSkipEmptyParts( text );
}

//--------------------------------------------------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions ApplicationLibCode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ find_package(
PrintSupport
Svg
Sql
Core5Compat
OPTIONAL_COMPONENTS Charts
)
set(QT_LIBRARIES
Expand All @@ -51,7 +50,6 @@ set(QT_LIBRARIES
Qt6::PrintSupport
Qt6::Svg
Qt6::Sql
Qt6::Core5Compat
Qt6::Charts
)
qt_standard_project_setup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void RifCaseRealizationParametersReader::parse()
QString line = dataStream.readLine();

lineNo++;
QStringList cols = RifFileParseTools::splitLineAndTrim( line, QRegExp( "[ \t]" ), true );
QStringList cols = RifFileParseTools::splitLineAndTrim( line, QRegularExpression( "[ \t]" ), true );

if ( cols.size() != 2 )
{
Expand Down Expand Up @@ -289,19 +289,18 @@ int RifCaseRealizationParametersFileLocator::realizationNumber( const QString& m

int resultIndex = -1;

// Use parenthesis to indicate capture of sub string
QString pattern = "(realization-\\d+)";
QRegularExpression pattern( "realization-(\\d+)", QRegularExpression::CaseInsensitiveOption );
QRegularExpressionMatch match = pattern.match( absolutePath );

QRegExp regexp( pattern, Qt::CaseInsensitive );
if ( regexp.indexIn( absolutePath ) )
if ( match.hasMatch() )
{
QString tempText = regexp.cap( 1 );
QString tempText = match.captured( 1 );

QRegExp rx( "(\\d+)" ); // Find number
int digitPos = rx.indexIn( tempText );
if ( digitPos > -1 )
QRegularExpression rx( "(\\d+)" );
QRegularExpressionMatch digitMatch = rx.match( tempText );
if ( digitMatch.hasMatch() )
{
resultIndex = rx.cap( 0 ).toInt();
resultIndex = digitMatch.captured( 1 ).toInt();
}
}

Expand Down
2 changes: 1 addition & 1 deletion ApplicationLibCode/FileInterface/RifColorLegendData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ cvf::ref<RigFormationNames> RifColorLegendData::readLyrFormationNameFile( const
if ( QColor::isValidColorName( colorWord ) ) numberString.remove( colorWord ); // remove color if present as last word on line

// extract words containing formation number(s)
QStringList numberWords = RiaTextStringTools::splitSkipEmptyParts( numberString, QRegExp( "-" ) );
QStringList numberWords = RiaTextStringTools::splitSkipEmptyParts( numberString, QRegularExpression( "-" ) );

if ( numberWords.size() == 2 ) // formation range with or without color at end of line
{
Expand Down
18 changes: 10 additions & 8 deletions ApplicationLibCode/FileInterface/RifCsvUserDataParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,12 @@ bool RifCsvUserDataParser::parseColumnInfo( QTextStream*
// "VECTOR_NAME [unit]"
{
// "VECTORNAME (unit)" ==> "(unit)"
QRegExp exp( "[[]([^]]+)[]]" );
if ( exp.indexIn( colName ) >= 0 )
QRegularExpression exp( R"(\[([^\]]+)\])" );
QRegularExpressionMatch match = exp.match( colName );
if ( match.hasMatch() )
{
QString fullCapture = exp.cap( 0 );
QString unitCapture = exp.cap( 1 );
QString fullCapture = match.captured( 0 );
QString unitCapture = match.captured( 1 );

unit = unitCapture;
colName = RiaTextStringTools::trimAndRemoveDoubleSpaces( colName.remove( fullCapture ) );
Expand All @@ -405,11 +406,12 @@ bool RifCsvUserDataParser::parseColumnInfo( QTextStream*

{
// "VECTOR_NAME [unit]" ==> "[unit]"
QRegExp exp( "[(]([^)]+)[)]" );
if ( exp.indexIn( colName ) >= 0 )
QRegularExpression exp( R"(\(([^)]+)\))" );
QRegularExpressionMatch match = exp.match( colName );
if ( match.hasMatch() )
{
QString fullCapture = exp.cap( 0 );
QString unitCapture = exp.cap( 1 );
QString fullCapture = match.captured( 0 );
QString unitCapture = match.captured( 1 );

unit = unitCapture;
colName = RiaTextStringTools::trimAndRemoveDoubleSpaces( colName.remove( fullCapture ) );
Expand Down
12 changes: 6 additions & 6 deletions ApplicationLibCode/FileInterface/RifEclipseSummaryAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "RifEclEclipseSummary.h"
#include "RiuSummaryQuantityNameInfoProvider.h"

#include <QRegExp>
#include <QStringList>
#include <QTextStream>

Expand Down Expand Up @@ -783,9 +782,10 @@ bool RifEclipseSummaryAddress::isUiTextMatchingFilterText( const QString& filter
if ( filterString.isEmpty() ) return true;
if ( filterString.trimmed() == "*" ) return !value.empty();

QRegExp searcher( filterString, Qt::CaseInsensitive, QRegExp::WildcardUnix );
QString qstrValue = QString::fromStdString( value );
return searcher.exactMatch( qstrValue );
QString pattern = QRegularExpression::wildcardToRegularExpression( filterString );
QRegularExpression searcher( pattern, QRegularExpression::CaseInsensitiveOption );
QString qstrValue = QString::fromStdString( value );
return searcher.match( qstrValue ).hasMatch();
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1226,7 +1226,7 @@ std::string RifEclipseSummaryAddress::blockAsString() const
//--------------------------------------------------------------------------------------------------
std::tuple<int, int, int> RifEclipseSummaryAddress::ijkTupleFromUiText( const std::string& s )
{
auto ijk = RiaTextStringTools::splitSkipEmptyParts( QString::fromStdString( s ).trimmed(), QRegExp( "[,]" ) );
auto ijk = RiaTextStringTools::splitSkipEmptyParts( QString::fromStdString( s ).trimmed(), QRegularExpression( "[,]" ) );

if ( ijk.size() != 3 ) return std::make_tuple( -1, -1, -1 );

Expand All @@ -1248,7 +1248,7 @@ std::string RifEclipseSummaryAddress::formatUiTextRegionToRegion() const
//--------------------------------------------------------------------------------------------------
std::pair<int, int> RifEclipseSummaryAddress::regionToRegionPairFromUiText( const std::string& s )
{
auto r2r = RiaTextStringTools::splitSkipEmptyParts( QString::fromStdString( s ).trimmed(), QRegExp( "[-]" ) );
auto r2r = RiaTextStringTools::splitSkipEmptyParts( QString::fromStdString( s ).trimmed(), QRegularExpression( "[-]" ) );

if ( r2r.size() != 2 ) return std::make_pair( -1, -1 );

Expand Down
10 changes: 1 addition & 9 deletions ApplicationLibCode/FileInterface/RifFileParseTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@
#include "RifFileParseTools.h"
#include "RiaTextStringTools.h"

// Disable deprecation warning for QString::SkipEmptyParts
#ifdef _MSC_VER
#pragma warning( disable : 4996 )
#endif
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand All @@ -43,7 +35,7 @@ QStringList RifFileParseTools::splitLineAndTrim( const QString& line, const QStr
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RifFileParseTools::splitLineAndTrim( const QString& line, const QRegExp& regexp, bool skipEmptyParts )
QStringList RifFileParseTools::splitLineAndTrim( const QString& line, const QRegularExpression& regexp, bool skipEmptyParts )
{
auto cols = RiaTextStringTools::splitString( line.trimmed(), regexp, skipEmptyParts );
for ( QString& col : cols )
Expand Down
4 changes: 2 additions & 2 deletions ApplicationLibCode/FileInterface/RifFileParseTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include <QRegExp>
#include <QRegularExpression>
#include <QString>
#include <QStringList>

Expand All @@ -29,7 +29,7 @@ class RifFileParseTools
{
public:
static QStringList splitLineAndTrim( const QString& line, const QString& separator, bool skipEmptyParts = false );
static QStringList splitLineAndTrim( const QString& line, const QRegExp& regexp, bool skipEmptyParts = false );
static QStringList splitLineAndTrim( const QString& line, const QRegularExpression& regexp, bool skipEmptyParts = false );
};

//==================================================================================================
Expand Down
2 changes: 1 addition & 1 deletion ApplicationLibCode/FileInterface/RifPolygonReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ std::vector<std::vector<cvf::Vec3d>> RifPolygonReader::parseText( const QString&
QStringList commentLineSegs = line.split( "#" );
if ( commentLineSegs.empty() ) continue; // Empty line

QStringList lineSegs = RiaTextStringTools::splitSkipEmptyParts( commentLineSegs[0], QRegExp( "\\s+" ) );
QStringList lineSegs = RiaTextStringTools::splitSkipEmptyParts( commentLineSegs[0], QRegularExpression( "\\s+" ) );

if ( lineSegs.empty() ) continue; // No data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void RimPolylinesFromFileAnnotation::readPolyLinesFile( QString* errorMessage )
QStringList commentLineSegs = line.split( "#" );
if ( commentLineSegs.empty() ) continue; // Empty line

QStringList lineSegs = RiaTextStringTools::splitSkipEmptyParts( commentLineSegs[0], QRegExp( "\\s+" ) );
QStringList lineSegs = RiaTextStringTools::splitSkipEmptyParts( commentLineSegs[0], QRegularExpression( "\\s+" ) );

if ( lineSegs.empty() ) continue; // No data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,9 @@ QString RimWellPathCompletionSettings::fluidInPlaceRegionForExport() const
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QRegExp RimWellPathCompletionSettings::wellNameForExportRegExp()
QRegularExpression RimWellPathCompletionSettings::wellNameForExportRegExp()
{
QRegExp rx( "[\\w\\-\\_]{1,8}" );
return rx;
return QRegularExpression( "[\\w\\-\\_]{1,8}" );
}

//--------------------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit a974bb1

Please sign in to comment.