Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to IReadOnlyList #133

Merged
merged 2 commits into from
Jul 12, 2024
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
6 changes: 4 additions & 2 deletions DiffPlex/Chunkers/CharacterChunker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace DiffPlex.Chunkers
using System.Collections.Generic;

namespace DiffPlex.Chunkers
{
public class CharacterChunker:IChunker
{
Expand All @@ -7,7 +9,7 @@ public class CharacterChunker:IChunker
/// </summary>
public static CharacterChunker Instance { get; } = new CharacterChunker();

public string[] Chunk(string text)
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();
Expand Down
7 changes: 4 additions & 3 deletions DiffPlex/Chunkers/CustomFunctionChunker.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System;
using System.Collections.Generic;

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

public CustomFunctionChunker(Func<string, string[]> customChunkerFunc)
public CustomFunctionChunker(Func<string, IReadOnlyList<string>> customChunkerFunc)
{
if (customChunkerFunc == null) throw new ArgumentNullException(nameof(customChunkerFunc));
this.customChunkerFunc = customChunkerFunc;
}

public string[] Chunk(string text)
public IReadOnlyList<string> Chunk(string text)
{
return customChunkerFunc(text);
}
Expand Down
4 changes: 2 additions & 2 deletions DiffPlex/Chunkers/DelimiterChunker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public DelimiterChunker(char[] delimiters)
this.delimiters = delimiters;
}

public string[] Chunk(string str)
public IReadOnlyList<string> Chunk(string str)
{
var list = new List<string>();
int begin = 0;
Expand Down Expand Up @@ -76,7 +76,7 @@ public string[] Chunk(string str)
}
}

return list.ToArray();
return list;
}
}
}
3 changes: 2 additions & 1 deletion DiffPlex/Chunkers/LineChunker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace DiffPlex.Chunkers
{
Expand All @@ -11,7 +12,7 @@ public class LineChunker:IChunker
/// </summary>
public static LineChunker Instance { get; } = new LineChunker();

public string[] Chunk(string text)
public IReadOnlyList<string> Chunk(string text)
{
return text.Split(lineSeparators, StringSplitOptions.None);
}
Expand Down
4 changes: 2 additions & 2 deletions DiffPlex/Chunkers/LineEndingsPreservingChunker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class LineEndingsPreservingChunker:IChunker
/// </summary>
public static LineEndingsPreservingChunker Instance { get; } = new LineEndingsPreservingChunker();

public string[] Chunk(string text)
public IReadOnlyList<string> Chunk(string text)
{
if (string.IsNullOrEmpty(text))
return emptyArray;
Expand Down Expand Up @@ -45,7 +45,7 @@ public string[] Chunk(string text)
output.Add(str);
}

return output.ToArray();
return output;
}
}
}
2 changes: 1 addition & 1 deletion DiffPlex/DiffBuilder/InlineDiffBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static void BuildDiffPieces(DiffResult diffResult, List<DiffPiece> piece
}
}

for (; bPos < diffResult.PiecesNew.Length; bPos++)
for (; bPos < diffResult.PiecesNew.Count; bPos++)
pieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1));
}
}
Expand Down
2 changes: 1 addition & 1 deletion DiffPlex/DiffBuilder/SideBySideDiffBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private static ChangeType BuildDiffPieces(DiffResult diffResult, List<DiffPiece>
}
}

while (bPos < diffResult.PiecesNew.Length && aPos < diffResult.PiecesOld.Length)
while (bPos < diffResult.PiecesNew.Count && aPos < diffResult.PiecesOld.Count)
{
oldPieces.Add(new DiffPiece(diffResult.PiecesOld[aPos], ChangeType.Unchanged, aPos + 1));
newPieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1));
Expand Down
4 changes: 1 addition & 3 deletions DiffPlex/DiffPlex.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\NuGet.props" />
<PropertyGroup>
<TargetFrameworks>net35;net40;netstandard1.0;netstandard2.0;net6.0</TargetFrameworks>
<TargetFrameworks>net45;netstandard1.0;netstandard2.0;net6.0</TargetFrameworks>
<Version>1.7.2</Version>
<PackageTags>diff</PackageTags>
<Description>DiffPlex is a diffing library that allows you to programmatically create text diffs. DiffPlex is a fast and tested library.</Description>
<PackageReleaseNotes>Fixed diffing of sub-components (like words). Ensures ignoreWhitespace and ignoreCase are honored in that case and that the parent reflects modification state of the child.</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Condition="'$(TargetFramework)' == 'net40'" Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
<PackageReference Condition="'$(TargetFramework)' == 'net35'" Include="jnm2.ReferenceAssemblies.net35" Version="1.0.1" PrivateAssets="all" />
<None Include="..\README.md" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions DiffPlex/Differ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,10 @@ private static void BuildPieceHashes(IDictionary<string, int> pieceHash, Modific
: chunker.Chunk(data.RawData);

data.Pieces = pieces;
data.HashedPieces = new int[pieces.Length];
data.Modifications = new bool[pieces.Length];
data.HashedPieces = new int[pieces.Count];
data.Modifications = new bool[pieces.Count];

for (int i = 0; i < pieces.Length; i++)
for (int i = 0; i < pieces.Count; i++)
{
string piece = pieces[i];
if (ignoreWhitespace) piece = piece.Trim();
Expand Down
6 changes: 4 additions & 2 deletions DiffPlex/IChunker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace DiffPlex
using System.Collections.Generic;

namespace DiffPlex
{
/// <summary>
/// Responsible for how to turn the document into pieces
Expand All @@ -8,6 +10,6 @@ public interface IChunker
/// <summary>
/// Divide text into sub-parts
/// </summary>
string[] Chunk(string text);
IReadOnlyList<string> Chunk(string text);
}
}
6 changes: 3 additions & 3 deletions DiffPlex/Model/DiffResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ public class DiffResult
/// <summary>
/// The chunked pieces of the old text
/// </summary>
public string[] PiecesOld { get; }
public IReadOnlyList<string> PiecesOld { get; }

/// <summary>
/// The chunked pieces of the new text
/// </summary>
public string[] PiecesNew { get; }
public IReadOnlyList<string> PiecesNew { get; }


/// <summary>
/// A collection of DiffBlocks which details deletions and insertions
/// </summary>
public IList<DiffBlock> DiffBlocks { get; }

public DiffResult(string[] piecesOld, string[] piecesNew, IList<DiffBlock> blocks)
public DiffResult(IReadOnlyList<string> piecesOld, IReadOnlyList<string> piecesNew, IList<DiffBlock> blocks)
{
PiecesOld = piecesOld;
PiecesNew = piecesNew;
Expand Down
6 changes: 4 additions & 2 deletions DiffPlex/Model/ModificationData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace DiffPlex.Model
using System.Collections.Generic;

namespace DiffPlex.Model
{
public class ModificationData
{
Expand All @@ -8,7 +10,7 @@ public class ModificationData

public bool[] Modifications { get; set; }

public string[] Pieces { get; set; }
public IReadOnlyList<string> Pieces { get; set; }

public ModificationData(string str)
{
Expand Down
8 changes: 4 additions & 4 deletions Facts.DiffPlex/Chunkers/LineEndingsPreservingChunkerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void should_split_lines_when_first_line_empty()
var chunks = chunker.Chunk(sampleText);

//ASSERT
Assert.Equal(4, chunks.Length);
Assert.Equal(4, chunks.Count);
Assert.Equal("\r\n", chunks[0]);
Assert.Equal("First\r\n", chunks[1]);
Assert.Equal("Second\r\n", chunks[2]);
Expand All @@ -34,7 +34,7 @@ public void should_split_lines_when_last_does_not_end_with_lineending()
var chunks = chunker.Chunk(sampleText);

//ASSERT
Assert.Equal(3, chunks.Length);
Assert.Equal(3, chunks.Count);
Assert.Equal("First\r\n", chunks[0]);
Assert.Equal("Second\r\n", chunks[1]);
Assert.Equal("Last", chunks[2]);
Expand All @@ -51,7 +51,7 @@ public void should_split_when_all_lines_are_empty()
var chunks = chunker.Chunk(sampleText);

//ASSERT
Assert.Equal(3, chunks.Length);
Assert.Equal(3, chunks.Count);
Assert.Equal("\r\n", chunks[0]);
Assert.Equal("\r\n", chunks[1]);
Assert.Equal("\r\n", chunks[2]);
Expand All @@ -68,7 +68,7 @@ public void should_split_when_different_line_ending()
var chunks = chunker.Chunk(sampleText);

//ASSERT
Assert.Equal(4, chunks.Length);
Assert.Equal(4, chunks.Count);
Assert.Equal("\r\n", chunks[0]);
Assert.Equal("First\n", chunks[1]);
Assert.Equal("Second\r", chunks[2]);
Expand Down
Loading