Skip to content
Closed
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
2 changes: 1 addition & 1 deletion DiffPlex.ConsoleRunner/DiffPlex.ConsoleRunner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
25 changes: 12 additions & 13 deletions DiffPlex/Chunkers/CharacterChunker.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using System.Collections.Generic;

namespace DiffPlex.Chunkers
namespace DiffPlex.Chunkers;

public class CharacterChunker : IChunker
{
public class CharacterChunker:IChunker
{
/// <summary>
/// Gets the default singleton instance of the chunker.
/// </summary>
public static CharacterChunker Instance { get; } = new CharacterChunker();
/// <summary>
/// Gets the default singleton instance of the chunker.
/// </summary>
public static CharacterChunker Instance { get; } = new();

public IReadOnlyList<string> Chunk(string text)
{
var s = new string[text.Length];
for (int i = 0; i < text.Length; i++) s[i] = text[i].ToString();
return s;
}
public IReadOnlyList<string> Chunk(string text)
{
var s = new string[text.Length];
for (int i = 0; i < text.Length; i++) s[i] = text[i].ToString();
return s;
}
}
21 changes: 7 additions & 14 deletions DiffPlex/Chunkers/CustomFunctionChunker.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
using System;
using System.Collections.Generic;

namespace DiffPlex.Chunkers
{
public class CustomFunctionChunker: IChunker
{
private readonly Func<string, IReadOnlyList<string>> customChunkerFunc;
namespace DiffPlex.Chunkers;

public CustomFunctionChunker(Func<string, IReadOnlyList<string>> customChunkerFunc)
{
if (customChunkerFunc == null) throw new ArgumentNullException(nameof(customChunkerFunc));
this.customChunkerFunc = customChunkerFunc;
}
public class CustomFunctionChunker(Func<string, IReadOnlyList<string>> customChunkerFunc) : IChunker
{
private readonly Func<string, IReadOnlyList<string>> customChunkerFunc = customChunkerFunc ?? throw new ArgumentNullException(nameof(customChunkerFunc));

public IReadOnlyList<string> Chunk(string text)
{
return customChunkerFunc(text);
}
public IReadOnlyList<string> Chunk(string text)
{
return customChunkerFunc(text);
}
}
97 changes: 44 additions & 53 deletions DiffPlex/Chunkers/DelimiterChunker.cs
Original file line number Diff line number Diff line change
@@ -1,82 +1,73 @@
using System;
using System.Collections.Generic;

namespace DiffPlex.Chunkers
{
public class DelimiterChunker : IChunker
{
private readonly char[] delimiters;

public DelimiterChunker(char[] delimiters)
{
if (delimiters is null || delimiters.Length == 0)
{
throw new ArgumentException($"{nameof(delimiters)} cannot be null or empty.", nameof(delimiters));
}
namespace DiffPlex.Chunkers;

this.delimiters = delimiters;
}
public class DelimiterChunker(char[] delimiters) : IChunker
{
private readonly char[] delimiters = delimiters is null || delimiters.Length == 0
? throw new ArgumentException($"{nameof(delimiters)} cannot be null or empty.", nameof(delimiters))
: delimiters;

public IReadOnlyList<string> Chunk(string str)
public IReadOnlyList<string> Chunk(string str)
{
var list = new List<string>();
int begin = 0;
bool processingDelim = false;
int delimBegin = 0;
for (int i = 0; i < str.Length; i++)
{
var list = new List<string>();
int begin = 0;
bool processingDelim = false;
int delimBegin = 0;
for (int i = 0; i < str.Length; i++)
if (Array.IndexOf(delimiters, str[i]) != -1)
{
if (Array.IndexOf(delimiters, str[i]) != -1)
if (i >= str.Length - 1)
{
if (i >= str.Length - 1)
if (processingDelim)
{
if (processingDelim)
{
list.Add(str.Substring(delimBegin, (i + 1 - delimBegin)));
}
else
{
list.Add(str.Substring(begin, (i - begin)));
list.Add(str.Substring(i, 1));
}
list.Add(str.Substring(delimBegin, (i + 1 - delimBegin)));
}
else
{
if (!processingDelim)
{
// Add everything up to this delimeter as the next chunk (if there is anything)
if (i - begin > 0)
{
list.Add(str.Substring(begin, (i - begin)));
}

processingDelim = true;
delimBegin = i;
}
list.Add(str.Substring(begin, (i - begin)));
list.Add(str.Substring(i, 1));
}

begin = i + 1;
}
else
{
if (processingDelim)
if (!processingDelim)
{
if (i - delimBegin > 0)
// Add everything up to this delimeter as the next chunk (if there is anything)
if (i - begin > 0)
{
list.Add(str.Substring(delimBegin, (i - delimBegin)));
list.Add(str.Substring(begin, (i - begin)));
}

processingDelim = false;
processingDelim = true;
delimBegin = i;
}
}

// If we are at the end, add the remaining as the last chunk
if (i >= str.Length - 1)
begin = i + 1;
}
else
{
if (processingDelim)
{
if (i - delimBegin > 0)
{
list.Add(str.Substring(begin, (i + 1 - begin)));
list.Add(str.Substring(delimBegin, (i - delimBegin)));
}

processingDelim = false;
}
}

return list;
// If we are at the end, add the remaining as the last chunk
if (i >= str.Length - 1)
{
list.Add(str.Substring(begin, (i + 1 - begin)));
}
}
}

return list;
}
}
23 changes: 11 additions & 12 deletions DiffPlex/Chunkers/LineChunker.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using System;
using System.Collections.Generic;

namespace DiffPlex.Chunkers
namespace DiffPlex.Chunkers;

public class LineChunker : IChunker
{
public class LineChunker:IChunker
{
private readonly string[] lineSeparators = new[] {"\r\n", "\r", "\n"};
private readonly string[] lineSeparators = ["\r\n", "\r", "\n"];

/// <summary>
/// Gets the default singleton instance of the chunker.
/// </summary>
public static LineChunker Instance { get; } = new LineChunker();
/// <summary>
/// Gets the default singleton instance of the chunker.
/// </summary>
public static LineChunker Instance { get; } = new();

public IReadOnlyList<string> Chunk(string text)
{
return text.Split(lineSeparators, StringSplitOptions.None);
}
public IReadOnlyList<string> Chunk(string text)
{
return text.Split(lineSeparators, StringSplitOptions.None);
}
}
75 changes: 37 additions & 38 deletions DiffPlex/Chunkers/LineEndingsPreservingChunker.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
using System.Collections.Generic;

namespace DiffPlex.Chunkers
namespace DiffPlex.Chunkers;

public class LineEndingsPreservingChunker : IChunker
{
public class LineEndingsPreservingChunker:IChunker
private static readonly string[] EmptyArray = [];

/// <summary>
/// Gets the default singleton instance of the chunker.
/// </summary>
public static LineEndingsPreservingChunker Instance { get; } = new();

public IReadOnlyList<string> Chunk(string text)
{
private static readonly string[] EmptyArray = new string[0];
if (string.IsNullOrEmpty(text))
return EmptyArray;

/// <summary>
/// Gets the default singleton instance of the chunker.
/// </summary>
public static LineEndingsPreservingChunker Instance { get; } = new LineEndingsPreservingChunker();
var output = new List<string>();
var lastCut = 0;

public IReadOnlyList<string> Chunk(string text)
for (int i = 0; i < text.Length; i++)
{
if (string.IsNullOrEmpty(text))
return EmptyArray;
int lineEndLen = 0;

var output = new List<string>();
var lastCut = 0;

for (int i = 0; i < text.Length; i++)
if (text[i] == '\r')
{
lineEndLen = 1;
if (i + 1 < text.Length && text[i + 1] == '\n')
lineEndLen = 2; // CRLF
}
else if (text[i] == '\n')
{
int lineEndLen = 0;

if (text[i] == '\r')
{
lineEndLen = 1;
if (i + 1 < text.Length && text[i + 1] == '\n')
lineEndLen = 2; // CRLF
}
else if (text[i] == '\n')
{
lineEndLen = 1; // LF
}

if (lineEndLen > 0)
{
int sliceLen = i - lastCut + lineEndLen;
output.Add(text.Substring(lastCut, sliceLen));

i += lineEndLen - 1; // we already consumed them
lastCut += sliceLen;
}
lineEndLen = 1; // LF
}

if (lastCut != text.Length) // trailing line without EOL
output.Add(text.Substring(lastCut));
if (lineEndLen > 0)
{
int sliceLen = i - lastCut + lineEndLen;
output.Add(text.Substring(lastCut, sliceLen));

return output;
i += lineEndLen - 1; // we already consumed them
lastCut += sliceLen;
}
}

if (lastCut != text.Length) // trailing line without EOL
output.Add(text.Substring(lastCut));

return output;
}
}
21 changes: 10 additions & 11 deletions DiffPlex/Chunkers/WordChunker.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
namespace DiffPlex.Chunkers
namespace DiffPlex.Chunkers;

public class WordChunker : DelimiterChunker
{
public class WordChunker:DelimiterChunker
{
private static char[] WordSeparators { get; } = { ' ', '\t', '.', '(', ')', '{', '}', ',', '!', '?', ';' };
private static char[] WordSeparators { get; } = [' ', '\t', '.', '(', ')', '{', '}', ',', '!', '?', ';'];

/// <summary>
/// Gets the default singleton instance of the chunker.
/// </summary>
public static WordChunker Instance { get; } = new WordChunker();
/// <summary>
/// Gets the default singleton instance of the chunker.
/// </summary>
public static WordChunker Instance { get; } = new();

public WordChunker() : base(WordSeparators)
{
}
public WordChunker() : base(WordSeparators)
{
}
}
11 changes: 5 additions & 6 deletions DiffPlex/DiffBuilder/IInlineDiffBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using DiffPlex.DiffBuilder.Model;

namespace DiffPlex.DiffBuilder
namespace DiffPlex.DiffBuilder;

public interface IInlineDiffBuilder
{
public interface IInlineDiffBuilder
{
DiffPaneModel BuildDiffModel(string oldText, string newText);
DiffPaneModel BuildDiffModel(string oldText, string newText, bool ignoreWhitespace, bool ignoreCase, IChunker chunker);
}
DiffPaneModel BuildDiffModel(string oldText, string newText);
DiffPaneModel BuildDiffModel(string oldText, string newText, bool ignoreWhitespace, bool ignoreCase, IChunker chunker);
}
23 changes: 11 additions & 12 deletions DiffPlex/DiffBuilder/ISideBySideDiffBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using DiffPlex.DiffBuilder.Model;

namespace DiffPlex.DiffBuilder
namespace DiffPlex.DiffBuilder;

/// <summary>
/// Provides methods that generate differences between texts for displaying in a side by side view.
/// </summary>
public interface ISideBySideDiffBuilder
{
/// <summary>
/// Provides methods that generate differences between texts for displaying in a side by side view.
/// Builds a diff model for displaying diffs in a side by side view
/// </summary>
public interface ISideBySideDiffBuilder
{
/// <summary>
/// Builds a diff model for displaying diffs in a side by side view
/// </summary>
/// <param name="oldText">The old text.</param>
/// <param name="newText">The new text.</param>
/// <returns>The side by side diff model</returns>
SideBySideDiffModel BuildDiffModel(string oldText, string newText);
}
/// <param name="oldText">The old text.</param>
/// <param name="newText">The new text.</param>
/// <returns>The side by side diff model</returns>
SideBySideDiffModel BuildDiffModel(string oldText, string newText);
}
Loading
Loading