Skip to content

Commit 0aef5cb

Browse files
committed
I'm was able to get ASCII to load larger files. Tested up to 15MB. However, 20MB Binary still have issues.
1 parent 30cf9a7 commit 0aef5cb

9 files changed

Lines changed: 200 additions & 110 deletions

File tree

Chizl.FileCompare.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
<Company>Chizl.com</Company>
1515
<Copyright>© 2025 chizl.com</Copyright>
1616
<PackageTags>file-compare;library;in-console;on-desktop;netstandard;core;framework</PackageTags>
17-
<FileVersion>5.09.18.1019</FileVersion>
18-
<Version>5.09.18.0</Version>
17+
<FileVersion>5.09.21.1261</FileVersion>
18+
<Version>5.09.21.0</Version>
1919
<AssemblyVersion>5.09.0.0</AssemblyVersion>
2020
</PropertyGroup>
2121

Chizl.FileComparer/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// Revision
3131
//
3232
[assembly: AssemblyVersion("5.9.0.0")]
33-
[assembly: AssemblyFileVersion("5.9.21.663")]
33+
[assembly: AssemblyFileVersion("5.9.21.1261")]
3434
[assembly: AssemblyInformationalVersion("5.9.21.0")]
3535
[assembly: AssemblyKeyFileAttribute("")]
3636
[assembly: AssemblyDelaySignAttribute(false)]

Chizl.FileComparer/StartForm.cs

Lines changed: 143 additions & 56 deletions
Large diffs are not rendered by default.

models/ComparisonResults.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
35
using System.Linq;
46

57
namespace Chizl.FileCompare
@@ -36,9 +38,11 @@ internal ComparisonResults(Exception ex)
3638
/// </summary>
3739
/// <param name="compareDiffs">List of per-line comparison results.</param>
3840
/// <param name="isBinary">True if the files compared were binary; otherwise false.</param>
39-
internal ComparisonResults(List<CompareDiff> compareDiffs, bool isBinary = false)
41+
internal ComparisonResults(List<CompareDiff> compareDiffs, FileLevel sourceFile, FileLevel targetFile)
4042
{
41-
this.IsBinary = isBinary;
43+
this.SourceFile = sourceFile;
44+
this.TargetFile = targetFile;
45+
this.IsBinary = sourceFile.IsBinary || targetFile.IsBinary;
4246
this.LineComparison = compareDiffs.ToArray();
4347
this.Diffs = new DiffCounts(
4448
compareDiffs.Count(w => w.DiffType == DiffType.Added),
@@ -54,6 +58,16 @@ internal ComparisonResults(List<CompareDiff> compareDiffs, bool isBinary = false
5458
/// </summary>
5559
public static ComparisonResults Empty { get; } = new ComparisonResults();
5660

61+
/// <summary>
62+
/// Source is usually reguarding the oldest file and the first argument in DiffTools.CompareFiles(). This holds information about the source file.
63+
/// </summary>
64+
public FileLevel SourceFile { get; }
65+
66+
/// <summary>
67+
/// Target is usually reguarding the newest file and the second argument in DiffTools.CompareFiles(). This holds information about the target file.
68+
/// </summary>
69+
public FileLevel TargetFile { get; }
70+
5771
/// <summary>
5872
/// Indicates whether this instance contains meaningful comparison data.
5973
/// True if the instance is empty or created due to an exception.

models/FileLevel.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ namespace Chizl.FileCompare
55
{
66
public class FileLevel : IDisposable
77
{
8-
const int _maxContentFileSize = ((1024 * 1024) * 10); // 10MB
8+
const int _maxContentFileSize = ((1024 * 1024) * 50); // 50MB
99
private bool disposedValue;
10-
// private LoadContentType _loadedType;
1110
private byte[] _bytes;
1211
private string[] _content;
13-
// private FileStream _fileStream;
1412

1513
#region Constructor/Deconstructor
1614
public FileLevel(string fullPath)
@@ -39,19 +37,7 @@ public FileLevel(string fullPath)
3937
$"Future versions will be streaming content.");
4038

4139
if (loadContentType == LoadContentType.InMemory)
42-
{
43-
// if (sizeFits)
44-
// {
45-
_bytes = File.ReadAllBytes(fi.FullName);
46-
// _loadedType = LoadContentType.InMemory;
47-
// }
48-
// else
49-
// {
50-
// _loadedType = LoadContentType.Stream;
51-
// _fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
52-
// }
53-
}
54-
// else if (loadContentType == LoadContentType.InMemory && !sizeFits)
40+
_bytes = File.ReadAllBytes(fi.FullName);
5541

5642
this.Format = Common.IsBinary(this.Bytes, this.Bytes.Length, out string err)
5743
&& string.IsNullOrWhiteSpace(err)
@@ -69,11 +55,7 @@ protected virtual void Dispose(bool disposing)
6955
{
7056
if (!disposedValue)
7157
{
72-
if (disposing)
73-
{
74-
// _fileStream?.Close();
75-
// _fileStream?.Dispose();
76-
}
58+
if (disposing){}
7759

7860
disposedValue = true;
7961
}
@@ -89,8 +71,6 @@ public void Dispose()
8971

9072
internal int Pointer { get; set; }
9173

92-
// public LoadContentType LoadedType { get { return _loadedType; } }
93-
// public FileStream FileStream { get { return _fileStream; } }
9474
public byte[] Bytes { get { return _bytes; } }
9575
public string[] Content { get { return _content; } }
9676
public string FullPath { get; }

rtf/RtfBuilder.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ public class RtfBuilder
2828
\cf2 = Green for added bytes
2929
/**/
3030

31-
const string FONT_FAMILY_SIZE = @"{\rtf1\ansi\ansicpg1250\deff0\deflang1050{\fonttbl{\f0\fnil\fcharset238 Courier New;}}\fs17\r\n"; // \r\n
31+
const string FONT_BASE = @"{\rtf1\ansi\ansicpg1250\deff0\deflang1050{\fonttbl{\f0\fnil\fcharset238 ";
3232
const string START_COLORS = @"{\colortbl;";
33-
const string EMPTY_FGCOLOR = @"\cf0"; // \~
34-
const string EMPTY_BGCOLOR = @"\chcbpat0 ";
3533

3634
private readonly RtfColorManager _colorTableManager;
3735
private readonly StringBuilder _contentTextRtf = new StringBuilder();
@@ -41,7 +39,7 @@ public class RtfBuilder
4139
private bool _pageSetup = true;
4240
private int _lastFontSize = 17;
4341
private int _originalFontSize = 0; // 17/2 = 8.5pt
44-
private string _fontFamilyWithSize = "{\\rtf1\\ansi\\ansicpg1250\\deff0\\deflang1050{\\fonttbl{\\f0\\fnil\\fcharset238 _FONTNAME_;}}\\fs_FONTSIZE_\r\n";
42+
private string _fontFamWithSize = FONT_BASE + "_FONTNAME_;}}\\fs_FONTSIZE_\r\n";
4543

4644
/// <summary>
4745
///
@@ -70,7 +68,7 @@ public RtfBuilder(Color[] colorIndexList, bool pageSetup = true, string fontName
7068
// 1st Font Index, (17/2 = 8.5pt)
7169
_lastFontSize = (int)Math.Round(fontPtSize * 2, 0);
7270

73-
_fontFamilyWithSize = _fontFamilyWithSize.Replace("_FONTNAME_", fontName).Replace("_FONTSIZE_", _lastFontSize.ToString());
71+
_fontFamWithSize = _fontFamWithSize.Replace("_FONTNAME_", fontName).Replace("_FONTSIZE_", _lastFontSize.ToString());
7472
_colorTableManager = new RtfColorManager(colorIndexList);
7573
_pageSetup = pageSetup;
7674
RtfSetupPage();
@@ -151,6 +149,14 @@ public string GetDocument(bool clear = true)
151149

152150
return retVal;
153151
}
152+
public int DocumentSize
153+
{
154+
get
155+
{
156+
// +1 because of the final '}' when GetDocument() is called.
157+
return _contentTextRtf.Length + 1;
158+
}
159+
}
154160
/// <summary>
155161
/// View existing RTF context data, which may or may not be complete.<br/>
156162
/// GetDocument(), will finalize the RTF document with any following RTF requirements.
@@ -175,7 +181,7 @@ internal void AddRTF(string text, bool endLine)
175181
if (endLine)
176182
{
177183
_contentTextAscii.Append("\n");
178-
_contentTextRtf.Append("\\par"); // \\par = RTF Line Feed, same as "\n"
184+
_contentTextRtf.Append($"\\par {Environment.NewLine}"); // \\par = RTF Line Feed, same as "\n"
179185
}
180186
}
181187
internal void AddText(string text, Color bg, Color fg, bool endLine, double fontPtSize)
@@ -185,25 +191,26 @@ internal void AddText(string text, Color bg, Color fg, bool endLine, double font
185191
// if previous background color wasn't empty and now it is, we want to reset color to default.
186192
if (!_bgPrevColor.IsEmpty && bg.IsEmpty)
187193
{
188-
_contentTextRtf.Append(EMPTY_BGCOLOR);
194+
_contentTextRtf.Append(_colorTableManager.ResetColor(Color_Appearance.Background));
189195
_bgPrevColor = bg;
190196
}
191197
// if previous foreground color wasn't empty and now it is, we want to reset color to default.
192198
if (!_fgPrevColor.IsEmpty && fg.IsEmpty)
193199
{
194-
_contentTextRtf.Append(EMPTY_FGCOLOR);
200+
//_contentTextRtf.Append(EMPTY_FGCOLOR);
201+
_contentTextRtf.Append(_colorTableManager.ResetColor(Color_Appearance.Foreground));
195202
_fgPrevColor = fg;
196203
}
197204
// if background color isn't empty and previous background is different than new color, lets set color.
198205
if (!bg.IsEmpty && !_bgPrevColor.Equals(bg))
199206
{
200-
_contentTextRtf.Append(_colorTableManager.GetIndex(bg, Color_Appearance.Background));
207+
_contentTextRtf.Append(_colorTableManager.GetCode(bg, Color_Appearance.Background));
201208
_bgPrevColor = bg;
202209
}
203210
// if foregroundcolor isn't empty and previous foregroundis different than new color, lets set color.
204211
if (!fg.IsEmpty && !_fgPrevColor.Equals(fg))
205212
{
206-
_contentTextRtf.Append(_colorTableManager.GetIndex(fg, Color_Appearance.Foreground));
213+
_contentTextRtf.Append(_colorTableManager.GetCode(fg, Color_Appearance.Foreground));
207214
_fgPrevColor = fg;
208215
}
209216

@@ -219,7 +226,7 @@ internal void AddText(string text, Color bg, Color fg, bool endLine, double font
219226
if (endLine)
220227
{
221228
_contentTextAscii.Append("\n");
222-
_contentTextRtf.Append("\\par"); // \\par = RTF Line Feed, same as "\n"
229+
_contentTextRtf.Append($"\\par {Environment.NewLine}"); // \\par = RTF Line Feed, same as "\n"
223230
}
224231
}
225232
private int GetSize(double fontPtSize) => (int)Math.Round(fontPtSize * 2, 0);
@@ -239,7 +246,7 @@ private void SetSize(double fontPtSize, bool append = true)
239246
{
240247
_lastFontSize = getRtfSize;
241248
if (append)
242-
_contentTextAscii.Append($"\\fs{_lastFontSize} ");
249+
_contentTextAscii.Append($"\\fs{_lastFontSize} {Environment.NewLine}");
243250
}
244251
}
245252
}
@@ -256,8 +263,8 @@ private void RtfSetupPage()
256263

257264
if (_pageSetup)
258265
{
259-
_contentTextRtf.Append(_fontFamilyWithSize); // {\rtf1\ansi\deff0 and font table + default font
260-
_contentTextRtf.Append(START_COLORS); // {\colortbl;
266+
_contentTextRtf.Append(_fontFamWithSize); // {\rtf1\ansi\deff0 and font table + default font
267+
_contentTextRtf.Append(START_COLORS); // {\colortbl;
261268

262269
foreach (var colorIndex in _colorTableManager.ColorArray)
263270
{
@@ -266,7 +273,7 @@ private void RtfSetupPage()
266273
_contentTextRtf.Append($"\\red{colorIndex.R}\\green{colorIndex.G}\\blue{colorIndex.B};");
267274
}
268275

269-
_contentTextRtf.Append("}");
276+
_contentTextRtf.Append($"}} {Environment.NewLine}");
270277
}
271278
}
272279
private bool ValidFontSize(double fontPtSize, out string msg)

rtf/RtfColorManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal enum Color_Appearance
1313
internal class RtfColorManager
1414
{
1515
readonly string[] ColorAppearances = new string[2] { @"\cf", @"\chcbpat" };
16+
const int RESET_COLOR = 0;
1617

1718
private readonly List<Color> _colors = new List<Color>();
1819
public RtfColorManager(Color[] colors)
@@ -27,7 +28,8 @@ public RtfColorManager(Color[] colors)
2728
_colors.Add(c);
2829
}
2930
}
30-
public string GetIndex(Color color, Color_Appearance appearance) => $"{ColorAppearances[(int)appearance]}{GetIndex(color)} ";
31+
public string ResetColor(Color_Appearance appearance) => $"{ColorAppearances[(int)appearance]}{RESET_COLOR} ";
32+
public string GetCode(Color color, Color_Appearance appearance) => $"{ColorAppearances[(int)appearance]}{GetIndex(color)} ";
3133
public int GetIndex(Color color)
3234
{
3335
int index = _colors.FindIndex(c => c == color);

services/BinaryComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static ComparisonResults CompareFiles(FileLevel sourceFile, FileLevel tar
118118
}
119119
}
120120

121-
return new ComparisonResults(arrDiffs, true);
121+
return new ComparisonResults(arrDiffs, sourceFile, targetFile);
122122
}
123123
private static ConcurrentDictionary<string, int> BuildHashLookups(byte[] byteData, int start, int length, byte startingByte)
124124
{

services/DiffTool.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,8 @@ public static ComparisonResults CompareFiles(string sourceFile, string targetFil
100100
// Binary path unchanged
101101
if (sourceFileInfo.IsBinary || targetFileInfo.IsBinary)
102102
return BinaryComparer.CompareFiles(sourceFileInfo, targetFileInfo);
103-
104-
// Read text lines (Myers is O(N+M) memory). If you already have arrays, call CompareStringArr directly.
105-
var a = File.ReadLines(sourceFile).ToArray();
106-
var b = File.ReadLines(targetFile).ToArray();
107-
108-
return CompareStringArr(a, b, scoreThreshold, lineLookAhead);
103+
else
104+
return CompareStringArr(sourceFileInfo, targetFileInfo, scoreThreshold, lineLookAhead);
109105
}
110106
catch (Exception ex)
111107
{
@@ -115,8 +111,12 @@ public static ComparisonResults CompareFiles(string sourceFile, string targetFil
115111
/// <summary>
116112
/// Builds results off of scripts from Myers, then merges with an LCS to seperate Added, Deleted, and Modified lines.
117113
/// </summary>
118-
private static ComparisonResults CompareStringArr(string[] linesOld, string[] linesNew, double scoreThreshold = 0.30, byte lineLookAhead = 3)
114+
private static ComparisonResults CompareStringArr(FileLevel sourceFileInfo, FileLevel targetFileInfo, double scoreThreshold = 0.30, byte lineLookAhead = 3)
119115
{
116+
// Read text lines (Myers is O(N+M) memory). If you already have arrays, call CompareStringArr directly.
117+
string[] linesOld = sourceFileInfo.Content;
118+
string[] linesNew = targetFileInfo.Content;
119+
120120
var retVal = new List<CompareDiff>();
121121
var lineNo = 0;
122122

@@ -142,14 +142,14 @@ private static ComparisonResults CompareStringArr(string[] linesOld, string[] li
142142
}
143143

144144
var textLine = entry.Text;
145-
if (prevSize == 0) prevSize = 30;
145+
if (prevSize == 0) prevSize = 32;
146146
if (textLine.Length == 0) textLine = new string(' ', prevSize);
147147

148148
retVal.Add(new CompareDiff(diffType, ++lineNo, textLine));
149149
prevSize = textLine.Length;
150150
}
151151

152-
return new ComparisonResults(retVal, false);
152+
return new ComparisonResults(retVal, sourceFileInfo, targetFileInfo);
153153
}
154154
/// <summary>
155155
/// Robust Myers implementation (array V, trace)

0 commit comments

Comments
 (0)