src/AnvilPacker/Data/IO/DataReader.cs
/// <summary> Reads a null-terminated UTF8 string. </summary>
Reads a null-terminated UTF8 string.
Reads a null - terminated UTF8 string .
public string ReadNulString()
        {
            Span<byte> buf = stackalloc byte[128];
            int pos = 0;
            if (_buf.Length < 16) {
                while (true) {
                    byte b = ReadByte();
                    if (b == 0) break;
                    EnsureCapacity(ref buf, pos, 1);
                    buf[pos++] = b;
                }
            } else {
                while (true) {
                    FillBuffer();
                    int bufRem = _bufLen - _bufPos;
                    int endIndex = _buf.AsSpan(_bufPos, bufRem).IndexOf((byte)0);
                    bool lastChunk = endIndex >= 0;
                    int chunkLen = lastChunk ? endIndex : bufRem;
                    var chunk = _buf.AsSpan(_bufPos, chunkLen);
                    _bufPos += chunkLen + (lastChunk ? 1 : 0);
                    if (lastChunk && pos == 0) {
                        buf = chunk;
                        pos = chunkLen;
                        break;
                    }
                    EnsureCapacity(ref buf, pos, chunkLen);
                    chunk.CopyTo(buf[pos..]);
                    pos += chunkLen;
                    if (lastChunk) break;
                }
            }
            return Encoding.UTF8.GetString(buf[0..pos]);
            static void EnsureCapacity(ref Span<byte> buf, int pos, int minCount)
            {
                if (pos + minCount > buf.Length) {
                    var newBuf = new byte[Math.Max(buf.Length + minCount + 64, buf.Length * 2)];
                    buf.CopyTo(newBuf);
                    buf = newBuf;
                }
            }
        }
public string ReadNulString ( ) { Span < byte > buf = stackalloc byte [ 128 ] ; int pos = 0 ; if ( _buf . Length < 16 ) { while ( true ) { byte b = ReadByte ( ) ; if ( b == 0 ) break ; EnsureCapacity ( ref buf , pos , 1 ) ; buf [ pos ++ ] = b ; } } else { while ( true ) { FillBuffer ( ) ; int bufRem = _bufLen - _bufPos ; int endIndex = _buf . AsSpan ( _bufPos , bufRem ) . IndexOf ( ( byte ) 0 ) ; bool lastChunk = endIndex >= 0 ; int chunkLen = lastChunk ? endIndex : bufRem ; var chunk = _buf . AsSpan ( _bufPos , chunkLen ) ; _bufPos += chunkLen + ( lastChunk ? 1 : 0 ) ; if ( lastChunk && pos == 0 ) { buf = chunk ; pos = chunkLen ; break ; } EnsureCapacity ( ref buf , pos , chunkLen ) ; chunk . CopyTo ( buf [ pos .. ] ) ; pos += chunkLen ; if ( lastChunk ) break ; } } return Encoding . UTF8 . GetString ( buf [ 0 .. pos ] ) ; static void EnsureCapacity ( ref Span < byte > buf , int pos , int minCount ) { if ( pos + minCount > buf . Length ) { var newBuf = new byte [ Math . Max ( buf . Length + minCount + 64 , buf . Length * 2 ) ] ; buf . CopyTo ( newBuf ) ; buf = newBuf ; } } }
Reads a null-terminated UTF8 string.
Reads a null - terminated UTF8 string .
//buffer is too small to read in chunks //whole string in buffer, no need of an extra copy