forked from qt-creator/qt-creator
Assume UTF-8 in TextFileFormat::readFileUTF8 as fallback
If the contents is not detected as UTF-8/16/32 (TextFileFormat::detect) and decoding with the given defaultCodec fails, still assume UTF-8. This affects reading in files for code model indexing. Better read in the file with the wrong encoding than not reading it at all. Change-Id: Ibc10b7268ecbed4e7fd50336182f6e71c7d93f5b Reviewed-by: Pawel Faron <pawelfaron87@wp.pl> Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
@@ -289,18 +289,13 @@ TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const QString &fileName,
|
|||||||
Utils::TextFileFormat format = Utils::TextFileFormat::detect(data);
|
Utils::TextFileFormat format = Utils::TextFileFormat::detect(data);
|
||||||
if (!format.codec)
|
if (!format.codec)
|
||||||
format.codec = defaultCodec ? defaultCodec : QTextCodec::codecForLocale();
|
format.codec = defaultCodec ? defaultCodec : QTextCodec::codecForLocale();
|
||||||
if (format.codec->name() == "UTF-8") {
|
QString target;
|
||||||
|
if (format.codec->name() == "UTF-8" || !format.decode(data, &target)) {
|
||||||
if (format.hasUtf8Bom)
|
if (format.hasUtf8Bom)
|
||||||
data.remove(0, 3);
|
data.remove(0, 3);
|
||||||
*plainText = data;
|
*plainText = data;
|
||||||
return Utils::TextFileFormat::ReadSuccess;
|
return Utils::TextFileFormat::ReadSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString target;
|
|
||||||
if (!format.decode(data, &target)) {
|
|
||||||
*errorString = QCoreApplication::translate("Utils::TextFileFormat", "An encoding error was encountered.");
|
|
||||||
return Utils::TextFileFormat::ReadEncodingError;
|
|
||||||
}
|
|
||||||
*plainText = target.toUtf8();
|
*plainText = target.toUtf8();
|
||||||
return Utils::TextFileFormat::ReadSuccess;
|
return Utils::TextFileFormat::ReadSuccess;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user