Struct StringSegment
- Namespace
- LfrlAnvil
- Assembly
- LfrlAnvil.Core.dll
A lightweight representation of a segment of string.
public readonly struct StringSegment : IEquatable<StringSegment>, IComparable<StringSegment>, IComparable, IReadOnlyList<char>, IReadOnlyCollection<char>, IEnumerable<char>, IEnumerable
- Implements
- Inherited Members
- Extension Methods
Constructors
StringSegment(string)
Creates a new StringSegment instance that contains the whole source
.
public StringSegment(string source)
Parameters
source
stringSource string.
StringSegment(string, int)
Creates a new StringSegment instance.
public StringSegment(string source, int startIndex)
Parameters
source
stringSource string.
startIndex
intPosition of the first character to include in the segment.
Exceptions
- ArgumentOutOfRangeException
When
startIndex
is less than 0.
StringSegment(string, int, int)
Creates a new StringSegment instance.
public StringSegment(string source, int startIndex, int length)
Parameters
source
stringSource string.
startIndex
intPosition of the first character to include in the segment.
length
intLength of the segment.
Exceptions
- ArgumentOutOfRangeException
When
startIndex
is less than 0 orlength
is less than 0.
Fields
Empty
An empty string segment.
public static readonly StringSegment Empty
Field Value
Properties
EndIndex
Position of the character right after the last character from the Source string included in this segment.
public int EndIndex { get; }
Property Value
this[int]
Gets the element at the specified index in the read-only list.
public char this[int index] { get; }
Parameters
index
intThe zero-based index of the element to get.
Property Value
- char
The element at the specified index in the read-only list.
Length
Length of this segment.
public int Length { get; }
Property Value
Source
Source string.
public string Source { get; }
Property Value
StartIndex
Position of the first character from the Source string included in this segment.
public int StartIndex { get; }
Property Value
Methods
AsMemory()
Creates a new ReadOnlyMemory<T> instance from this segment.
[Pure]
public ReadOnlyMemory<char> AsMemory()
Returns
- ReadOnlyMemory<char>
New ReadOnlyMemory<T> instance.
AsSpan()
Creates a new ReadOnlySpan<T> instance from this segment.
[Pure]
public ReadOnlySpan<char> AsSpan()
Returns
- ReadOnlySpan<char>
New ReadOnlySpan<T> instance.
CompareTo(StringSegment)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
[Pure]
public int CompareTo(StringSegment other)
Parameters
other
StringSegmentAn object to compare with this instance.
Returns
- int
A value that indicates the relative order of the objects being compared. The return value has these meanings:
Value Meaning Less than zero This instance precedes other
in the sort order.Zero This instance occurs in the same position in the sort order as other
.Greater than zero This instance follows other
in the sort order.
CompareTo(StringSegment, StringComparison)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
[Pure]
public int CompareTo(StringSegment other, StringComparison comparisonType)
Parameters
other
StringSegmentAn object to compare with this instance.
comparisonType
StringComparisonStringComparison to use.
Returns
- int
A value that indicates the relative order of the objects being compared.
CompareTo(object?)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
[Pure]
public int CompareTo(object? obj)
Parameters
obj
objectAn object to compare with this instance.
Returns
- int
A value that indicates the relative order of the objects being compared. The return value has these meanings:
Value Meaning Less than zero This instance precedes obj
in the sort order.Zero This instance occurs in the same position in the sort order as obj
.Greater than zero This instance follows obj
in the sort order.
Exceptions
- ArgumentException
obj
is not the same type as this instance.
Equals(StringSegment)
Indicates whether the current object is equal to another object of the same type.
[Pure]
public bool Equals(StringSegment other)
Parameters
other
StringSegmentAn object to compare with this object.
Returns
Equals(StringSegment, StringComparison)
Indicates whether the current object is equal to another object of the same type using provided comparisonType
.
[Pure]
public bool Equals(StringSegment other, StringComparison comparisonType)
Parameters
other
StringSegmentAn object to compare with this object.
comparisonType
StringComparisonStringComparison to use.
Returns
- bool
true if the current object is equal to the other parameter; otherwise, false.
Equals(object?)
Indicates whether this instance and a specified object are equal.
[Pure]
public override bool Equals(object? obj)
Parameters
obj
objectThe object to compare with the current instance.
Returns
- bool
true if
obj
and this instance are the same type and represent the same value; otherwise, false.
Expand(int)
Creates a new StringSegment instance by subtracting the specified count
from the StartIndex of this instance and adding it to its EndIndex at the same time,
increasing its Length.
[Pure]
public StringSegment Expand(int count)
Parameters
count
intNumber of characters to expand the new segment by.
Returns
- StringSegment
New StringSegment instance.
Exceptions
- ArgumentOutOfRangeException
When
count
is less than 0.
FromMemory(ReadOnlyMemory<char>)
Creates a new StringSegment instance from the provided ReadOnlyMemory<T>source
.
[Pure]
public static StringSegment FromMemory(ReadOnlyMemory<char> source)
Parameters
source
ReadOnlyMemory<char>Source read-only memory.
Returns
- StringSegment
New StringSegment instance
Remarks
GetEnumerator()
Returns an enumerator that iterates through the collection.
[Pure]
public IEnumerator<char> GetEnumerator()
Returns
- IEnumerator<char>
An enumerator that can be used to iterate through the collection.
GetHashCode()
Returns the hash code for this instance.
[Pure]
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Offset(int)
Creates a new StringSegment instance by adding the specified offset
to the StartIndex of this instance.
[Pure]
public StringSegment Offset(int offset)
Parameters
offset
intNumber of characters to offset the new segment by.
Returns
- StringSegment
New StringSegment instance with unchanged Length, if possible.
SetEndIndex(int)
Creates a new StringSegment instance with the provided EndIndexvalue
.
[Pure]
public StringSegment SetEndIndex(int value)
Parameters
value
intPosition of the character right after the last character from the Source string included in this segment.
Returns
- StringSegment
New StringSegment instance with unchanged Length, if possible.
Exceptions
- ArgumentOutOfRangeException
When
value
is less than 0.
SetLength(int)
Creates a new StringSegment instance with the provided Lengthvalue
.
[Pure]
public StringSegment SetLength(int value)
Parameters
value
intLength of the new segment.
Returns
- StringSegment
New StringSegment instance with unchanged StartIndex.
Exceptions
- ArgumentOutOfRangeException
When
value
is less than 0.
SetStartIndex(int)
Creates a new StringSegment instance with the provided StartIndexvalue
.
[Pure]
public StringSegment SetStartIndex(int value)
Parameters
value
intPosition of the first character of this segment to include in the new segment.
Returns
- StringSegment
New StringSegment instance with unchanged Length, if possible.
Exceptions
- ArgumentOutOfRangeException
When
value
is less than 0.
Shrink(int)
Creates a new StringSegment instance by adding the specified count
to the StartIndex of this instance and subtracting it from its EndIndex at the same time,
decreasing its Length.
[Pure]
public StringSegment Shrink(int count)
Parameters
count
intNumber of characters to shrink the new segment by.
Returns
- StringSegment
New StringSegment instance.
Exceptions
- ArgumentOutOfRangeException
When
count
is less than 0.
Slice(int)
Creates a new StringSegment instance.
[Pure]
public StringSegment Slice(int startIndex)
Parameters
startIndex
intPosition of the first character of this segment to include in the new segment.
Returns
- StringSegment
New StringSegment instance.
Exceptions
- ArgumentOutOfRangeException
When
startIndex
is less than 0.
Slice(int, int)
Creates a new StringSegment instance.
[Pure]
public StringSegment Slice(int startIndex, int length)
Parameters
startIndex
intPosition of the first character of this segment to include in the new segment.
length
intLength of the new segment.
Returns
- StringSegment
New StringSegment instance.
Exceptions
- ArgumentOutOfRangeException
When
startIndex
is less than 0 orlength
is less than 0.
ToString()
Returns a string representation of this StringSegment instance.
[Pure]
public override string ToString()
Returns
- string
String representation.
Operators
operator ==(StringSegment, StringSegment)
Checks if a
is equal to b
.
[Pure]
public static bool operator ==(StringSegment a, StringSegment b)
Parameters
a
StringSegmentFirst operand.
b
StringSegmentSecond operand.
Returns
- bool
true when operands are equal, otherwise false.
operator >(StringSegment, StringSegment)
Checks if a
is greater than b
.
[Pure]
public static bool operator >(StringSegment a, StringSegment b)
Parameters
a
StringSegmentFirst operand.
b
StringSegmentSecond operand.
Returns
- bool
true when
a
is greater thanb
, otherwise false.
operator >=(StringSegment, StringSegment)
Checks if a
is greater than or equal to b
.
[Pure]
public static bool operator >=(StringSegment a, StringSegment b)
Parameters
a
StringSegmentFirst operand.
b
StringSegmentSecond operand.
Returns
- bool
true when
a
is greater than or equal tob
, otherwise false.
implicit operator StringSegment(string)
Converts a strings
to StringSegment.
[Pure]
public static implicit operator StringSegment(string s)
Parameters
s
stringString to convert.
Returns
- StringSegment
New StringSegment instance.
operator !=(StringSegment, StringSegment)
Checks if a
is not equal to b
.
[Pure]
public static bool operator !=(StringSegment a, StringSegment b)
Parameters
a
StringSegmentFirst operand.
b
StringSegmentSecond operand.
Returns
- bool
true when operands are not equal, otherwise false.
operator <(StringSegment, StringSegment)
Checks if a
is less than b
.
[Pure]
public static bool operator <(StringSegment a, StringSegment b)
Parameters
a
StringSegmentFirst operand.
b
StringSegmentSecond operand.
Returns
- bool
true when
a
is less thanb
, otherwise false.
operator <=(StringSegment, StringSegment)
Checks if a
is less than or equal to b
.
[Pure]
public static bool operator <=(StringSegment a, StringSegment b)
Parameters
a
StringSegmentFirst operand.
b
StringSegmentSecond operand.
Returns
- bool
true when
a
is less than or equal tob
, otherwise false.