mirror of https://github.com/buggins/dlangui.git
fix line endings detection when opening text files - issue #48
This commit is contained in:
parent
d2e37e38b8
commit
224b6d1fb5
|
@ -24,6 +24,8 @@ import dlangui.core.linestream;
|
||||||
import std.algorithm;
|
import std.algorithm;
|
||||||
import std.stream;
|
import std.stream;
|
||||||
|
|
||||||
|
// uncomment FileFormats debug symbol to dump file formats for loaded/saved files.
|
||||||
|
//debug = FileFormats;
|
||||||
|
|
||||||
immutable dchar EOL = '\n';
|
immutable dchar EOL = '\n';
|
||||||
|
|
||||||
|
@ -1031,6 +1033,7 @@ class EditableContent {
|
||||||
// EOF
|
// EOF
|
||||||
_format = lines.textFormat;
|
_format = lines.textFormat;
|
||||||
_undoBuffer.clear();
|
_undoBuffer.clear();
|
||||||
|
debug(FileFormats)Log.d("loaded file:", filename, " format detected:", _format);
|
||||||
notifyContentReplaced();
|
notifyContentReplaced();
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -1060,6 +1063,7 @@ class EditableContent {
|
||||||
_format = format;
|
_format = format;
|
||||||
import dlangui.core.linestream;
|
import dlangui.core.linestream;
|
||||||
try {
|
try {
|
||||||
|
debug(FileFormats)Log.d("creating output stream, file=", filename, " format=", format);
|
||||||
OutputLineStream writer = new OutputLineStream(stream, filename, format);
|
OutputLineStream writer = new OutputLineStream(stream, filename, format);
|
||||||
scope(exit) { writer.close(); }
|
scope(exit) { writer.close(); }
|
||||||
for (int i = 0; i < _lines.length; i++) {
|
for (int i = 0; i < _lines.length; i++) {
|
||||||
|
|
|
@ -94,6 +94,9 @@ struct TextFileFormat {
|
||||||
LineEnding lineEnding;
|
LineEnding lineEnding;
|
||||||
/// byte order mark character flag
|
/// byte order mark character flag
|
||||||
bool bom;
|
bool bom;
|
||||||
|
string toString() {
|
||||||
|
return to!string(encoding) ~ " " ~ to!string(lineEnding) ~ (bom ? " bom" : "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Text file writer which supports different text file formats
|
/// Text file writer which supports different text file formats
|
||||||
|
@ -266,11 +269,12 @@ class LineStream {
|
||||||
} else if (_crCount > _lfCount) {
|
} else if (_crCount > _lfCount) {
|
||||||
le = LineEnding.CR;
|
le = LineEnding.CR;
|
||||||
} else if (_lfCount > _crCount) {
|
} else if (_lfCount > _crCount) {
|
||||||
le = LineEnding.CR;
|
le = LineEnding.LF;
|
||||||
} else {
|
} else {
|
||||||
le = LineEnding.MIXED;
|
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++) {
|
for (; p < charsLeft; p++) {
|
||||||
dchar ch = _textBuf[_textPos + p];
|
dchar ch = _textBuf[_textPos + p];
|
||||||
if (ch == 0x0D) {
|
if (ch == '\r') { // CR
|
||||||
lastchar = p;
|
lastchar = p;
|
||||||
if (p == charsLeft - 1) {
|
if (p == charsLeft - 1) {
|
||||||
// need one more char to check if it's 0D0A or just 0D eol
|
// need one more char to check if it's 0D0A or just 0D eol
|
||||||
|
@ -421,21 +425,23 @@ class LineStream {
|
||||||
charsLeft = _textLen - _textPos;
|
charsLeft = _textLen - _textPos;
|
||||||
}
|
}
|
||||||
dchar ch2 = (p < charsLeft - 1) ? _textBuf[_textPos + p + 1] : 0;
|
dchar ch2 = (p < charsLeft - 1) ? _textBuf[_textPos + p + 1] : 0;
|
||||||
if (ch2 == 0x0A) {
|
if (ch2 == '\n') { // LF
|
||||||
|
// CRLF
|
||||||
eol = p + 2;
|
eol = p + 2;
|
||||||
_lfCount++;
|
_lfCount++;
|
||||||
_crCount++;
|
_crCount++;
|
||||||
_crlfCount++;
|
_crlfCount++;
|
||||||
} else {
|
} else {
|
||||||
|
// just CR
|
||||||
eol = p + 1;
|
eol = p + 1;
|
||||||
_lfCount++;
|
_crCount++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
} else if (ch == 0x0A || ch == 0x2028 || ch == 0x2029) {
|
} else if (ch == '\n' || ch == 0x2028 || ch == 0x2029) {
|
||||||
// single char eoln
|
// single char eoln
|
||||||
lastchar = p;
|
lastchar = p;
|
||||||
eol = p + 1;
|
eol = p + 1;
|
||||||
_crCount++;
|
_lfCount++;
|
||||||
break;
|
break;
|
||||||
} else if (ch == 0 || ch == 0x001A) {
|
} else if (ch == 0 || ch == 0x001A) {
|
||||||
// eof
|
// eof
|
||||||
|
|
Loading…
Reference in New Issue