Add LineEndings module.
This commit is contained in:
parent
97e01ab714
commit
1b5a2d92af
61
src/jes/core/lineendings.d
Normal file
61
src/jes/core/lineendings.d
Normal file
@ -0,0 +1,61 @@
|
||||
module jes.core.lineendings;
|
||||
|
||||
struct LineEndings
|
||||
{
|
||||
enum : ubyte
|
||||
{
|
||||
LF,
|
||||
CRLF,
|
||||
}
|
||||
|
||||
ubyte type;
|
||||
alias type this;
|
||||
|
||||
this(ubyte type)
|
||||
{
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
static LineEndings detect_line_endings(const ubyte * data, size_t n)
|
||||
{
|
||||
size_t lf_count = 0u;
|
||||
for (size_t i = 0u; i < n; i++)
|
||||
{
|
||||
if (data[i] == '\n')
|
||||
{
|
||||
lf_count++;
|
||||
/* Every LF must be preceded by a CR for the content to have
|
||||
* CRLF line endings. */
|
||||
if ((i == 0u) || (data[i - 1u] != '\r'))
|
||||
{
|
||||
return LineEndings(LF);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (lf_count > 0u)
|
||||
{
|
||||
return LineEndings(CRLF);
|
||||
}
|
||||
return LineEndings(LF);
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
import std.stdio;
|
||||
import std.file;
|
||||
const(ubyte)[] dat;
|
||||
LineEndings le;
|
||||
|
||||
dat = cast(const(ubyte)[])std.file.read("test/files/line_endings/crlf_format.txt");
|
||||
le = LineEndings.detect_line_endings(dat.ptr, dat.length);
|
||||
assert(le == LineEndings.CRLF);
|
||||
|
||||
dat = cast(const(ubyte)[])std.file.read("test/files/empty.txt");
|
||||
le = LineEndings.detect_line_endings(dat.ptr, 0u);
|
||||
assert(le == LineEndings.LF);
|
||||
|
||||
dat = cast(const(ubyte)[])std.file.read("test/files/line_endings/lf_format.txt");
|
||||
le = LineEndings.detect_line_endings(dat.ptr, dat.length);
|
||||
assert(le == LineEndings.LF);
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
Hello.
This file is in CR line ending format.
|
Loading…
x
Reference in New Issue
Block a user