fix: dsl ann file codec detection

This commit is contained in:
xiaoyifang 2024-11-22 11:32:50 +08:00
parent 0f3336977f
commit 3f49d92011
4 changed files with 17 additions and 5 deletions

View file

@ -48,7 +48,7 @@ QString unescapeAmps( QString const & str )
return result;
}
QTextCodec::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstCharacter = 0 )
QTextCodec::Encoding detectCodec( QByteArray & ba, char16_t expectedFirstCharacter = 0 )
{
QStringConverter::Encoding detectedEncoding = QStringConverter::encodingForData( data, expectedFirstCharacter );
// mapping the encoding
@ -58,6 +58,17 @@ QTextCodec::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstChar
// default utf8
return QTextCodec::Utf8;
}
QStringConverter::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstCharacter = 0 )
{
QStringConverter::Encoding detectedEncoding = QStringConverter::encodingForData( data, expectedFirstCharacter );
// mapping the encoding
if ( detectedEncoding.has_value() ) {
return detectedEncoding.value();
}
// default utf8
return QStringConverter::Utf8;
}
} // namespace Utils
QString Utils::Path::combine( const QString & path1, const QString & path2 )

View file

@ -371,6 +371,7 @@ const QMap< QStringConverter::Encoding, QTextCodec::Encoding > encodingMap = {
};
}
QTextCodec::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstCharacter = 0 );
QTextCodec::Encoding detectCodec( QByteArray & ba, char16_t expectedFirstCharacter = 0 );
QStringConverter::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstCharacter = 0 );
} // namespace Utils

View file

@ -1042,8 +1042,8 @@ QString const & DslDictionary::getDescription()
QString data, str;
str = annStream.readLine();
auto codec = Utils::detectEncoding( str );
annStream.setCodec( codec );
auto encoding = Utils::detectEncoding( str );
annStream.setEncoding( encoding );
if ( str.left( 10 ).compare( "#LANGUAGE " ) != 0 ) {
annStream.seek( 0 );

View file

@ -873,7 +873,7 @@ DslScanner::DslScanner( string const & fileName ):
// Note that .dsl format always starts with "#NAME"
codec = Utils::detectEncoding( { firstBytes, firstBytesSize }, '#' );
codec = Utils::detectCodec( { firstBytes, firstBytesSize }, '#' );
qDebug() << "DSL encoding ->" << codec->name();