Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utf 16 fixes #166

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ bool shellDeleteQuiet(const QString& fileName, QWidget* dialog)
return true;
}

QString readFileText(const QString& fileName, QString* encoding)
QString readFileText(const QString& fileName, QString* encoding, bool* hadBOM)
{

QFile textFile(fileName);
Expand All @@ -856,26 +856,37 @@ QString readFileText(const QString& fileName, QString* encoding)
}

QByteArray buffer = textFile.readAll();
return decodeTextData(buffer, encoding);
return decodeTextData(buffer, encoding, hadBOM);
}

QString decodeTextData(const QByteArray& fileData, QString* encoding)
QString decodeTextData(const QByteArray& fileData, QString* encoding, bool* hadBOM)
{
QStringConverter::Encoding codec = QStringConverter::Encoding::Utf8;
QStringEncoder encoder(codec);
QStringDecoder decoder(codec);
QStringDecoder decoder(codec, QStringConverter::Flag::ConvertInitialBom);
QString text = decoder.decode(fileData);

// embedded nulls probably mean it was UTF-16 - they're rare/illegal in text files
bool hasEmbeddedNulls = false;
for (const auto& character : text) {
if (character.isNull()) {
hasEmbeddedNulls = true;
break;
}
}

// check reverse conversion. If this was unicode text there can't be data loss
// this assumes QString doesn't normalize the data in any way so this is a bit unsafe
if (encoder.encode(text) != fileData) {
if (hasEmbeddedNulls || encoder.encode(text) != fileData) {
log::debug("conversion failed assuming local encoding");
auto codecSearch = QStringConverter::encodingForData(fileData);
if (codecSearch.has_value()) {
codec = codecSearch.value();
decoder = QStringDecoder(codec);
decoder = QStringDecoder(codec, QStringConverter::Flag::ConvertInitialBom);
} else {
decoder = QStringDecoder(QStringConverter::Encoding::System);
// encodingForData doesn't handle UTF-16 without BOM
decoder = QStringDecoder(hasEmbeddedNulls ? QStringConverter::Encoding::Utf16
: QStringConverter::Encoding::System);
}
text = decoder.decode(fileData);
}
Expand All @@ -884,6 +895,16 @@ QString decodeTextData(const QByteArray& fileData, QString* encoding)
*encoding = QStringConverter::nameForEncoding(codec);
}

if (!text.isEmpty() && text.startsWith(QChar::ByteOrderMark)) {
text.remove(0, 1);

if (hadBOM != nullptr) {
*hadBOM = true;
}
} else if (hadBOM != nullptr) {
*hadBOM = false;
}

return text;
}

Expand Down
5 changes: 3 additions & 2 deletions src/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ QDLLEXPORT QString getStartMenuDirectory();
*the encoding used
* @return the textual content of the file or an empty string if the file doesn't exist
**/
QDLLEXPORT QString readFileText(const QString& fileName, QString* encoding = nullptr);
QDLLEXPORT QString readFileText(const QString& fileName, QString* encoding = nullptr,
bool* hadBOM = nullptr);

/**
* @brief decode raw text data. This tries to guess the encoding used in the file
Expand All @@ -462,7 +463,7 @@ QDLLEXPORT QString readFileText(const QString& fileName, QString* encoding = nul
* @return the textual content of the file or an empty string if the file doesn't exist
**/
QDLLEXPORT QString decodeTextData(const QByteArray& fileData,
QString* encoding = nullptr);
QString* encoding = nullptr, bool* hadBOM = nullptr);

/**
* @brief delete files matching a pattern
Expand Down
Loading