Skip to content
This repository was archived by the owner on Nov 15, 2021. It is now read-only.
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
82 changes: 37 additions & 45 deletions main/OpenCover.Framework/Utility/CodeCoverageStringTextSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,62 +96,54 @@ public CodeCoverageStringTextSource(string source, string filePath)

private LineInfo[] InitLines ()
{
var lineInfoList = new List<LineInfo>();
int offset = 0;
int counter = 0;
bool newLine = false;
bool cr = false;
bool lf = false;
const ushort carriageReturn = 0xD;
const ushort lineFeed = 0xA;
LineInfo line;
var lineInfoList = new List<LineInfo>();
foreach (var ch in _textSource) {
switch ((ushort)ch) {
case carriageReturn:
if (lf || cr) {
lf = false;
newLine = true; // cr after cr|lf
} else {
cr = true; // cr found
}
break;
case lineFeed:
if (lf) {
newLine = true; // lf after lf
} else {
lf = true; // lf found
}
break;
default:
if (cr || lf) {
cr = false;
lf = false;
newLine = true; // any non-line-end char after any line-end
}
break;
}
if (newLine) { // newLine detected - add line
newLine = false;
line = new LineInfo
{
Offset = offset,
Length = counter - offset
};
lineInfoList.Add(line);
if (NextChar(ch)) { // newLine detected - add line
lineInfoList.Add(new LineInfo { Offset = offset, Length = counter - offset });
offset = counter;
}
++counter;
}
// Add last line
line = new LineInfo
{
Offset = offset,
Length = counter - offset
};
lineInfoList.Add(line);
lineInfoList.Add(new LineInfo { Offset = offset, Length = counter - offset });
return lineInfoList.ToArray();
}

private const ushort carriageReturn = 0xD;
private const ushort lineFeed = 0xA;

private bool cr = false;
private bool lf = false;

private bool NextChar(ushort ch)
{
switch (ch) {
case carriageReturn:
if (lf || cr) {
lf = false; // cr after cr|lf
return true;
}
cr = true; // cr found
break;
case lineFeed:
if (lf) { // lf after lf
return true;
}
lf = true; // lf found
break;
default:
if (cr || lf) { // any non-line-end char after any line-end
cr = false;
lf = false;
return true;
}
break;
}
return false;
}

/// <summary>Return text/source using SequencePoint line/col info
/// </summary>
/// <param name="sp"></param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ public void GetSource()
Assert.True (source.FileTime == DateTime.MinValue);

// arrange
var timeReference = DateTime.Now;
System.IO.File.WriteAllLines(cSharpFileName, lines);
// act on existing file
source = CodeCoverageStringTextSource.GetSource(cSharpFileName);
Expand All @@ -400,11 +401,16 @@ public void GetSource()
Assert.True (source.FilePath == cSharpFileName);
Assert.True (source.FileFound);
Assert.True (source.FileTime == System.IO.File.GetLastWriteTime (cSharpFileName));
Assert.False (source.IsChanged (source.FileTime));
Assert.False (source.IsChanged (DateTime.MinValue));
Assert.False (source.IsChanged (DateTime.Now));
Assert.True (source.IsChanged (timeReference));

// destroy temp file
System.IO.File.Delete(cSharpFileName);

// arrange
timeReference = DateTime.Now;
System.IO.File.WriteAllLines(vBasicFileName, lines);
// act on existing file
source = CodeCoverageStringTextSource.GetSource(vBasicFileName);
Expand All @@ -415,6 +421,10 @@ public void GetSource()
Assert.True (source.FilePath == vBasicFileName);
Assert.True (source.FileFound);
Assert.True (source.FileTime == System.IO.File.GetLastWriteTime (vBasicFileName));
Assert.False (source.IsChanged (source.FileTime));
Assert.False (source.IsChanged (DateTime.MinValue));
Assert.False (source.IsChanged (DateTime.Now));
Assert.True (source.IsChanged (timeReference));

// destroy temp file
System.IO.File.Delete(vBasicFileName);
Expand Down