Improve binary detection for cases starting by chance by a BOM

The presence of a sequence of bytes resembling a BOM does not guarantee
that the data is text. We can in those cases use the detection provided
by Qt. If the codec matches the one selected, we can consider that text.

See issue #2197
This commit is contained in:
mgrojo
2020-06-21 23:18:09 +02:00
parent f4d78ad786
commit efb5b1a07a
+6 -3
View File
@@ -16,9 +16,12 @@ static const QByteArray bom4b("\xFF\xFE\x00\x00", 4);
bool isTextOnly(QByteArray data, const QString& encoding, bool quickTest)
{
// If the data starts with a Unicode BOM, we always assume it is text
if(startsWithBom(data))
return true;
// If the data starts with a Unicode BOM, we can use detection provided by QTextCodec.
if(startsWithBom(data)) {
QTextCodec *codec = encoding.isEmpty()? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForName(encoding.toUtf8());
QTextCodec *detectedCodec = QTextCodec::codecForUtfText(data, nullptr);
return detectedCodec == codec;
}
// Truncate to the first few bytes for quick testing
int testSize = quickTest? std::min(512, data.size()) : data.size();