TextChunker Class

Splits a text into the chunks that a diff compares against each other. Derive from this type and override TextChunker.Chunk to supply a custom chunking strategy.

public class TextChunker

Constructors

NameDescription
TextChunker()

TextChunker()

public TextChunker()

No documentation is available.

Properties

NameDescription
CharactersGets a chunker that produces one chunk per Char.
LinesGets a chunker that splits on line terminators, keeping the terminator at the end of each chunk so the text can be rebuilt exactly.
WordsGets a chunker that alternates between runs of non-whitespace and runs of whitespace, so that whitespace is preserved in its own chunks.

Characters

TextChunker.cs

Gets a chunker that produces one chunk per Char.

public static TextChunker Characters { get; }

Property value

TextChunker

Remarks

Chunks are UTF-16 code units, not grapheme clusters: a surrogate pair is split into two chunks.

Gets a chunker that splits on line terminators, keeping the terminator at the end of each chunk so the text can be rebuilt exactly.

public static TextChunker Lines { get; }

Property value

TextChunker

Remarks

A text that ends with a line terminator produces a final empty chunk, and an empty text produces a single empty chunk. This is what makes the chunks concatenate back to the original text; TextChunker.Words and TextChunker.Characters produce no chunks at all for an empty text.

Gets a chunker that alternates between runs of non-whitespace and runs of whitespace, so that whitespace is preserved in its own chunks.

public static TextChunker Words { get; }

Property value

TextChunker

Methods

NameDescription
Chunk(ReadOnlySpan<char>)Splits value into chunks.

Chunk(ReadOnlySpan<char>)

TextChunker.cs

Splits value into chunks.

public virtual IEnumerable<string> Chunk(ReadOnlySpan<char> value)

Parameters

value ReadOnlySpan<char>
The text to split.

Returns

IEnumerable<string> The chunks, in order. Concatenating them should reproduce value exactly, otherwise the diff cannot be used to rebuild either text.

Remarks

The base implementation delegates to TextChunker.Lines. A derived chunker that does not override this method therefore produces line chunks rather than failing.