fix line endings detection when opening text files - issue #48

This commit is contained in:
Vadim Lopatin 2015-02-03 10:55:52 +03:00
parent d2e37e38b8
commit 224b6d1fb5
2 changed files with 17 additions and 7 deletions

View File

@ -24,6 +24,8 @@ import dlangui.core.linestream;
import std.algorithm;
import std.stream;
// uncomment FileFormats debug symbol to dump file formats for loaded/saved files.
//debug = FileFormats;
immutable dchar EOL = '\n';
@ -1031,6 +1033,7 @@ class EditableContent {
// EOF
_format = lines.textFormat;
_undoBuffer.clear();
debug(FileFormats)Log.d("loaded file:", filename, " format detected:", _format);
notifyContentReplaced();
return true;
} catch (Exception e) {
@ -1060,6 +1063,7 @@ class EditableContent {
_format = format;
import dlangui.core.linestream;
try {
debug(FileFormats)Log.d("creating output stream, file=", filename, " format=", format);
OutputLineStream writer = new OutputLineStream(stream, filename, format);
scope(exit) { writer.close(); }
for (int i = 0; i < _lines.length; i++) {

View File

@ -94,6 +94,9 @@ struct TextFileFormat {
LineEnding lineEnding;
/// byte order mark character flag
bool bom;
string toString() {
return to!string(encoding) ~ " " ~ to!string(lineEnding) ~ (bom ? " bom" : "");
}
}
/// Text file writer which supports different text file formats
@ -266,11 +269,12 @@ class LineStream {
} else if (_crCount > _lfCount) {
le = LineEnding.CR;
} else if (_lfCount > _crCount) {
le = LineEnding.CR;
le = LineEnding.LF;
} else {
le = LineEnding.MIXED;
}
return TextFileFormat(_encoding, le, _bomDetected);
TextFileFormat res = TextFileFormat(_encoding, le, _bomDetected);
return res;
}
@ -409,7 +413,7 @@ class LineStream {
}
for (; p < charsLeft; p++) {
dchar ch = _textBuf[_textPos + p];
if (ch == 0x0D) {
if (ch == '\r') { // CR
lastchar = p;
if (p == charsLeft - 1) {
// need one more char to check if it's 0D0A or just 0D eol
@ -421,21 +425,23 @@ class LineStream {
charsLeft = _textLen - _textPos;
}
dchar ch2 = (p < charsLeft - 1) ? _textBuf[_textPos + p + 1] : 0;
if (ch2 == 0x0A) {
if (ch2 == '\n') { // LF
// CRLF
eol = p + 2;
_lfCount++;
_crCount++;
_crlfCount++;
} else {
// just CR
eol = p + 1;
_lfCount++;
_crCount++;
}
break;
} else if (ch == 0x0A || ch == 0x2028 || ch == 0x2029) {
} else if (ch == '\n' || ch == 0x2028 || ch == 0x2029) {
// single char eoln
lastchar = p;
eol = p + 1;
_crCount++;
_lfCount++;
break;
} else if (ch == 0 || ch == 0x001A) {
// eof