Skip to content

Commit

Permalink
Detect mixed newline characters
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashertzman committed Sep 19, 2023
1 parent 6f49a98 commit add4740
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions FileDiff/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum NewlineMode
Windows,
Unix,
Mac,
Mixed,
}

public enum Themes
Expand Down
40 changes: 22 additions & 18 deletions FileDiff/Unicode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace FileDiff;

Expand All @@ -13,9 +14,10 @@ public static FileEncoding GetEncoding(string path)
NewlineMode newlineMode = NewlineMode.Windows;
bool endOfFileNewline = false;

byte[] bytes = new byte[10000];
var bytes = new byte[10000];
int bytesRead = 0;

// Check if the file ends with a newline character
using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
bytesRead = fileStream.Read(bytes, 0, bytes.Length);
Expand Down Expand Up @@ -74,26 +76,28 @@ public static FileEncoding GetEncoding(string path)
}
}


// Check what newline characters are used
for (int i = 0; i < bytesRead; i++)
MatchCollection allNewLines = Regex.Matches(File.ReadAllText(path, encoding), "(\r\n|\r|\n)");

HashSet<string> distinctNewLines = new();

foreach (Match match in allNewLines)
{
if (bytes[i] == '\n')
{
newlineMode = NewlineMode.Unix;
break;
}
else if (bytes[i] == '\r')
distinctNewLines.Add(match.Value);
}

if (distinctNewLines.Count > 1)
{
newlineMode = NewlineMode.Mixed;
}
else if (distinctNewLines.Count == 1)
{
newlineMode = distinctNewLines.ToArray()[0] switch
{
int newLineBytes = encoding == Encoding.Unicode || encoding == Encoding.BigEndianUnicode ? 2 : 1;
if (i < bytesRead - newLineBytes && bytes[i + newLineBytes] == '\n')
{
newlineMode = NewlineMode.Windows;
break;
}
newlineMode = NewlineMode.Mac;
break;
}
"\n" => NewlineMode.Unix,
"\r" => NewlineMode.Mac,
_ => NewlineMode.Windows,
};
}

return new FileEncoding(encoding, bom, newlineMode, endOfFileNewline);
Expand Down

0 comments on commit add4740

Please sign in to comment.