Skip to content

Commit

Permalink
Fix clazy "qstring-ref" warnings
Browse files Browse the repository at this point in the history
From the clazy docs:

Finds places where QString::fooRef() should be used instead of
QString::foo(), to avoid temporary heap allocations

eg

str.mid(5).toInt(ok) // BAD

str.midRef(5).toInt(ok) // GOOD
  • Loading branch information
nyalldawson committed Oct 22, 2016
1 parent a51bebd commit ef51107
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/app/main.cpp
Expand Up @@ -1126,7 +1126,7 @@ int main( int argc, char *argv[] )
break;
}

coords[i] = QString( myInitialExtent.mid( posOld, pos - posOld ) ).toDouble( &ok );
coords[i] = myInitialExtent.midRef( posOld, pos - posOld ).toDouble( &ok );
if ( !ok )
break;

Expand All @@ -1135,7 +1135,7 @@ int main( int argc, char *argv[] )

// parse last coordinate
if ( ok )
coords[3] = QString( myInitialExtent.mid( posOld ) ).toDouble( &ok );
coords[3] = myInitialExtent.midRef( posOld ).toDouble( &ok );

if ( !ok )
{
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -1908,14 +1908,14 @@ void QgsProjectProperties::populateEllipsoidList()
// Crash if no column?
para1 = ( const char * )sqlite3_column_text( myPreparedStatement, 2 );
para2 = ( const char * )sqlite3_column_text( myPreparedStatement, 3 );
myItem.semiMajor = para1.mid( 2 ).toDouble();
myItem.semiMajor = para1.midRef( 2 ).toDouble();
if ( para2.left( 2 ) == "b=" )
{
myItem.semiMinor = para2.mid( 2 ).toDouble();
myItem.semiMinor = para2.midRef( 2 ).toDouble();
}
else if ( para2.left( 3 ) == "rf=" )
{
double invFlattening = para2.mid( 3 ).toDouble();
double invFlattening = para2.midRef( 3 ).toDouble();
if ( invFlattening != 0.0 )
{
myItem.semiMinor = myItem.semiMajor - ( myItem.semiMajor / invFlattening );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgscolorramp.cpp
Expand Up @@ -72,7 +72,7 @@ QgsColorRamp* QgsGradientColorRamp::create( const QgsStringMap& props )
continue;

QColor c = QgsSymbolLayerUtils::decodeColor( stop.mid( i + 1 ) );
stops.append( QgsGradientStop( stop.left( i ).toDouble(), c ) );
stops.append( QgsGradientStop( stop.leftRef( i ).toDouble(), c ) );
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -500,7 +500,7 @@ bool QgsCoordinateReferenceSystem::hasAxisInverted() const
{
OGRSpatialReferenceH crs = OSRNewSpatialReference( nullptr );

if ( OSRImportFromEPSGA( crs, d->mAuthId.mid( 5 ).toInt() ) == OGRERR_NONE )
if ( OSRImportFromEPSGA( crs, d->mAuthId.midRef( 5 ).toInt() ) == OGRERR_NONE )
{
OSRGetAxis( crs, OSRIsGeographic( crs ) ? "GEOGCS" : "PROJCS", 0, &orientation );
}
Expand Down Expand Up @@ -1750,7 +1750,7 @@ bool QgsCoordinateReferenceSystem::loadWkts( QHash<int, QString> &wkts, const ch
return false;

bool ok;
int epsg = line.left( pos ).toInt( &ok );
int epsg = line.leftRef( pos ).toInt( &ok );
if ( !ok )
return false;

Expand Down Expand Up @@ -1791,7 +1791,7 @@ bool QgsCoordinateReferenceSystem::loadIds( QHash<int, QString> &wkts )
continue;

bool ok;
int epsg = line.left( pos ).toInt( &ok );
int epsg = line.leftRef( pos ).toInt( &ok );
if ( !ok )
continue;

Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsdistancearea.cpp
Expand Up @@ -191,7 +191,7 @@ bool QgsDistanceArea::setEllipsoid( const QString& ellipsoid )

// get major semiaxis
if ( radius.left( 2 ) == "a=" )
mSemiMajor = radius.mid( 2 ).toDouble();
mSemiMajor = radius.midRef( 2 ).toDouble();
else
{
QgsDebugMsg( QString( "setEllipsoid: wrong format of radius field: '%1'" ).arg( radius ) );
Expand All @@ -203,12 +203,12 @@ bool QgsDistanceArea::setEllipsoid( const QString& ellipsoid )
// second one must be computed using formula: invf = a/(a-b)
if ( parameter2.left( 2 ) == "b=" )
{
mSemiMinor = parameter2.mid( 2 ).toDouble();
mSemiMinor = parameter2.midRef( 2 ).toDouble();
mInvFlattening = mSemiMajor / ( mSemiMajor - mSemiMinor );
}
else if ( parameter2.left( 3 ) == "rf=" )
{
mInvFlattening = parameter2.mid( 3 ).toDouble();
mInvFlattening = parameter2.midRef( 3 ).toDouble();
mSemiMinor = mSemiMajor - ( mSemiMajor / mInvFlattening );
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsogcutils.cpp
Expand Up @@ -2910,7 +2910,7 @@ QDomElement QgsOgcUtilsSQLStatementToFilter::toOgcFilter( const QgsSQLStatement:
static QString mapBinarySpatialToOgc( const QString& name )
{
QString nameCompare( name );
if ( name.size() > 3 && name.mid( 0, 3 ).compare( "ST_", Qt::CaseInsensitive ) == 0 )
if ( name.size() > 3 && name.midRef( 0, 3 ).compare( "ST_", Qt::CaseInsensitive ) == 0 )
nameCompare = name.mid( 3 );
QStringList spatialOps;
spatialOps << "BBOX" << "Intersects" << "Contains" << "Crosses" << "Equals"
Expand All @@ -2926,7 +2926,7 @@ static QString mapBinarySpatialToOgc( const QString& name )
static QString mapTernarySpatialToOgc( const QString& name )
{
QString nameCompare( name );
if ( name.size() > 3 && name.mid( 0, 3 ).compare( "ST_", Qt::CaseInsensitive ) == 0 )
if ( name.size() > 3 && name.midRef( 0, 3 ).compare( "ST_", Qt::CaseInsensitive ) == 0 )
nameCompare = name.mid( 3 );
if ( nameCompare.compare( "DWithin", Qt::CaseInsensitive ) == 0 )
return "DWithin";
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/georeferencer/qgsgeorefvalidators.cpp
Expand Up @@ -57,7 +57,7 @@ QValidator::State QgsDMSAndDDValidator::validate( QString &input, int &pos ) con
rx.setPattern( "-?\\d{1,3}\\s60" );
if ( rx.exactMatch( input ) )
{
int in = input.left( input.indexOf( ' ' ) ).toInt();
int in = input.leftRef( input.indexOf( ' ' ) ).toInt();
int grad = input.startsWith( '-' ) ? in - 1 : in + 1;
if ( grad <= 180 )
input = QString::number( grad );
Expand Down
2 changes: 1 addition & 1 deletion src/providers/grass/qgsgrassprovider.cpp
Expand Up @@ -525,7 +525,7 @@ int QgsGrassProvider::grassLayer( QString name )
return -1;
}

return name.left( pos ).toInt();
return name.leftRef( pos ).toInt();
}

int QgsGrassProvider::grassLayerType( QString name )
Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -3852,7 +3852,7 @@ static QString getNextString( const QString& txt, int& i, const QString& sep )
}
i += stringRe.cap( 1 ).length() + 2;
jumpSpace( txt, i );
if ( !txt.mid( i ).startsWith( sep ) && i < txt.length() )
if ( !txt.midRef( i ).startsWith( sep ) && i < txt.length() )
{
QgsLogger::warning( "Cannot find separator: " + txt.mid( i ) );
return QString::null;
Expand Down
4 changes: 2 additions & 2 deletions tests/bench/main.cpp
Expand Up @@ -557,7 +557,7 @@ int main( int argc, char *argv[] )
break;
}

coords[i] = QString( myInitialExtent.mid( posOld, pos - posOld ) ).toDouble( &ok );
coords[i] = myInitialExtent.midRef( posOld, pos - posOld ).toDouble( &ok );
if ( !ok )
break;

Expand All @@ -566,7 +566,7 @@ int main( int argc, char *argv[] )

// parse last coordinate
if ( ok )
coords[3] = QString( myInitialExtent.mid( posOld ) ).toDouble( &ok );
coords[3] = myInitialExtent.midRef( posOld ).toDouble( &ok );

if ( !ok )
{
Expand Down

0 comments on commit ef51107

Please sign in to comment.