@@ -61,36 +61,38 @@ public override string ToString()
6161 /// <summary>
6262 /// Gets the string segment that precedes this one.
6363 /// </summary>
64- public StringSegment Preceding ( )
65- => IsValid ? Create ( Source , 0 , Index ) : default ;
64+ /// <param name="includeSegment">When true, will include this segment.</param>
65+ public StringSegment Preceding ( bool includeSegment = false )
66+ => IsValid ? Create ( Source , 0 , includeSegment ? ( Index + Length ) : Index ) : default ;
6667
6768 /// <summary>
6869 /// Gets the string segment that follows this one.
6970 /// </summary>
70- public StringSegment Following ( )
71- => IsValid ? Create ( Source , Index + Length ) : default ;
71+ /// <param name="includeSegment">When true, will include this segment.</param>
72+ public StringSegment Following ( bool includeSegment = false )
73+ => IsValid ? Create ( Source , includeSegment ? Index : ( Index + Length ) ) : default ;
7274
7375 /// <inheritdoc cref="Preceding"/>
7476 /// <param name="maxCharacters">The max number of characters to get.</param>
75- public StringSegment Preceding ( int maxCharacters )
77+ public StringSegment Preceding ( int maxCharacters , bool includeSegment = false )
7678 {
7779 if ( maxCharacters < 0 ) throw new ArgumentOutOfRangeException ( nameof ( maxCharacters ) , maxCharacters , "Must be at least zero." ) ;
7880 if ( ! IsValid ) return default ;
79- if ( maxCharacters == 0 ) return new ( Source , Index , 0 ) ;
81+ if ( maxCharacters == 0 ) return includeSegment ? this : new ( Source , Index , 0 ) ;
8082 var start = Math . Max ( 0 , Index - maxCharacters ) ;
81- return new ( Source , start , Index - start ) ;
83+ return new ( Source , start , includeSegment ? ( Index - start + Length ) : ( Index - start ) ) ;
8284 }
8385
8486 /// <inheritdoc cref="Following"/>
8587 /// <param name="maxCharacters">The max number of characters to get.</param>
86- public StringSegment Following ( int maxCharacters )
88+ public StringSegment Following ( int maxCharacters , bool includeSegment = false )
8789 {
8890 if ( maxCharacters < 0 ) throw new ArgumentOutOfRangeException ( nameof ( maxCharacters ) , maxCharacters , "Must be at least zero." ) ;
8991 if ( ! IsValid ) return default ;
90- var start = Index + Length ;
91- if ( maxCharacters == 0 ) return new ( Source , start , 0 ) ;
92- var len = Source . Length ;
93- return new ( Source , start , Math . Min ( maxCharacters , len - start ) ) ;
92+ var start = includeSegment ? Index : ( Index + Length ) ;
93+ if ( maxCharacters == 0 ) return includeSegment ? this : new ( Source , start , 0 ) ;
94+ var len = Math . Min ( includeSegment ? ( maxCharacters + Length ) : maxCharacters , Source . Length - start ) ;
95+ return new ( Source , start , len ) ;
9496 }
9597
9698 /// <summary>
@@ -130,7 +132,11 @@ public StringSegment OffsetLength(int offset)
130132 /// </param>
131133 public StringSegment Slice ( int offset , int length , bool ignoreLengthBoundary = false )
132134 {
133- if ( ! IsValid ) return default ;
135+ if ( ! IsValid )
136+ {
137+ if ( offset == 0 && length == 0 ) return this ;
138+ throw new InvalidOperationException ( "Cannot slice a null value." ) ;
139+ }
134140 var newIndex = Index + offset ;
135141 if ( newIndex < 0 ) throw new ArgumentOutOfRangeException ( nameof ( offset ) , offset , "Cannot index less than the start of the string." ) ;
136142 var newEnd = newIndex + length ;
@@ -142,7 +148,7 @@ public StringSegment Slice(int offset, int length, bool ignoreLengthBoundary = f
142148 if ( newIndex > end ) throw new ArgumentOutOfRangeException ( nameof ( offset ) , offset , "Index is greater than the end of the source string." ) ;
143149 throw new ArgumentOutOfRangeException ( nameof ( length ) , length , "Desired length will extend greater than the end of the source string." ) ;
144150 }
145- return new ( Source , newIndex , end ) ;
151+ return new ( Source , newIndex , length ) ;
146152 }
147153 else
148154 {
@@ -152,7 +158,7 @@ public StringSegment Slice(int offset, int length, bool ignoreLengthBoundary = f
152158 if ( newIndex > end ) throw new ArgumentOutOfRangeException ( nameof ( offset ) , offset , "Index is greater than the length of the segment." ) ;
153159 throw new ArgumentOutOfRangeException ( nameof ( length ) , length , "Desired length will extend greater than the length of the segment." ) ;
154160 }
155- return new ( Source , newIndex , end ) ;
161+ return new ( Source , newIndex , length ) ;
156162 }
157163
158164 }
@@ -163,6 +169,21 @@ public StringSegment Slice(int offset, int length, bool ignoreLengthBoundary = f
163169 public bool Equals ( StringSegment other )
164170 => Index == other . Index & Length == other . Length && Source == other . Source ;
165171
172+
173+ /// <inheritdoc cref="string.Equals(string, StringComparison)">
174+ public bool Equals ( string ? other , StringComparison stringComparison = StringComparison . Ordinal )
175+ {
176+ if ( other is null ) return ! IsValid ;
177+ if ( other . Length != Length ) return false ;
178+ return AsSpan ( ) . Equals ( other , stringComparison ) ;
179+ }
180+
181+ public bool Equals ( in ReadOnlySpan < char > other , StringComparison stringComparison = StringComparison . Ordinal )
182+ {
183+ if ( other . Length != Length ) return false ;
184+ return AsSpan ( ) . Equals ( other , stringComparison ) ;
185+ }
186+
166187 /// <inheritdoc />
167188 public override bool Equals ( object ? obj )
168189 => obj is StringSegment segment && Equals ( segment ) ;
0 commit comments