Skip to content

Commit

Permalink
[qt6][symbology] Migrate away from QRegExp in src/core
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jul 14, 2021
1 parent 90a5f36 commit 89e126e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 64 deletions.
29 changes: 15 additions & 14 deletions src/core/symbology/qgscptcityarchive.cpp
Expand Up @@ -15,15 +15,6 @@
* *
***************************************************************************/

#include <QApplication>
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QVector>
#include <QStyle>
#include <QDomDocument>
#include <QDomElement>

#include "qgssettings.h"
#include "qgscptcityarchive.h"
#include "qgis.h"
Expand All @@ -34,6 +25,16 @@
#include "qgsapplication.h"
#include "qgssymbollayerutils.h"

#include <QApplication>
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QVector>
#include <QStyle>
#include <QDomDocument>
#include <QDomElement>
#include <QRegularExpression>

typedef QMap< QString, QgsCptCityArchive * > ArchiveRegistry;
typedef QMap< QString, QMap< QString, QString > > CopyingInfoMap;

Expand Down Expand Up @@ -974,12 +975,12 @@ QMap< QString, QStringList > QgsCptCityDirectoryItem::rampsMap()
}
else
{
QRegExp rxVariant( "^(.*[^\\d])(\\d{1,3})$" );
int pos = rxVariant.indexIn( schemeName );
if ( pos > -1 )
const thread_local QRegularExpression rxVariant( "^(.*[^\\d])(\\d{1,3})$" );
const QRegularExpressionMatch match = rxVariant.match( schemeName );
if ( match.hasMatch() )
{
curName = rxVariant.cap( 1 );
curVariant = rxVariant.cap( 2 );
curName = match.captured( 1 );
curVariant = match.captured( 2 );
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology/qgsrendererrange.cpp
Expand Up @@ -16,6 +16,7 @@
#include "qgsrendererrange.h"
#include "qgsclassificationmethod.h"
#include "qgssymbol.h"

#include <QLocale>


Expand Down Expand Up @@ -220,7 +221,7 @@ QString QgsRendererRangeLabelFormat::formatNumber( double value ) const
QString valueStr = QLocale().toString( value, 'f', mPrecision );
if ( mTrimTrailingZeroes )
valueStr = valueStr.remove( mReTrailingZeroes );
if ( mReNegativeZero.exactMatch( valueStr ) )
if ( mReNegativeZero.match( valueStr ).hasMatch() )
valueStr = valueStr.mid( 1 );
return valueStr;
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/symbology/qgsrendererrange.h
Expand Up @@ -16,12 +16,12 @@
#ifndef QGSRENDERERRANGE_H
#define QGSRENDERERRANGE_H

#include <QRegExp>

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgssymbollayerutils.h"

#include <QRegularExpression>

class QDomDocument;
class QDomElement;

Expand Down Expand Up @@ -141,8 +141,8 @@ class CORE_EXPORT Q_DECL_DEPRECATED QgsRendererRangeLabelFormat SIP_DEPRECATED
// values used to manage number formatting - precision and trailing zeroes
double mNumberScale = 1.0;
QString mNumberSuffix;
QRegExp mReTrailingZeroes;
QRegExp mReNegativeZero;
QRegularExpression mReTrailingZeroes;
QRegularExpression mReNegativeZero;
};


Expand Down
97 changes: 52 additions & 45 deletions src/core/symbology/qgssymbollayerutils.cpp
Expand Up @@ -51,7 +51,6 @@
#include <QIcon>
#include <QPainter>
#include <QSettings>
#include <QRegExp>
#include <QPicture>
#include <QUrl>
#include <QUrlQuery>
Expand Down Expand Up @@ -3258,7 +3257,8 @@ QList<QColor> QgsSymbolLayerUtils::parseColorList( const QString &colorStr )
QList<QColor> colors;

//try splitting string at commas, spaces or newlines
QStringList components = colorStr.simplified().split( QRegExp( "(,|\\s)" ) );
const thread_local QRegularExpression sepCommaSpaceRegExp( "(,|\\s)" );
QStringList components = colorStr.simplified().split( sepCommaSpaceRegExp );
QStringList::iterator it = components.begin();
for ( ; it != components.end(); ++it )
{
Expand All @@ -3274,7 +3274,8 @@ QList<QColor> QgsSymbolLayerUtils::parseColorList( const QString &colorStr )
}

//try splitting string at commas or newlines
components = colorStr.split( QRegExp( "(,|\n)" ) );
const thread_local QRegularExpression sepCommaRegExp( "(,|\n)" );
components = colorStr.split( sepCommaRegExp );
it = components.begin();
for ( ; it != components.end(); ++it )
{
Expand Down Expand Up @@ -3594,10 +3595,11 @@ QgsNamedColorList QgsSymbolLayerUtils::importColorsFromGpl( QFile &file, bool &o
}
if ( line.startsWith( QLatin1String( "Name:" ) ) )
{
QRegExp nameRx( "Name:\\s*(\\S.*)$" );
if ( nameRx.indexIn( line ) != -1 )
const thread_local QRegularExpression nameRx( "Name:\\s*(\\S.*)$" );
const QRegularExpressionMatch match = nameRx.match( line );
if ( match.hasMatch() )
{
name = nameRx.cap( 1 );
name = match.captured( 1 );
}
}

Expand All @@ -3613,17 +3615,18 @@ QgsNamedColorList QgsSymbolLayerUtils::importColorsFromGpl( QFile &file, bool &o
}

//ready to start reading colors
QRegExp rx( "^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)(\\s.*)?$" );
const thread_local QRegularExpression rx( "^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)(\\s.*)?$" );
while ( !in.atEnd() )
{
line = in.readLine();
if ( rx.indexIn( line ) == -1 )
const QRegularExpressionMatch match = rx.match( line );
if ( !match.hasMatch() )
{
continue;
}
int red = rx.cap( 1 ).toInt();
int green = rx.cap( 2 ).toInt();
int blue = rx.cap( 3 ).toInt();
int red = match.captured( 1 ).toInt();
int green = match.captured( 2 ).toInt();
int blue = match.captured( 3 ).toInt();
QColor color = QColor( red, green, blue );
if ( !color.isValid() )
{
Expand All @@ -3634,7 +3637,7 @@ QgsNamedColorList QgsSymbolLayerUtils::importColorsFromGpl( QFile &file, bool &o
QString label;
if ( rx.captureCount() > 3 )
{
label = rx.cap( 4 ).simplified();
label = match.captured( 4 ).simplified();
}
else
{
Expand All @@ -3659,11 +3662,11 @@ QColor QgsSymbolLayerUtils::parseColorWithAlpha( const QString &colorStr, bool &
{
QColor parsedColor;

QRegExp hexColorAlphaRx( "^\\s*#?([0-9a-fA-F]{6})([0-9a-fA-F]{2})\\s*$" );
int hexColorIndex = hexColorAlphaRx.indexIn( colorStr );
const thread_local QRegularExpression hexColorAlphaRx( "^\\s*#?([0-9a-fA-F]{6})([0-9a-fA-F]{2})\\s*$" );
QRegularExpressionMatch match = hexColorAlphaRx.match( colorStr );

//color in hex format "#aabbcc", but not #aabbccdd
if ( hexColorIndex == -1 && QColor::isValidColor( colorStr ) )
if ( !match.hasMatch() && QColor::isValidColor( colorStr ) )
{
//string is a valid hex color string
parsedColor.setNamedColor( colorStr );
Expand All @@ -3675,12 +3678,12 @@ QColor QgsSymbolLayerUtils::parseColorWithAlpha( const QString &colorStr, bool &
}

//color in hex format, with alpha
if ( hexColorIndex > -1 )
if ( match.hasMatch() )
{
QString hexColor = hexColorAlphaRx.cap( 1 );
QString hexColor = match.captured( 1 );
parsedColor.setNamedColor( QStringLiteral( "#" ) + hexColor );
bool alphaOk;
int alphaHex = hexColorAlphaRx.cap( 2 ).toInt( &alphaOk, 16 );
int alphaHex = match.captured( 2 ).toInt( &alphaOk, 16 );

if ( parsedColor.isValid() && alphaOk )
{
Expand All @@ -3693,8 +3696,8 @@ QColor QgsSymbolLayerUtils::parseColorWithAlpha( const QString &colorStr, bool &
if ( !strictEval )
{
//color in hex format, without #
QRegExp hexColorRx2( "^\\s*(?:[0-9a-fA-F]{3}){1,2}\\s*$" );
if ( hexColorRx2.indexIn( colorStr ) != -1 )
const thread_local QRegularExpression hexColorRx2( "^\\s*(?:[0-9a-fA-F]{3}){1,2}\\s*$" );
if ( colorStr.indexOf( hexColorRx2 ) != -1 )
{
//add "#" and parse
parsedColor.setNamedColor( QStringLiteral( "#" ) + colorStr );
Expand All @@ -3707,12 +3710,13 @@ QColor QgsSymbolLayerUtils::parseColorWithAlpha( const QString &colorStr, bool &
}

//color in (rrr,ggg,bbb) format, brackets and rgb prefix optional
QRegExp rgbFormatRx( "^\\s*(?:rgb)?\\(?\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*,\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*,\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*\\)?\\s*;?\\s*$" );
if ( rgbFormatRx.indexIn( colorStr ) != -1 )
const thread_local QRegularExpression rgbFormatRx( "^\\s*(?:rgb)?\\(?\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*,\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*,\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*\\)?\\s*;?\\s*$" );
match = rgbFormatRx.match( colorStr );
if ( match.hasMatch() )
{
int r = rgbFormatRx.cap( 1 ).toInt();
int g = rgbFormatRx.cap( 2 ).toInt();
int b = rgbFormatRx.cap( 3 ).toInt();
int r = match.captured( 1 ).toInt();
int g = match.captured( 2 ).toInt();
int b = match.captured( 3 ).toInt();
parsedColor.setRgb( r, g, b );
if ( parsedColor.isValid() )
{
Expand All @@ -3722,8 +3726,8 @@ QColor QgsSymbolLayerUtils::parseColorWithAlpha( const QString &colorStr, bool &
}

//color in hsl(h,s,l) format, brackets optional
const QRegularExpression hslFormatRx( "^\\s*hsl\\(?\\s*(\\d+)\\s*,\\s*(\\d+)\\s*%\\s*,\\s*(\\d+)\\s*%\\s*\\)?\\s*;?\\s*$" );
QRegularExpressionMatch match = hslFormatRx.match( colorStr );
const thread_local QRegularExpression hslFormatRx( "^\\s*hsl\\(?\\s*(\\d+)\\s*,\\s*(\\d+)\\s*%\\s*,\\s*(\\d+)\\s*%\\s*\\)?\\s*;?\\s*$" );
match = hslFormatRx.match( colorStr );
if ( match.hasMatch() )
{
int h = match.captured( 1 ).toInt();
Expand All @@ -3738,12 +3742,13 @@ QColor QgsSymbolLayerUtils::parseColorWithAlpha( const QString &colorStr, bool &
}

//color in (r%,g%,b%) format, brackets and rgb prefix optional
QRegExp rgbPercentFormatRx( "^\\s*(?:rgb)?\\(?\\s*(100|0*\\d{1,2})\\s*%\\s*,\\s*(100|0*\\d{1,2})\\s*%\\s*,\\s*(100|0*\\d{1,2})\\s*%\\s*\\)?\\s*;?\\s*$" );
if ( rgbPercentFormatRx.indexIn( colorStr ) != -1 )
const thread_local QRegularExpression rgbPercentFormatRx( "^\\s*(?:rgb)?\\(?\\s*(100|0*\\d{1,2})\\s*%\\s*,\\s*(100|0*\\d{1,2})\\s*%\\s*,\\s*(100|0*\\d{1,2})\\s*%\\s*\\)?\\s*;?\\s*$" );
match = rgbPercentFormatRx.match( colorStr );
if ( match.hasMatch() )
{
int r = std::round( rgbPercentFormatRx.cap( 1 ).toDouble() * 2.55 );
int g = std::round( rgbPercentFormatRx.cap( 2 ).toDouble() * 2.55 );
int b = std::round( rgbPercentFormatRx.cap( 3 ).toDouble() * 2.55 );
int r = std::round( match.captured( 1 ).toDouble() * 2.55 );
int g = std::round( match.captured( 2 ).toDouble() * 2.55 );
int b = std::round( match.captured( 3 ).toDouble() * 2.55 );
parsedColor.setRgb( r, g, b );
if ( parsedColor.isValid() )
{
Expand All @@ -3753,13 +3758,14 @@ QColor QgsSymbolLayerUtils::parseColorWithAlpha( const QString &colorStr, bool &
}

//color in (r,g,b,a) format, brackets and rgba prefix optional
QRegExp rgbaFormatRx( "^\\s*(?:rgba)?\\(?\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*,\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*,\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*,\\s*(0|0?\\.\\d*|1(?:\\.0*)?)\\s*\\)?\\s*;?\\s*$" );
if ( rgbaFormatRx.indexIn( colorStr ) != -1 )
const thread_local QRegularExpression rgbaFormatRx( "^\\s*(?:rgba)?\\(?\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*,\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*,\\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\s*,\\s*(0|0?\\.\\d*|1(?:\\.0*)?)\\s*\\)?\\s*;?\\s*$" );
match = rgbaFormatRx.match( colorStr );
if ( match.hasMatch() )
{
int r = rgbaFormatRx.cap( 1 ).toInt();
int g = rgbaFormatRx.cap( 2 ).toInt();
int b = rgbaFormatRx.cap( 3 ).toInt();
int a = std::round( rgbaFormatRx.cap( 4 ).toDouble() * 255.0 );
int r = match.captured( 1 ).toInt();
int g = match.captured( 2 ).toInt();
int b = match.captured( 3 ).toInt();
int a = std::round( match.captured( 4 ).toDouble() * 255.0 );
parsedColor.setRgb( r, g, b, a );
if ( parsedColor.isValid() )
{
Expand All @@ -3769,13 +3775,14 @@ QColor QgsSymbolLayerUtils::parseColorWithAlpha( const QString &colorStr, bool &
}

//color in (r%,g%,b%,a) format, brackets and rgba prefix optional
QRegExp rgbaPercentFormatRx( "^\\s*(?:rgba)?\\(?\\s*(100|0*\\d{1,2})\\s*%\\s*,\\s*(100|0*\\d{1,2})\\s*%\\s*,\\s*(100|0*\\d{1,2})\\s*%\\s*,\\s*(0|0?\\.\\d*|1(?:\\.0*)?)\\s*\\)?\\s*;?\\s*$" );
if ( rgbaPercentFormatRx.indexIn( colorStr ) != -1 )
const thread_local QRegularExpression rgbaPercentFormatRx( "^\\s*(?:rgba)?\\(?\\s*(100|0*\\d{1,2})\\s*%\\s*,\\s*(100|0*\\d{1,2})\\s*%\\s*,\\s*(100|0*\\d{1,2})\\s*%\\s*,\\s*(0|0?\\.\\d*|1(?:\\.0*)?)\\s*\\)?\\s*;?\\s*$" );
match = rgbaPercentFormatRx.match( colorStr );
if ( match.hasMatch() )
{
int r = std::round( rgbaPercentFormatRx.cap( 1 ).toDouble() * 2.55 );
int g = std::round( rgbaPercentFormatRx.cap( 2 ).toDouble() * 2.55 );
int b = std::round( rgbaPercentFormatRx.cap( 3 ).toDouble() * 2.55 );
int a = std::round( rgbaPercentFormatRx.cap( 4 ).toDouble() * 255.0 );
int r = std::round( match.captured( 1 ).toDouble() * 2.55 );
int g = std::round( match.captured( 2 ).toDouble() * 2.55 );
int b = std::round( match.captured( 3 ).toDouble() * 2.55 );
int a = std::round( match.captured( 4 ).toDouble() * 255.0 );
parsedColor.setRgb( r, g, b, a );
if ( parsedColor.isValid() )
{
Expand All @@ -3785,7 +3792,7 @@ QColor QgsSymbolLayerUtils::parseColorWithAlpha( const QString &colorStr, bool &
}

//color in hsla(h,s%,l%,a) format, brackets optional
const QRegularExpression hslaPercentFormatRx( "^\\s*hsla\\(?\\s*(\\d+)\\s*,\\s*(\\d+)\\s*%\\s*,\\s*(\\d+)\\s*%\\s*,\\s*([\\d\\.]+)\\s*\\)?\\s*;?\\s*$" );
const thread_local QRegularExpression hslaPercentFormatRx( "^\\s*hsla\\(?\\s*(\\d+)\\s*,\\s*(\\d+)\\s*%\\s*,\\s*(\\d+)\\s*%\\s*,\\s*([\\d\\.]+)\\s*\\)?\\s*;?\\s*$" );
match = hslaPercentFormatRx.match( colorStr );
if ( match.hasMatch() )
{
Expand Down

0 comments on commit 89e126e

Please sign in to comment.