Пример #1
0
        public static async Task TestNullTextReaderAsync(TextReader input)
        {
            var chars = new char[2];

            Assert.Equal(0, await input.ReadAsync(chars, 0, chars.Length));
            Assert.Equal(0, await input.ReadAsync(chars.AsMemory(), default));
            Assert.Equal(0, await input.ReadBlockAsync(chars, 0, chars.Length));
            Assert.Equal(0, await input.ReadBlockAsync(chars.AsMemory(), default));
            Assert.Null(await input.ReadLineAsync());
            Assert.Null(await input.ReadLineAsync(default));
Пример #2
0
        public ReadOnlyString GenerateFieldName()
        {
            const int maxLength   = 10;
            var       alphabet    = App.LatinAlphabet;
            int       targetIndex = alphabet.Length;

            int length    = 1;
            int lastIndex = maxLength - 1;

            var           nameBuffer = new char[maxLength];
            Span <int>    indices    = stackalloc int[maxLength];
            Memory <char> name;

            bool tryGet = true;

            do
            {
                for (int i = 0; i < length; i++)
                {
                    nameBuffer[nameBuffer.Length - 1 - i] = alphabet[indices[indices.Length - 1 - i]];
                }
                name = nameBuffer.AsMemory(nameBuffer.Length - length, length);

                if (!Expressions.ContainsKey(name))
                {
                    break;
                }

                indices[^ 1]++;
Пример #3
0
    /// <inheritdoc />
    public async Task WriteOutputAsync(Stream content, IOutputFormatterOptions?options = null, CancellationToken cancellationToken = default)
    {
        using var reader = new StreamReader(content);
        const int BUFFER_LENGTH = 4096;
        var       charsReceived = 0;

        do
        {
            var buffer = new char[BUFFER_LENGTH];
            charsReceived = await reader.ReadAsync(buffer.AsMemory(0, buffer.Length), cancellationToken);

            if (charsReceived == 0)
            {
                break;
            }
            _ansiConsole.Write(new string(buffer, 0, charsReceived));
        } while(charsReceived == BUFFER_LENGTH);
        _ansiConsole.WriteLine();
    }
Пример #4
0
        public static void Decode(
            TextReader input,
            Stream output,
            Func <ReadOnlyMemory <char>, Memory <byte> > decodeBufferFunc,
            int bufferSize = defaultBufferSize)
        {
            var buffer = new char[bufferSize];

            while (true)
            {
                int bytesRead = input.Read(buffer, 0, bufferSize);
                if (bytesRead < 1)
                {
                    break;
                }

                var result = decodeBufferFunc(buffer.AsMemory(0, bytesRead));
                output.Write(result.ToArray(), 0, result.Length);
            }
        }
Пример #5
0
        public static async Task DecodeAsync(
            TextReader input,
            Stream output,
            Func <ReadOnlyMemory <char>, Memory <byte> > decodeBufferFunc,
            int bufferSize = defaultBufferSize)
        {
            var buffer = new char[bufferSize];

            while (true)
            {
                int bytesRead = await input.ReadAsync(buffer, 0, bufferSize).ConfigureAwait(false);

                if (bytesRead < 1)
                {
                    break;
                }

                var result = decodeBufferFunc(buffer.AsMemory(0, bytesRead));
                await output.WriteAsync(result).ConfigureAwait(false);
            }
        }
 private ReadOnlyMemory <char> FinishWriteUtf16(ResizableMemory <byte> data)
 {
     try
     {
         var span = data.AsSpan();
         if (Encodings.Utf8.ToUtf16Length(span, out int bytes) != OperationStatus.Done)
         {
             throw new SerializationException("Failed to convert to UTF16");
         }
         var utf16 = new char[bytes / 2];
         if (Encodings.Utf8.ToUtf16(span, MemoryMarshal.AsBytes(utf16.AsSpan()), out _, out _) != OperationStatus.Done)
         {
             throw new SerializationException("Failed to convert to UTF16");
         }
         return(utf16.AsMemory());
     }
     finally
     {
         _pool.Return(data.Array);
     }
 }