public int Length => _pos;
+ public ref T this[int index]
+ {
+ get
+ {
+ Debug.Assert(index < _pos);
+ return ref _span[index];
+ }
+ }
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(T item)
{
_pos = 0;
}
- public int Length
+ public int Length => _pos;
+
+ public ref char this[int index]
{
- get => _pos;
- set
- {
- int delta = value - _pos;
- if (delta > 0)
- {
- Append('\0', delta);
- }
- else
- {
- _pos = value;
- }
+ get
+ {
+ Debug.Assert(index < _pos);
+ return ref _chars[index];
}
}
public override string ToString()
{
var s = new string(_chars.Slice(0, _pos));
- Clear();
+ Dispose();
return s;
}
if (_chars.Slice(0, _pos).TryCopyTo(destination))
{
charsWritten = _pos;
- Clear();
+ Dispose();
return true;
}
else
{
charsWritten = 0;
- Clear();
+ Dispose();
return false;
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void Clear()
+ public void Dispose()
{
char[] toReturn = _arrayToReturnToPool;
this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again