Skip to content

Commit

Permalink
Use QLatin1StringView instead of QLatin1String, minor optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
fo76utils committed Nov 3, 2024
1 parent 8f48df7 commit 78552e9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 73 deletions.
29 changes: 14 additions & 15 deletions src/model/basemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ const NifItem * BaseModel::getItemInternal( const NifItem * parent, const QStrin
return nullptr;
}

const NifItem * BaseModel::getItemInternal( const NifItem * parent, const QLatin1String & name, bool reportErrors ) const
const NifItem * BaseModel::getItemInternal( const NifItem * parent, const QLatin1StringView & name, bool reportErrors ) const
{
for ( auto item : parent->childIter() )
if ( item->hasName(name) && evalCondition(item) )
Expand All @@ -618,20 +618,18 @@ const NifItem * BaseModel::getItemInternal( const NifItem * parent, const QLatin
return nullptr;
}

const QString SLASH_QSTRING("\\");
const QString DOTS_QSTRING("..");
const QLatin1String SLASH_LATIN("\\");
const QLatin1String DOTS_LATIN("..");
static const QString DOTS_QSTRING("..");
static const QLatin1StringView DOTS_LATIN("..");

const NifItem * BaseModel::getItem( const NifItem * parent, const QString & name, bool reportErrors ) const
{
if ( !parent )
return nullptr;

int slashPos = name.indexOf( SLASH_QSTRING );
int slashPos = name.indexOf( QChar('\\') );
if ( slashPos > 0 ) {
QString left = name.left( slashPos );
QString right = name.right( name.length() - slashPos - SLASH_QSTRING.length() );
QString right = name.right( name.length() - slashPos - 1 );

const NifItem * pp = ( left == DOTS_QSTRING ) ? parent->parent() : getItemInternal( parent, left, reportErrors );
return getItem( pp, right, reportErrors );
Expand All @@ -640,15 +638,16 @@ const NifItem * BaseModel::getItem( const NifItem * parent, const QString & name
return getItemInternal( parent, name, reportErrors );
}

const NifItem * BaseModel::getItem( const NifItem * parent, const QLatin1String & name, bool reportErrors ) const
const NifItem * BaseModel::getItem( const NifItem * parent, const QLatin1StringView & name, bool reportErrors ) const
{
if ( !parent )
return nullptr;

int slashPos = name.indexOf( SLASH_LATIN );
if ( slashPos > 0 ) {
QLatin1String left = name.left( slashPos );
QLatin1String right = name.right( name.size() - slashPos - SLASH_LATIN.size() );
size_t nameLen = size_t( name.length() );
size_t slashPos = std::string_view( name.data(), nameLen ).find( '\\' );
if ( slashPos != std::string_view::npos && slashPos > 0 ) {
QLatin1StringView left = name.left( slashPos );
QLatin1StringView right = name.right( nameLen - slashPos - 1 );

const NifItem * pp = ( left == DOTS_LATIN ) ? parent->parent() : getItemInternal( parent, left, reportErrors );
return getItem( pp, right, reportErrors );
Expand Down Expand Up @@ -704,7 +703,7 @@ const NifItem * BaseModel::getItem( const QModelIndex & index, bool reportErrors
/*
* Uses implicit load order
*/
const NifItem * BaseModel::getItemX( const NifItem * item, const QLatin1String & name ) const
const NifItem * BaseModel::getItemX( const NifItem * item, const QLatin1StringView & name ) const
{
while ( item ) {
const NifItem * parent = item->parent();
Expand All @@ -723,7 +722,7 @@ const NifItem * BaseModel::getItemX( const NifItem * item, const QLatin1String &
return nullptr;
}

const NifItem * BaseModel::findItemX( const NifItem * parent, const QLatin1String & name ) const
const NifItem * BaseModel::findItemX( const NifItem * parent, const QLatin1StringView & name ) const
{
while ( parent ) {
const NifItem * c = getItem( parent, name, false );
Expand Down Expand Up @@ -811,7 +810,7 @@ QString BaseModel::itemRepr( const NifItem * item ) const
if ( parent->isArray() )
subres = QString(" [%1]").arg( item->row() );
else
subres = SLASH_QSTRING + item->name();
subres = QString( QChar('\\') ) + item->name();
result = subres + result;
item = parent;
}
Expand Down
Loading

0 comments on commit 78552e9

Please sign in to comment.