Skip to content
Merged
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
68 changes: 53 additions & 15 deletions src/ScintillaNext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,23 @@ static QFileDevice::FileError writeToDisk(const QByteArray &data, const QString
return file.error();
}

// Write actual data
if (file.write(data) == -1) {
qWarning("writeToDisk() failed writing data: %s", qPrintable(file.errorString()));
return file.error();
// TODO make this convert and write chunks rather than the entire thing
if (bom == ScintillaNext::BomType::Utf16LE) {
QStringEncoder encoder(QStringEncoder::Utf16LE);
const QString text = QString::fromUtf8(data);
const QByteArray encoded = encoder(text);

file.write(encoded);
}
else if (bom == ScintillaNext::BomType::Utf16BE) {
QStringEncoder encoder(QStringEncoder::Utf16BE);
const QString text = QString::fromUtf8(data);
const QByteArray encoded = encoder(text);

file.write(encoded);
}
else {
file.write(data);
}

return file.error();
Expand Down Expand Up @@ -681,25 +694,50 @@ bool ScintillaNext::readFromDisk(QFile &file)
// - determine space vs tabs
// - determine indentation size

QStringDecoder decoder;
int offset = 0;
if (first_read) {
first_read = false;

bomType = detectBom(chunk);

if (bomType != BomType::None) {
qDebug("BOM found");
offset = bomLength(bomType);

switch (bomType) {
case BomType::Utf16BE:
decoder = QStringDecoder(QStringDecoder::Utf16BE);
offset = 2;
break;

case BomType::Utf16LE:
decoder = QStringDecoder(QStringDecoder::Utf16LE);
offset = 2;
break;

case BomType::Utf8:
// No decoder needed if you're already passing UTF-8 to Scintilla.
offset = 3;
break;

default:
// Whatever your handling is for no BOM.
break;
}
}

if (bomType == BomType::Utf8) {
chunk.remove(0, bomLength(bomType));
}
QByteArrayView input(chunk.constData() + offset, chunk.size() - offset);

if (bomType == BomType::Utf16BE || bomType == BomType::Utf16LE) {
// Um...ignore this for now?
}
}
QString text = decoder(input);
QByteArray utf8 = text.toUtf8();

if (bomType == BomType::Utf16BE ||bomType == BomType::Utf16LE) {
QString text = decoder(input);
QByteArray utf8 = text.toUtf8();

appendText(chunk.size(), chunk.constData());
appendText(utf8.size(), utf8.constData());
} else {
// No conversion necessary.
appendText(input.size(), input.data());
}
} while (!file.atEnd() && status() == SC_STATUS_OK);

file.close();
Expand Down
Loading