optimize:move getHostBase method to utils

This commit is contained in:
xiaoyifang 2022-04-14 00:24:13 +08:00
parent 0759259b76
commit 753f07d3fd
2 changed files with 32 additions and 33 deletions

View file

@ -147,39 +147,6 @@ using std::string;
return size; return size;
} }
namespace
{
/// Uses some heuristics to chop off the first domain name from the host name,
/// but only if it's not too base. Returns the resulting host name.
QString getHostBase( QUrl const & url )
{
QString host = url.host();
QStringList domains = host.split( '.' );
int left = domains.size();
// Skip last <=3-letter domain name
if ( left && domains[ left - 1 ].size() <= 3 )
--left;
// Skip another <=3-letter domain name
if ( left && domains[ left - 1 ].size() <= 3 )
--left;
if ( left > 1 )
{
// We've got something like www.foobar.co.uk -- we can chop off the first
// domain
return host.mid( domains[ 0 ].size() + 1 );
}
else
return host;
}
}
QNetworkReply * ArticleNetworkAccessManager::createRequest( Operation op, QNetworkReply * ArticleNetworkAccessManager::createRequest( Operation op,
QNetworkRequest const & req, QNetworkRequest const & req,
QIODevice * outgoingData ) QIODevice * outgoingData )

View file

@ -148,4 +148,36 @@ inline QString getWordFromUrl( const QUrl & url )
} }
namespace
{
/// Uses some heuristics to chop off the first domain name from the host name,
/// but only if it's not too base. Returns the resulting host name.
inline QString getHostBase( QUrl const & url )
{
QString host = url.host();
QStringList domains = host.split( '.' );
int left = domains.size();
// Skip last <=3-letter domain name
if ( left && domains[ left - 1 ].size() <= 3 )
--left;
// Skip another <=3-letter domain name
if ( left && domains[ left - 1 ].size() <= 3 )
--left;
if ( left > 1 )
{
// We've got something like www.foobar.co.uk -- we can chop off the first
// domain
return host.mid( domains[ 0 ].size() + 1 );
}
else
return host;
}
}
#endif // UTILS_HH #endif // UTILS_HH