-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Builder can be used in foreach loops
- Loading branch information
1 parent
31e9533
commit fdde198
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/LinkDotNet.StringBuilder/ValueStringBuilder.Enumerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace LinkDotNet.StringBuilder; | ||
|
||
public ref partial struct ValueStringBuilder | ||
{ | ||
public readonly Enumerator GetEnumerator() => new(buffer[..bufferPosition]); | ||
|
||
/// <summary>Enumerates the elements of a <see cref="Span{T}"/>.</summary> | ||
[StructLayout(LayoutKind.Auto)] | ||
public ref struct Enumerator | ||
{ | ||
private readonly Span<char> span; | ||
private int index; | ||
|
||
/// <summary>Initializes a new instance of the <see cref="Enumerator"/> struct.</summary> | ||
/// <param name="span">The span to enumerate.</param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal Enumerator(Span<char> span) | ||
{ | ||
this.span = span; | ||
index = -1; | ||
} | ||
|
||
/// <summary>Gets the element at the current position of the enumerator.</summary> | ||
public ref char Current | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => ref span[index]; | ||
} | ||
|
||
/// <summary>Advances the enumerator to the next element of the span.</summary> | ||
/// <returns>True if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the span.</returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool MoveNext() | ||
{ | ||
var nextIndex = index + 1; | ||
if (nextIndex < span.Length) | ||
{ | ||
index = nextIndex; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters