StringSlice Struct

A lightweight struct that represents a slice of a string.

public struct StringSlice : ICharIterator
Implements

Constructors

NameDescription
StringSlice(string)Initializes a new instance of the StringSlice struct.
StringSlice(string, NewLine)Initializes a new instance of the StringSlice struct.
StringSlice(string, int, int)Initializes a new instance of the StringSlice struct.
StringSlice(string, int, int, NewLine)Initializes a new instance of the StringSlice struct.

StringSlice overloads

StringSlice(string)

StringSlice.cs

Initializes a new instance of the StringSlice struct.

public StringSlice(string text)
Parameters
text string
The text.

StringSlice(string, NewLine)

StringSlice.cs

Initializes a new instance of the StringSlice struct.

public StringSlice(string text, NewLine newLine)
Parameters
text string
The text.
newLine NewLine
The line separation.

StringSlice(string, int, int)

StringSlice.cs

Initializes a new instance of the StringSlice struct.

public StringSlice(string text, int start, int end)
Parameters
text string
The text.
start int
The start.
end int
The end.
Exceptions
ArgumentNullException

StringSlice(string, int, int, NewLine)

StringSlice.cs

Initializes a new instance of the StringSlice struct.

public StringSlice(string text, int start, int end, NewLine newLine)
Parameters
text string
The text.
start int
The start.
end int
The end.
newLine NewLine
The line separation.
Exceptions
ArgumentNullException

Properties

NameDescription
CurrentCharGets the current character.
CurrentRuneGets the current Rune. Recognizes supplementary code points that cannot be covered by a single Char.
EndGets or sets the end position (inclusive) within StringSlice.Text.
IsEmptyGets a value indicating whether this instance is empty.
LengthGets the length.
StartGets or sets the start position within StringSlice.Text.

CurrentChar

StringSlice.cs

Gets the current character.

public readonly char CurrentChar { get; }

Property value

char

CurrentRune

StringSlice.cs

Gets the current Rune. Recognizes supplementary code points that cannot be covered by a single Char.

public readonly Rune CurrentRune { get; }

Property value

Rune

Gets or sets the end position (inclusive) within StringSlice.Text.

public int End { readonly get; set; }

Property value

int

Gets a value indicating whether this instance is empty.

public readonly bool IsEmpty { get; }

Property value

bool

Gets the length.

public readonly int Length { get; }

Property value

int

Gets or sets the start position within StringSlice.Text.

public int Start { readonly get; set; }

Property value

int

Indexers

NameDescription
this[int]Gets the Char at the specified index.

Gets the Char at the specified index.

public readonly char this[int index] { get; }

Parameters

index int
The index.

Property value

char

Methods

NameDescription
AsSpan()Performs the as span operation.
IndexOf(char)Searches for the specified character within this slice.
IndexOf(string, int, bool)Searches the specified text within this slice.
IsEmptyOrWhitespace()Determines whether this slice is empty or made only of whitespaces.
Match(string, int)Matches the specified text.
Match(string, int, int)Matches the specified text.
MatchLowercase(string, int)Matches the specified text using lowercase comparison.
MatchLowercase(string, int, int)Matches the specified text using lowercase comparison.
NextChar()Goes to the next character, incrementing the StringSlice.Start position.
NextRune()Goes to the next Rune, incrementing the StringSlice.Start position. If StringSlice.CurrentRune is a supplementary character, StringSlice.Start will be advanced by 2.
Overlaps(StringSlice)Performs the overlaps operation.
PeekChar()Peeks a character at the offset of 1 from the current StringSlice.Start position inside the range StringSlice.Start and StringSlice.End, returns `\0` if outside this range.
PeekChar(int)Peeks a character at the specified offset from the current StringSlice.Start position inside the range StringSlice.Start and StringSlice.End, returns `\0` if outside this range.
PeekCharAbsolute(int)Peeks a character at the specified offset from the current beginning of the string, without taking into account StringSlice.Start and StringSlice.End
PeekCharExtra(int)Peeks a character at the specified offset from the current beginning of the slice without using the range StringSlice.Start or StringSlice.End, returns `\0` if outside the StringSlice.Text.
PeekRuneExtra(int)Peeks a Rune at the specified offset from the current beginning of the slice without using the range StringSlice.Start or StringSlice.End, returns default if outside the StringSlice.Text. Recognizes supplementary code points that cannot be covered by a single Char. A positive offset value expects the high surrogate and a negative offset expects the low surrogate of the surrogate pair of a supplementary character at that position.
RuneAt(int)Gets the Rune at the specified index. Recognizes supplementary code points that cannot be covered by a single Char.
SkipChar()Goes to the next character, incrementing the StringSlice.Start position.
SkipSpacesToEndOfLineOrEndOfDocument()Expect spaces until a end of line. Return false otherwise.
ToString()Returns a String that represents this instance.
Trim()Trims whitespaces from both the start and end of this slice.
TrimEnd()Trims whitespaces at the end of this slice, starting from StringSlice.End position.
TrimStart()Trims whitespaces at the beginning of this slice starting from StringSlice.Start position.
TrimStart(int)Trims whitespaces at the beginning of this slice starting from StringSlice.Start position.

Performs the as span operation.

public readonly ReadOnlySpan<char> AsSpan()

IndexOf overloads

IndexOf(char)

StringSlice.cs

Searches for the specified character within this slice.

public readonly int IndexOf(char c)
Parameters
c char
Returns

int A value >= 0 if the character was found, otherwise < 0

IndexOf(string, int, bool)

StringSlice.cs

Searches the specified text within this slice.

public readonly int IndexOf(string text, int offset = 0, bool ignoreCase = false)
Parameters
text string
The text.
offset int
The offset.
ignoreCase bool
true if ignore case
Returns

int true if the text was found; false otherwise

IsEmptyOrWhitespace()

StringSlice.cs

Determines whether this slice is empty or made only of whitespaces.

public readonly bool IsEmptyOrWhitespace()

Returns

bool true if this slice is empty or made only of whitespaces; false otherwise

Match overloads

Match(string, int)

StringSlice.cs

Matches the specified text.

public readonly bool Match(string text, int offset = 0)
Parameters
text string
The text.
offset int
The offset.
Returns

bool true if the text matches; false otherwise

Match(string, int, int)

StringSlice.cs

Matches the specified text.

public readonly bool Match(string text, int end, int offset)
Parameters
text string
The text.
end int
The end.
offset int
The offset.
Returns

bool true if the text matches; false otherwise

MatchLowercase overloads

MatchLowercase(string, int)

StringSlice.cs

Matches the specified text using lowercase comparison.

public readonly bool MatchLowercase(string text, int offset = 0)
Parameters
text string
The text.
offset int
The offset.
Returns

bool true if the text matches; false otherwise

MatchLowercase(string, int, int)

StringSlice.cs

Matches the specified text using lowercase comparison.

public readonly bool MatchLowercase(string text, int end, int offset)
Parameters
text string
The text.
end int
The end.
offset int
The offset.
Returns

bool true if the text matches; false otherwise

NextChar()

StringSlice.cs

Goes to the next character, incrementing the StringSlice.Start position.

public char NextChar()

Returns

char The next character. `\0` is end of the iteration.

NextRune()

StringSlice.cs

Goes to the next Rune, incrementing the StringSlice.Start position. If StringSlice.CurrentRune is a supplementary character, StringSlice.Start will be advanced by 2.

public Rune NextRune()

Returns

Rune The current rune or default if the next position contains an incomplete surrogate pair or StringSlice.IsEmpty.

Overlaps(StringSlice)

StringSlice.cs

Performs the overlaps operation.

public bool Overlaps(StringSlice other)

Parameters

other StringSlice

Returns

bool

PeekChar overloads

PeekChar()

StringSlice.cs

Peeks a character at the offset of 1 from the current StringSlice.Start position inside the range StringSlice.Start and StringSlice.End, returns `\0` if outside this range.

public readonly char PeekChar()
Returns

char The character at offset, returns `\0` if none.

PeekChar(int)

StringSlice.cs

Peeks a character at the specified offset from the current StringSlice.Start position inside the range StringSlice.Start and StringSlice.End, returns `\0` if outside this range.

public readonly char PeekChar(int offset)
Parameters
offset int
The offset.
Returns

char The character at offset, returns `\0` if none.

PeekCharAbsolute(int)

StringSlice.cs

Peeks a character at the specified offset from the current beginning of the string, without taking into account StringSlice.Start and StringSlice.End

public readonly char PeekCharAbsolute(int index)

Parameters

index int

Returns

char The character at offset, returns `\0` if none.

PeekCharExtra(int)

StringSlice.cs

Peeks a character at the specified offset from the current beginning of the slice without using the range StringSlice.Start or StringSlice.End, returns `\0` if outside the StringSlice.Text.

public readonly char PeekCharExtra(int offset)

Parameters

offset int
The offset.

Returns

char The character at offset, returns `\0` if none.

PeekRuneExtra(int)

StringSlice.cs

Peeks a Rune at the specified offset from the current beginning of the slice without using the range StringSlice.Start or StringSlice.End, returns default if outside the StringSlice.Text. Recognizes supplementary code points that cannot be covered by a single Char. A positive offset value expects the high surrogate and a negative offset expects the low surrogate of the surrogate pair of a supplementary character at that position.

public readonly Rune PeekRuneExtra(int offset)

Parameters

offset int
The offset.

Returns

Rune The rune at the specified offset, returns default if none.

RuneAt(int)

StringSlice.cs

Gets the Rune at the specified index. Recognizes supplementary code points that cannot be covered by a single Char.

public readonly Rune RuneAt(int index)

Parameters

index int
The index into StringSlice.Text.

Returns

Rune The rune at the specified index or default if the location contains an incomplete surrogate pair.

Exceptions

IndexOutOfRangeException
Thrown when the given index is out of range

SkipChar()

StringSlice.cs

Goes to the next character, incrementing the StringSlice.Start position.

public void SkipChar()

SkipSpacesToEndOfLineOrEndOfDocument()

StringSlice.cs

Expect spaces until a end of line. Return false otherwise.

public bool SkipSpacesToEndOfLineOrEndOfDocument()

Returns

bool true if whitespaces where matched until a end of line

ToString()

StringSlice.cs

Returns a String that represents this instance.

public override readonly string ToString()

Returns

string A String that represents this instance.

Trims whitespaces from both the start and end of this slice.

public void Trim()

Trims whitespaces at the end of this slice, starting from StringSlice.End position.

public bool TrimEnd()

Returns

bool

TrimStart overloads

TrimStart()

StringSlice.cs

Trims whitespaces at the beginning of this slice starting from StringSlice.Start position.

public bool TrimStart()
Returns

bool true if it has reaches the end of the iterator

TrimStart(int)

StringSlice.cs

Trims whitespaces at the beginning of this slice starting from StringSlice.Start position.

public void TrimStart(out int spaceCount)
Parameters
spaceCount int
The number of spaces trimmed.

Fields

NameDescription
EmptyAn empty string slice.
NewLineGets or sets the new line.
TextThe text of this slice.

Empty

An empty string slice.

public static readonly StringSlice Empty

NewLine

Gets or sets the new line.

public NewLine NewLine

Text

The text of this slice.

public readonly string Text