opt: port away QRegExp in langcoder.cc

This commit is contained in:
shenleban tongying 2024-06-18 23:10:14 -04:00
parent 3a0880fa81
commit 5c9f67b09c
No known key found for this signature in database
8 changed files with 26 additions and 29 deletions

View file

@ -700,7 +700,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f
// if no languages found, try dictionary's name
if ( langs.first == 0 || langs.second == 0 ) {
langs = LangCoder::findLangIdPairFromStr( QString::fromStdString( dictionaryName ) );
langs = LangCoder::findLangIdPairFromName( QString::fromStdString( dictionaryName ) );
}
idxHeader.langFrom = langs.first;

View file

@ -1371,7 +1371,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f
// if no languages found, try dictionary's name
if ( langs.first == 0 || langs.second == 0 ) {
langs = LangCoder::findLangIdPairFromStr( QString::fromStdString( dictionaryName ) );
langs = LangCoder::findLangIdPairFromName( QString::fromStdString( dictionaryName ) );
}
idxHeader.langFrom = langs.first;
idxHeader.langTo = langs.second;

View file

@ -1427,7 +1427,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f
// if no languages found, try dictionary name
if ( langs.first == 0 || langs.second == 0 ) {
langs = LangCoder::findLangIdPairFromStr( parser.title() );
langs = LangCoder::findLangIdPairFromName( parser.title() );
}
idxHeader.langFrom = langs.first;

View file

@ -1904,10 +1904,10 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f
idxHeader.sameTypeSequenceSize = ifo.sametypesequence.size();
// read languages from dictioanry file name
auto langs = LangCoder::findLangIdPairFromStr( QString::fromStdString( dictFileName ) );
auto langs = LangCoder::findLangIdPairFromName( QString::fromStdString( dictFileName ) );
// if no languages found, try dictionary's name
if ( langs.first == 0 || langs.second == 0 ) {
langs = LangCoder::findLangIdPairFromStr( QString::fromStdString( ifo.bookname ) );
langs = LangCoder::findLangIdPairFromName( QString::fromStdString( ifo.bookname ) );
}
idxHeader.langFrom = langs.first;

View file

@ -7,14 +7,11 @@
#include <QFileInfo>
#include <QLocale>
#include <QRegularExpression>
#ifdef _MSC_VER
#include <stub_msvc.h>
#endif
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
#include <QtCore5Compat/QRegExp>
#endif
// Language codes
QMap< QString, GDLangCode > LangCoder::LANG_CODE_MAP = {
@ -285,28 +282,28 @@ quint32 LangCoder::guessId( const QString & lang )
return code2toInt( lstr.left( 2 ).toLatin1().data() );
}
std::pair< quint32, quint32 > LangCoder::findLangIdPairFromStr( QString const & name )
QPair< quint32, quint32 > LangCoder::findLangIdPairFromName( QString const & name )
{
QString nameFolded = "|" + name.toCaseFolded() + "|";
QRegExp reg( "[^a-z]([a-z]{2,3})-([a-z]{2,3})[^a-z]" );
reg.setMinimal( true );
int off = 0;
static QRegularExpression reg( "(?=([a-z]{2,3})-([a-z]{2,3}))", QRegularExpression::CaseInsensitiveOption );
while ( reg.indexIn( nameFolded, off ) >= 0 ) {
quint32 from = guessId( reg.cap( 1 ) );
quint32 to = guessId( reg.cap( 2 ) );
if ( from && to )
return QPair< quint32, quint32 >( from, to );
auto matches = reg.globalMatch( name );
while ( matches.hasNext() ) {
auto m = matches.next();
off += reg.matchedLength();
auto fromId = guessId( m.captured( 1 ).toLower() );
auto toId = guessId( m.captured( 2 ).toLower() );
if ( code2Exists( intToCode2( fromId ) ) && code2Exists( intToCode2( toId ) ) ) {
return { fromId, toId };
}
return QPair< quint32, quint32 >( 0, 0 );
}
return { 0, 0 };
}
static std::pair< quint32, quint32 > findLangIdPairFromPath( std::string const & p )
QPair< quint32, quint32 > LangCoder::findLangIdPairFromPath( std::string const & p )
{
return LangCoder::findLangIdPairFromStr( QFileInfo( QString::fromStdString( p ) ).fileName() );
return findLangIdPairFromName( QFileInfo( QString::fromStdString( p ) ).fileName() );
}
bool LangCoder::isLanguageRTL( quint32 _code )

View file

@ -40,8 +40,8 @@ public:
static quint32 findIdForLanguageCode3( std::string const & );
/// find id pairs like en-zh in dictioanry name
static std::pair< quint32, quint32 > findLangIdPairFromStr( QString const & );
static std::pair< quint32, quint32 > findLangIdPairFromPath( std::string const & );
static QPair< quint32, quint32 > findLangIdPairFromName( QString const & );
static QPair< quint32, quint32 > findLangIdPairFromPath( std::string const & );
static quint32 guessId( const QString & lang );

View file

@ -661,7 +661,7 @@ void DictGroupsWidget::addAutoGroups()
// Attempt to find language pair in dictionary name
const QPair< quint32, quint32 > ids =
LangCoder::findLangIdPairFromStr( QString::fromUtf8( dict->getName().c_str() ) );
LangCoder::findLangIdPairFromName( QString::fromUtf8( dict->getName().c_str() ) );
idFrom = ids.first;
idTo = ids.second;
}

View file

@ -33,7 +33,7 @@ bool dictLessThan( sptr< Dictionary::Class > const & dict1, sptr< Dictionary::Cl
int idFrom1 = dict1->getLangFrom();
int idTo1 = dict1->getLangTo();
if ( idFrom1 == 0 ) {
QPair< quint32, quint32 > ids = LangCoder::findLangIdPairFromStr( QString::fromUtf8( dict1->getName().c_str() ) );
QPair< quint32, quint32 > ids = LangCoder::findLangIdPairFromName( QString::fromUtf8( dict1->getName().c_str() ) );
idFrom1 = ids.first;
idTo1 = ids.second;
}
@ -41,7 +41,7 @@ bool dictLessThan( sptr< Dictionary::Class > const & dict1, sptr< Dictionary::Cl
int idFrom2 = dict2->getLangFrom();
int idTo2 = dict2->getLangTo();
if ( idFrom2 == 0 ) {
QPair< quint32, quint32 > ids = LangCoder::findLangIdPairFromStr( QString::fromUtf8( dict2->getName().c_str() ) );
QPair< quint32, quint32 > ids = LangCoder::findLangIdPairFromName( QString::fromUtf8( dict2->getName().c_str() ) );
idFrom2 = ids.first;
idTo2 = ids.second;
}