| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | |
| | | 3 | | namespace NGql.Core.Builders; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Ref struct for building paths using spans without allocations |
| | | 7 | | /// </summary> |
| | | 8 | | internal ref struct SpanPathBuilder(Span<char> buffer) |
| | | 9 | | { |
| | 4251 | 10 | | private Span<char> _buffer = buffer; |
| | 4251 | 11 | | private int _length = 0; |
| | | 12 | | |
| | | 13 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 14 | | public void Append(ReadOnlySpan<char> segment) |
| | | 15 | | { |
| | 11796 | 16 | | if (_length > 0) |
| | | 17 | | { |
| | 7545 | 18 | | if (_length >= _buffer.Length) |
| | 3 | 19 | | throw new InvalidOperationException($"SpanPathBuilder buffer overflow: cannot append separator, buffer l |
| | 7542 | 20 | | _buffer[_length++] = '.'; |
| | | 21 | | } |
| | | 22 | | |
| | 11793 | 23 | | if (segment.Length > 0) |
| | | 24 | | { |
| | 11793 | 25 | | if (_length + segment.Length > _buffer.Length) |
| | 6 | 26 | | throw new InvalidOperationException($"SpanPathBuilder buffer overflow: cannot append segment of length { |
| | 11787 | 27 | | segment.CopyTo(_buffer[_length..]); |
| | 11787 | 28 | | _length += segment.Length; |
| | | 29 | | } |
| | 11787 | 30 | | } |
| | | 31 | | |
| | | 32 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 9078 | 33 | | public readonly ReadOnlySpan<char> AsSpan() => _buffer[.._length]; |
| | | 34 | | |
| | | 35 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 12 | 36 | | public readonly override string ToString() => _buffer[.._length].ToString(); |
| | | 37 | | } |