diff --git a/src/dict/dictdfiles.cc b/src/dict/dictdfiles.cc index 510bce44..6f71130d 100644 --- a/src/dict/dictdfiles.cc +++ b/src/dict/dictdfiles.cc @@ -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; diff --git a/src/dict/gls.cc b/src/dict/gls.cc index 598d02d8..8f20a915 100644 --- a/src/dict/gls.cc +++ b/src/dict/gls.cc @@ -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; diff --git a/src/dict/mdx.cc b/src/dict/mdx.cc index 2825b6a4..62f7389d 100644 --- a/src/dict/mdx.cc +++ b/src/dict/mdx.cc @@ -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; diff --git a/src/dict/stardict.cc b/src/dict/stardict.cc index 48f0e280..bd6d41ae 100644 --- a/src/dict/stardict.cc +++ b/src/dict/stardict.cc @@ -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; diff --git a/src/langcoder.cc b/src/langcoder.cc index 4a0834fc..2b6c04a5 100644 --- a/src/langcoder.cc +++ b/src/langcoder.cc @@ -7,14 +7,11 @@ #include #include +#include #ifdef _MSC_VER #include #endif -#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) ) - #include -#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 ) diff --git a/src/langcoder.hh b/src/langcoder.hh index cbe564d3..56084034 100644 --- a/src/langcoder.hh +++ b/src/langcoder.hh @@ -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 ); diff --git a/src/ui/groups_widgets.cc b/src/ui/groups_widgets.cc index eb19ed5c..c8df7c70 100644 --- a/src/ui/groups_widgets.cc +++ b/src/ui/groups_widgets.cc @@ -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; } diff --git a/src/ui/orderandprops.cc b/src/ui/orderandprops.cc index d7de502b..fa389ac1 100644 --- a/src/ui/orderandprops.cc +++ b/src/ui/orderandprops.cc @@ -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; }