Optimize GC for enumerating TargetString.

This commit is contained in:
2023-01-26 16:46:43 +08:00
parent 2304257201
commit 5e01b654bd

View File

@@ -76,17 +76,27 @@ namespace Cryville.Common.Buffers {
if (ev != null) ev.Invoke(); if (ev != null) ev.Invoke();
} }
IEnumerator IEnumerable.GetEnumerator() { /// <summary>
return GetEnumerator(); /// Returns an enumerator that iterates through the <see cref="TargetString" />.
/// </summary>
/// <returns>A <see cref="Enumerator" /> for the <see cref="TargetString" />.</returns>
public Enumerator GetEnumerator() {
return new Enumerator(this);
} }
public IEnumerator<char> GetEnumerator() { IEnumerator<char> IEnumerable<char>.GetEnumerator() {
return new Enumerator(this);
}
IEnumerator IEnumerable.GetEnumerator() {
return new Enumerator(this); return new Enumerator(this);
} }
class Enumerator : IEnumerator<char> { public struct Enumerator : IEnumerator<char> {
readonly TargetString _self; readonly TargetString _self;
int _index = -1; int _index;
public Enumerator(TargetString self) { _self = self; } internal Enumerator(TargetString self) {
_self = self;
_index = -1;
}
public char Current { public char Current {
get { get {