CharReadOnlySpanExtensions Class

Provides extension methods for read-only spans of characters.

public static class CharReadOnlySpanExtensions

Methods

NameDescription
ReadOnlySpan<char>.ApplyEdits(IEnumerable<TextEdit>)Applies a set of edits to a span of characters, and returns the resulting string.
ReadOnlySpan<char>.ComputeLineClipping(int, int)Finds the first and the last lines of a span of characters, and counts the lines between them.
ReadOnlySpan<char>.ComputeLiteralLength(StringLiteralKind)Computes the length of the representation of a span of characters as a string literal.
ReadOnlySpan<char>.ComputeQuotedLiteralLength()Computes the length of the quoted literal representation of a span of characters.
ReadOnlySpan<char>.ComputeVerbatimLiteralLength()Computes the length of the verbatim literal representation of a span of characters.
ReadOnlySpan<char>.ToClippedLiteral(StringLiteralKind, int, int, bool)Builds and returns a string representing a given span of characters as a C# string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters.
ReadOnlySpan<char>.ToClippedQuotedLiteral(int, int, bool)Builds and returns a string representing a given span of characters as a C# quoted string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters.
ReadOnlySpan<char>.ToClippedVerbatimLiteral(int, int, bool)Builds and returns a string representing a given span of characters as a C# verbatim string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters.
ReadOnlySpan<char>.ToLiteral(StringLiteralKind)Builds and returns a string representing a given span of characters as a C# string literal.
ReadOnlySpan<char>.ToQuotedLiteral()Builds and returns a string representing a given span of characters as a C# quoted string literal.
ReadOnlySpan<char>.ToVerbatimLiteral()Builds and returns a string representing a given span of characters as a C# verbatim string literal.

ReadOnlySpan<char>.ApplyEdits(IEnumerable<TextEdit>)

Applies a set of edits to a span of characters, and returns the resulting string.

extension(ReadOnlySpan<char> @this)
{
    public string ApplyEdits(IEnumerable<TextEdit> edits)
}

Parameters

this ReadOnlySpan<char>
The ReadOnlySpan<char> on which this method is called.
edits IEnumerable<TextEdit>
The edits to apply. The TextEdit.Start of every edit is an index in this, whatever the other edits do.

Returns

string A newly-constructed string, in which TextEdit.Text has replaced the characters of each edit.

Exceptions

ArgumentNullException
edits is null.
ArgumentOutOfRangeException
An edit has a negative TextEdit.Start, a TextEdit.Start greater than the length of this, a negative TextEdit.Length, or characters to replace after the end of this.
ArgumentException
Two edits overlap.

Remarks

The order of edits matters only for insertions at one index: their texts appear in the result in the order of edits.

Two edits overlap when both replace one character, or when one inserts a text between two characters that the other replaces.

An insertion at the first character that another edit replaces overlaps nothing: the inserted text precedes the replacing text in the result. An insertion just after the last character that another edit replaces follows the replacing text.

See also

ReadOnlySpan<char>.ComputeLineClipping(int, int)

Finds the first and the last lines of a span of characters, and counts the lines between them.

extension(ReadOnlySpan<char> @this)
{
    public LineClipping ComputeLineClipping(int headLines, int tailLines)
}

Parameters

this ReadOnlySpan<char>
The ReadOnlySpan<char> on which this method is called.
headLines int
The number of lines to keep at the start of the span, when the span is clipped.
tailLines int
The number of lines to keep at the end of the span, when the span is clipped.

Returns

LineClipping A LineClipping value, whose ranges refer to this.

Exceptions

ArgumentOutOfRangeException
headLines is less than 0. - or - tailLines is less than 0.

Remarks

A line ends at a line feed (\n), at a carriage return (\r), at a CR-LF pair (\r\n), or at the end of the span, as in TextReader.ReadLine.

A line terminator at the end of the span does not start a new line.

A blank line is a line that is empty, or holds white-space characters alone, as Char.IsWhiteSpace defines them. This method ignores the blank lines at the start and at the end of the span, and the rest of these remarks does not count them among the lines of the span. A blank line between two other lines is not ignored.

An empty span and a span of blank lines alone have no lines.

A span of more than (headLines + tailLines + 1) lines is clipped: LineClipping.Head holds its first headLines lines, and LineClipping.Tail its last tailLines lines. The reason for the +1 above is that a clipped text is usually displayed with a placeholder line (e.g., "--- more... ---") between head and tail.

When a span is not clipped, LineClipping.Head holds all of its lines, and LineClipping.Tail is empty.

This method copies no characters and allocates no memory.

See also

ReadOnlySpan<char>.ComputeLiteralLength(StringLiteralKind)

Computes the length of the representation of a span of characters as a string literal.

extension(ReadOnlySpan<char> @this)
{
    public int ComputeLiteralLength(StringLiteralKind literalKind)
}

Parameters

this ReadOnlySpan<char>
The ReadOnlySpan<char> on which this method is called.
literalKind StringLiteralKind
A StringLiteralKind constant specifying the kind of string literal that would be built.

Returns

int The length of the string that would result from calling ReadOnlySpan<char>.ToLiteral on this with literalKind as last parameter.

Exceptions

ArgumentException
literalKind is neither StringLiteralKind.Quoted nor StringLiteralKind.Verbatim.

ReadOnlySpan<char>.ComputeQuotedLiteralLength()

Computes the length of the quoted literal representation of a span of characters.

extension(ReadOnlySpan<char> @this)
{
    public int ComputeQuotedLiteralLength()
}

Parameters

this ReadOnlySpan<char>
The ReadOnlySpan<char> on which this method is called.

Returns

int The length of the string that would result from calling ReadOnlySpan<char>.ToQuotedLiteral on this.

ReadOnlySpan<char>.ComputeVerbatimLiteralLength()

Computes the length of the verbatim literal representation of a span of characters.

extension(ReadOnlySpan<char> @this)
{
    public int ComputeVerbatimLiteralLength()
}

Parameters

this ReadOnlySpan<char>
The ReadOnlySpan<char> on which this method is called.

Returns

int The length of the string that would result from calling ReadOnlySpan<char>.ToVerbatimLiteral on this.

ReadOnlySpan<char>.ToClippedLiteral(StringLiteralKind, int, int, bool)

Builds and returns a string representing a given span of characters as a C# string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters.

extension(ReadOnlySpan<char> @this)
{
    public string ToClippedLiteral(StringLiteralKind literalKind, int headLength, int tailLength, bool useUnicodeEllipsis = false)
}

Parameters

this ReadOnlySpan<char>
The ReadOnlySpan<char> on which this method is called.
literalKind StringLiteralKind
A StringLiteralKind constant specifying the kind of string literal to build.
headLength int
The number of characters to leave before clipping.
tailLength int
The number of characters to leave after clipping.
useUnicodeEllipsis bool
true to use a single Unicode character Ellipsis ('\x2026', …) to replace the clipped part; false to use three dots (...) instead. Default is false.

Returns

string A newly-constructed string.

Exceptions

ArgumentException
literalKind is neither StringLiteralKind.Quoted nor StringLiteralKind.Verbatim.
ArgumentOutOfRangeException
headLength is less than 0. - or - tailLength is less than 0.

See also

ReadOnlySpan<char>.ToClippedQuotedLiteral(int, int, bool)

Builds and returns a string representing a given span of characters as a C# quoted string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters.

extension(ReadOnlySpan<char> @this)
{
    public string ToClippedQuotedLiteral(int headLength, int tailLength, bool useUnicodeEllipsis = false)
}

Parameters

this ReadOnlySpan<char>
The ReadOnlySpan<char> on which this method is called.
headLength int
The number of characters to leave before clipping.
tailLength int
The number of characters to leave after clipping.
useUnicodeEllipsis bool
true to use a single Unicode character Ellipsis ('\x2026', …) to replace the clipped part; false to use three dots (...) instead. Default is false.

Returns

string A newly-constructed string.

Exceptions

ArgumentOutOfRangeException
headLength is less than 0. - or - tailLength is less than 0.

See also

ReadOnlySpan<char>.ToClippedVerbatimLiteral(int, int, bool)

Builds and returns a string representing a given span of characters as a C# verbatim string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters.

extension(ReadOnlySpan<char> @this)
{
    public string ToClippedVerbatimLiteral(int headLength, int tailLength, bool useUnicodeEllipsis = false)
}

Parameters

this ReadOnlySpan<char>
The ReadOnlySpan<char> on which this method is called.
headLength int
The number of characters to leave before clipping.
tailLength int
The number of characters to leave after clipping.
useUnicodeEllipsis bool
true to use a single Unicode character Ellipsis ('\x2026', …) to replace the clipped part; false to use three dots (...) instead. Default is false.

Returns

string A newly-constructed string.

Exceptions

ArgumentOutOfRangeException
headLength is less than 0. - or - tailLength is less than 0.

See also

ReadOnlySpan<char>.ToLiteral(StringLiteralKind)

Builds and returns a string representing a given span of characters as a C# string literal.

extension(ReadOnlySpan<char> @this)
{
    public string ToLiteral(StringLiteralKind literalKind)
}

Parameters

this ReadOnlySpan<char>
The ReadOnlySpan<char> on which this method is called.
literalKind StringLiteralKind
A StringLiteralKind constant specifying the kind of string literal to build.

Returns

string A newly-constructed string.

Exceptions

ArgumentException
literalKind is neither StringLiteralKind.Quoted nor StringLiteralKind.Verbatim.

See also

ReadOnlySpan<char>.ToQuotedLiteral()

Builds and returns a string representing a given span of characters as a C# quoted string literal.

extension(ReadOnlySpan<char> @this)
{
    public string ToQuotedLiteral()
}

Parameters

this ReadOnlySpan<char>
The ReadOnlySpan<char> on which this method is called.

Returns

string A newly-constructed string.

See also

ReadOnlySpan<char>.ToVerbatimLiteral()

Builds and returns a string representing a given span of characters as a C# verbatim string literal.

extension(ReadOnlySpan<char> @this)
{
    public string ToVerbatimLiteral()
}

Parameters

this ReadOnlySpan<char>
The ReadOnlySpan<char> on which this method is called.

Returns

string A newly-constructed string.

See also