Exemple #1
0
        public void WriteValue(UInt64 value)
        {
            WriteStarted(InternalState.Value);

            if (!Utf8Formatter.TryFormat(value, m_output.GetSpan(), out var bytesWritten) &&
                !Utf8Formatter.TryFormat(value, m_output.GetSpan(Constants.Max64BitNumberSize), out bytesWritten))
            {
                ThrowFormatException(value);
            }

            m_output.Advance(bytesWritten);
        }
 public void Append(System.DateTimeOffset value, StandardFormat format)
 {
     if (!Utf8Formatter.TryFormat(value, buffer.AsSpan(index), out var written, format))
     {
         Grow();
         if (!Utf8Formatter.TryFormat(value, buffer.AsSpan(index), out written, format))
         {
             ThrowArgumentException(nameof(value));
         }
     }
     index += written;
 }
        public static bool TryWrite(ref ResizableMemory <byte> writer, DateTime value, StandardFormat standardFormat)
        {
            // TODO: Will this break in other locales?
            var data = writer.RequestSpan(31); // Fri, 31 Dec 9999 11:59:59 ACWST

            if (!Utf8Formatter.TryFormat(value, data, out int bytesWritten, standardFormat))
            {
                return(false);
            }
            writer.Advance(bytesWritten);
            return(true);
        }
Exemple #4
0
                public void Write(long value)
                {
                    const int MaxInt64Length = 20;

                    this.EnsureRemainingBufferSpace(MaxInt64Length);
                    if (!Utf8Formatter.TryFormat(value, this.Cursor.Span, out int bytesWritten))
                    {
                        throw new InvalidOperationException($"Failed to {nameof(this.Write)}({typeof(long).FullName}{value})");
                    }

                    this.Position += bytesWritten;
                }
 public void Append(System.Decimal value)
 {
     if (!Utf8Formatter.TryFormat(value, buffer.AsSpan(index), out var written))
     {
         Grow();
         if (!Utf8Formatter.TryFormat(value, buffer.AsSpan(index), out written))
         {
             ThrowArgumentException(nameof(value));
         }
     }
     index += written;
 }
Exemple #6
0
                public void Write(Guid value)
                {
                    const int GuidLength = 38;

                    this.EnsureRemainingBufferSpace(GuidLength);
                    if (!Utf8Formatter.TryFormat(value, this.Cursor, out int bytesWritten))
                    {
                        throw new InvalidOperationException($"Failed to {nameof(this.Write)}({typeof(double).FullName}{value})");
                    }

                    this.Position += bytesWritten;
                }
        public bool WriteStringDouble(double value)
        {
            Span <byte> span = this.writer.GetSpan(32);

            if (Utf8Formatter.TryFormat(value, span, out var written))
            {
                this.WriteSpan(span.Slice(0, written));
                return(true);
            }

            return(false);
        }
Exemple #8
0
        private static void EscapeNextBytes(byte value, Span <byte> destination, ref int written)
        {
            destination[written++] = (byte)'\\';
            switch (value)
            {
            case JsonConstants.Quote:
                // Optimize for the common quote case.
                destination[written++] = (byte)'u';
                destination[written++] = (byte)'0';
                destination[written++] = (byte)'0';
                destination[written++] = (byte)'2';
                destination[written++] = (byte)'2';
                break;

            case JsonConstants.LineFeed:
                destination[written++] = (byte)'n';
                break;

            case JsonConstants.CarriageReturn:
                destination[written++] = (byte)'r';
                break;

            case JsonConstants.Tab:
                destination[written++] = (byte)'t';
                break;

            case JsonConstants.BackSlash:
                destination[written++] = (byte)'\\';
                break;

            case JsonConstants.Slash:
                destination[written++] = (byte)'/';
                break;

            case JsonConstants.BackSpace:
                destination[written++] = (byte)'b';
                break;

            case JsonConstants.FormFeed:
                destination[written++] = (byte)'f';
                break;

            default:
                destination[written++] = (byte)'u';

                bool result = Utf8Formatter.TryFormat(value, destination.Slice(written), out int bytesWritten, format: s_hexStandardFormat);
                Debug.Assert(result);
                Debug.Assert(bytesWritten == 4);
                written += bytesWritten;
                break;
            }
        }
Exemple #9
0
        private async Task WriteResultHeaderAsync(
            Stream outputStream,
            int contentLength,
            CancellationToken cancellationToken)
        {
            byte[] buffer = ArrayPool <byte> .Shared.Rent(128);

            try
            {
                Utf8Formatter.TryFormat(contentLength, buffer, out var w);

                // Each part of the multipart response must start with --- and a CRLF
                await outputStream.WriteAsync(Start, 0, Start.Length, cancellationToken)
                .ConfigureAwait(false);

                await outputStream.WriteAsync(CrLf, 0, CrLf.Length, cancellationToken)
                .ConfigureAwait(false);

                // Each part of the multipart response must contain a Content-Type header.
                // Similar to the GraphQL specification this specification does not require
                // a specific serialization format. For consistency and ease of notation,
                // examples of the response are given in JSON throughout the spec.
                await outputStream.WriteAsync(
                    ContentType, 0, ContentType.Length, cancellationToken)
                .ConfigureAwait(false);

                await outputStream.WriteAsync(CrLf, 0, CrLf.Length, cancellationToken)
                .ConfigureAwait(false);

                // Each part of the multipart response must contain a Content-Length header.
                // This should be the number of bytes of the payload of the response.
                // It does not include the size of the headers, boundaries,
                // or CRLFs used to separate the content.
                await outputStream.WriteAsync(
                    ContentLength, 0, ContentLength.Length, cancellationToken)
                .ConfigureAwait(false);

                await outputStream.WriteAsync(buffer, 0, w, cancellationToken)
                .ConfigureAwait(false);

                await outputStream.WriteAsync(CrLf, 0, CrLf.Length, cancellationToken)
                .ConfigureAwait(false);

                // After all headers, an additional CRLF is sent.
                await outputStream.WriteAsync(CrLf, 0, CrLf.Length, cancellationToken)
                .ConfigureAwait(false);
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }
        }
        private void WriteStringIndented(ReadOnlySpan <byte> escapedPropertyName, DateTime value)
        {
            int indent = Indentation;

            Debug.Assert(indent <= 2 * JsonConstants.MaxWriterDepth);

            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatDateTimeOffsetLength - 7 - s_newLineLength);

            int minRequired = indent + escapedPropertyName.Length + JsonConstants.MaximumFormatDateTimeOffsetLength + 6; // 2 quotes for property name, 2 quotes for date, 1 colon, and 1 space
            int maxRequired = minRequired + 1 + s_newLineLength;                                                         // Optionally, 1 list separator and 1-2 bytes for new line

            if (_memory.Length - BytesPending < maxRequired)
            {
                Grow(maxRequired);
            }

            Span <byte> output = _memory.Span;

            if (_currentDepth < 0)
            {
                output[BytesPending++] = JsonConstants.ListSeparator;
            }

            if (_tokenType != JsonTokenType.None)
            {
                WriteNewLine(output);
            }

            JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
            BytesPending += indent;

            output[BytesPending++] = JsonConstants.Quote;

            escapedPropertyName.CopyTo(output.Slice(BytesPending));
            BytesPending += escapedPropertyName.Length;

            output[BytesPending++] = JsonConstants.Quote;
            output[BytesPending++] = JsonConstants.KeyValueSeperator;
            output[BytesPending++] = JsonConstants.Space;

            output[BytesPending++] = JsonConstants.Quote;

            Span <byte> tempSpan = stackalloc byte[JsonConstants.MaximumFormatDateTimeOffsetLength];
            bool        result   = Utf8Formatter.TryFormat(value, tempSpan, out int bytesWritten, s_dateTimeStandardFormat);

            Debug.Assert(result);
            JsonWriterHelper.TrimDateTimeOffset(tempSpan.Slice(0, bytesWritten), out bytesWritten);
            tempSpan.Slice(0, bytesWritten).CopyTo(output.Slice(BytesPending));
            BytesPending += bytesWritten;

            output[BytesPending++] = JsonConstants.Quote;
        }
Exemple #11
0
        private void WriteValueFast(decimal value)
        {
            // Calculated based on the following: ',number'
            int bytesNeeded = 1 + JsonConstants.MaximumDecimalLength;

            Span <byte> byteBuffer = WriteValue(bytesNeeded, out int idx);
            bool        result     = Utf8Formatter.TryFormat(value, byteBuffer.Slice(idx), out int bytesWritten);

            Debug.Assert(result);
            idx += bytesWritten;

            Advance(idx);
        }
 public bool TryWriteLine(DateTime date, StandardFormat format)
 {
     if (!Utf8Formatter.TryFormat(date, Free, out int written, format))
     {
         return(false);
     }
     if (!NewLine.TryCopyTo(Free.Slice(written)))
     {
         return(false);
     }
     _written += written + NewLine.Length;
     return(true);
 }
Exemple #13
0
        public IntValueNode(Location?location, uint value)
        {
            Location   = location;
            _uIntValue = value;

            Span <byte> buffer = stackalloc byte[32];

            Utf8Formatter.TryFormat(value, buffer, out int written);
            var memory = new Memory <byte>(new byte[written]);

            buffer.Slice(0, written).CopyTo(memory.Span);
            _memory = memory;
        }
Exemple #14
0
        private void WriteStringIndented(ReadOnlySpan <byte> escapedPropertyName, Guid value)
        {
            int indent = Indentation;

            Debug.Assert(indent <= 2 * _options.MaxDepth);

            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatGuidLength - 7 - s_newLineLength);

            int minRequired = indent + escapedPropertyName.Length + JsonConstants.MaximumFormatGuidLength + 6; // 2 quotes for property name, 2 quotes for date, 1 colon, and 1 space
            int maxRequired = minRequired + 1 + s_newLineLength;                                               // Optionally, 1 list separator and 1-2 bytes for new line

            if (_memory.Length - BytesPending < maxRequired)
            {
                Grow(maxRequired);
            }

            Span <byte> output = _memory.Span;

            if (_currentDepth < 0)
            {
                output[BytesPending++] = JsonConstants.ListSeparator;
            }

            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

            if (_tokenType != JsonTokenType.None)
            {
                WriteNewLine(output);
            }

            JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
            BytesPending += indent;

            output[BytesPending++] = JsonConstants.Quote;

            escapedPropertyName.CopyTo(output.Slice(BytesPending));
            BytesPending += escapedPropertyName.Length;

            output[BytesPending++] = JsonConstants.Quote;
            output[BytesPending++] = JsonConstants.KeyValueSeparator;
            output[BytesPending++] = JsonConstants.Space;

            output[BytesPending++] = JsonConstants.Quote;

            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);

            Debug.Assert(result);
            BytesPending += bytesWritten;

            output[BytesPending++] = JsonConstants.Quote;
        }
Exemple #15
0
        private void WriteStringIndented(ReadOnlySpan <char> escapedPropertyName, Guid value)
        {
            int indent = Indentation;

            Debug.Assert(indent <= 2 * _options.MaxDepth);

            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - JsonConstants.MaximumFormatGuidLength - 7 - s_newLineLength);

            // All ASCII, 2 quotes for property name, 2 quotes for date, 1 colon, and 1 space => escapedPropertyName.Length + JsonConstants.MaximumFormatGuidLength + 6
            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + JsonConstants.MaximumFormatGuidLength + 7 + s_newLineLength;

            if (_memory.Length - BytesPending < maxRequired)
            {
                Grow(maxRequired);
            }

            Span <byte> output = _memory.Span;

            if (_currentDepth < 0)
            {
                output[BytesPending++] = JsonConstants.ListSeparator;
            }

            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

            if (_tokenType != JsonTokenType.None)
            {
                WriteNewLine(output);
            }

            JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
            BytesPending += indent;

            output[BytesPending++] = JsonConstants.Quote;

            TranscodeAndWrite(escapedPropertyName, output);

            output[BytesPending++] = JsonConstants.Quote;
            output[BytesPending++] = JsonConstants.KeyValueSeparator;
            output[BytesPending++] = JsonConstants.Space;

            output[BytesPending++] = JsonConstants.Quote;

            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);

            Debug.Assert(result);
            BytesPending += bytesWritten;

            output[BytesPending++] = JsonConstants.Quote;
        }
Exemple #16
0
        public uint Get(Composition <DateTimeOffset> parameter)
        {
            Span <byte> span   = stackalloc byte[33];
            var         format = Utf8Formatter.TryFormat(parameter.Instance, span, out var count, _format);

            if (format)
            {
                var result = _trimmer.Get(span.Slice(0, count));
                span.Slice(0, (int)result).CopyTo(parameter.View);
                return(result);
            }

            throw new InvalidOperationException($"Could not format '{parameter.Instance}' into its UTF-8 equivalent.");
        }
Exemple #17
0
        public void WriteValue(Guid value)
        {
            WriteStarted(InternalState.Value);
            WriteUTFByte(Constants.Quote);

            if (!Utf8Formatter.TryFormat(value, m_output.GetSpan(Constants.MaxGuidSize), out var bytesWritten))
            {
                ThrowFormatException(value);
            }

            m_output.Advance(bytesWritten);

            WriteUTFByte(Constants.Quote);
        }
Exemple #18
0
        internal static GrainType CreateGrainServiceGrainType(int typeCode, string grainSystemId)
        {
            var extraLen = grainSystemId is null ? 0 : Encoding.UTF8.GetByteCount(grainSystemId);
            var buf      = new byte[GrainTypePrefix.GrainServicePrefix.Length + 8 + extraLen];

            GrainTypePrefix.GrainServicePrefixBytes.Span.CopyTo(buf);
            Utf8Formatter.TryFormat(typeCode, buf.AsSpan(GrainTypePrefix.GrainServicePrefix.Length), out var len, new StandardFormat('X', 8));
            Debug.Assert(len == 8);
            if (grainSystemId != null)
            {
                Encoding.UTF8.GetBytes(grainSystemId, 0, grainSystemId.Length, buf, buf.Length - extraLen);
            }
            return(new GrainType(buf));
        }
Exemple #19
0
 private static void SetDateValues(DateTimeOffset value)
 {
     lock (s_headerBytesScratch)
     {
         if (!Utf8Formatter.TryFormat(value, s_headerBytesScratch.AsSpan(prefixLength), out var written, 'R'))
         {
             throw new Exception("date time format failed");
         }
         Debug.Assert(written == dateTimeRLength);
         var temp = s_headerBytesMaster;
         s_headerBytesMaster  = s_headerBytesScratch;
         s_headerBytesScratch = temp;
     }
 }
Exemple #20
0
        public async Task <ExecutionOutputResult> ExecuteInContainerAsync(
            ActiveContainer container,
            byte[] assemblyBytes,
            byte[] outputBufferBytes,
            bool includePerformance,
            CancellationToken cancellationToken
            )
        {
            var outputStartMarker = Guid.NewGuid();
            var outputEndMarker   = Guid.NewGuid();
            await _stdinWriter.WriteCommandAsync(container.Stream, new ExecuteCommand(
                                                     assemblyBytes,
                                                     outputStartMarker,
                                                     outputEndMarker,
                                                     includePerformance
                                                     ), cancellationToken);

            const int OutputMarkerLength = 36; // length of guid

            byte[]? outputStartMarkerBytes = null;
            byte[]? outputEndMarkerBytes   = null;
            try {
                outputStartMarkerBytes = ArrayPool <byte> .Shared.Rent(OutputMarkerLength);

                outputEndMarkerBytes = ArrayPool <byte> .Shared.Rent(OutputMarkerLength);

                Utf8Formatter.TryFormat(outputStartMarker, outputStartMarkerBytes, out _);
                Utf8Formatter.TryFormat(outputEndMarker, outputEndMarkerBytes, out _);

                using var executionCancellation = CancellationFactory.ContainerExecution(cancellationToken);
                return(await _stdoutReader.ReadOutputAsync(
                           container.Stream,
                           outputBufferBytes,
                           outputStartMarkerBytes.AsMemory(0, OutputMarkerLength),
                           outputEndMarkerBytes.AsMemory(0, OutputMarkerLength),
                           executionCancellation.Token
                           ));
            }
            finally {
                if (outputStartMarkerBytes != null)
                {
                    ArrayPool <byte> .Shared.Return(outputStartMarkerBytes);
                }
                if (outputEndMarkerBytes != null)
                {
                    ArrayPool <byte> .Shared.Return(outputEndMarkerBytes);
                }
            }
        }
        private static bool WriteUnsignedInteger(ulong value, Span <byte> span, out int written)
        {
            if (Utf8Formatter.TryFormat(value, span, out int formatted))
            {
                if (span.Length > formatted)
                {
                    span[formatted] = IntegerSuffix;
                    written         = formatted + 1;
                    return(true);
                }
            }

            written = 0;
            return(false);
        }
Exemple #22
0
        public static void FillIntegerUtf8Array(byte[] array, int minValue, int maxValue, int seed = 42)
        {
            Random r = new Random(seed);

            Span <byte> span = new Span <byte>(array);

            // Generate ints across the entire range
            int next = r.Next(minValue + 1, maxValue) + r.Next(-1, 2);

            while (Utf8Formatter.TryFormat(next, span, out int written) && span.Length > written)
            {
                next = r.Next(minValue + 1, maxValue) + r.Next(-1, 2);
                span = span.Slice(written + 1);
            }
        }
Exemple #23
0
        public void Write_Type_Name(byte type, int intName)
        {
            var buffer = ArrayPool <byte> .Shared.Rent(10);

            try
            {
                _ = Utf8Formatter.TryFormat(intName, buffer, out int written);
                WriteByte(type);
                WriteCString(buffer.AsSpan(0, written));
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }
        }
Exemple #24
0
 public static bool TryFormat(sbyte value, Span <byte> buffer, out int bytesWritten, StandardFormat format = default, SymbolTable symbolTable = null)
 {
     if (symbolTable == null || symbolTable == SymbolTable.InvariantUtf8)
     {
         return(Utf8Formatter.TryFormat(value, buffer, out bytesWritten, format));
     }
     else if (symbolTable == SymbolTable.InvariantUtf16)
     {
         return(Utf16Formatter.TryFormat(value, buffer, out bytesWritten, format));
     }
     else
     {
         return(TryFormatInt64(value, 0xff, buffer, out bytesWritten, format, symbolTable));
     }
 }
Exemple #25
0
 public static bool TryFormat(Guid value, Span <byte> buffer, out int bytesWritten, StandardFormat format = default, SymbolTable symbolTable = null)
 {
     if (symbolTable == null || symbolTable == SymbolTable.InvariantUtf8)
     {
         return(Utf8Formatter.TryFormat(value, buffer, out bytesWritten, format));
     }
     else if (symbolTable == SymbolTable.InvariantUtf16)
     {
         return(Utf16Formatter.TryFormat(value, buffer, out bytesWritten, format));
     }
     else
     {
         throw new NotSupportedException();
     }
 }
            public static void WriteDateTimeOffset(Utf8JsonWriter writer, DateTimeOffset value)
            {
                long     unixTime  = Convert.ToInt64((value - s_Epoch).TotalMilliseconds);
                TimeSpan utcOffset = value.Offset;

                int stackSize = 64;

                while (true)
                {
                    Span <byte> span = stackSize <= 1024 ? stackalloc byte[stackSize] : new byte[stackSize];

                    if (!Utf8Formatter.TryFormat(unixTime, span.Slice(7), out int bytesWritten, new StandardFormat('D')) ||
                        stackSize < 15 + bytesWritten)
                    {
                        stackSize *= 2;
                        continue;
                    }

                    JsonMicrosoftDateTimeConverter.Start.CopyTo(span);
                    span[7 + bytesWritten] = utcOffset >= TimeSpan.Zero ? 0x2B : 0x2D;

                    int hours = Math.Abs(utcOffset.Hours);
                    if (hours < 10)
                    {
                        span[7 + bytesWritten + 1] = 0x30;
                        span[7 + bytesWritten + 2] = (byte)(0x30 + hours);
                    }
                    else
                    {
                        Utf8Formatter.TryFormat(hours, span.Slice(7 + bytesWritten + 1), out _, new StandardFormat('D'));
                    }
                    int minutes = Math.Abs(utcOffset.Minutes);
                    if (minutes < 10)
                    {
                        span[7 + bytesWritten + 3] = 0x30;
                        span[7 + bytesWritten + 4] = (byte)(0x30 + minutes);
                    }
                    else
                    {
                        Utf8Formatter.TryFormat(minutes, span.Slice(7 + bytesWritten + 3), out _, new StandardFormat('D'));
                    }
                    JsonMicrosoftDateTimeConverter.End.CopyTo(span.Slice(7 + bytesWritten + 5));

                    writer.WriteStringValue(
                        JsonMicrosoftDateTimeConverter.CreateJsonEncodedTextFunc(span.Slice(0, 15 + bytesWritten).ToArray()));
                    break;
                }
            }
Exemple #27
0
        public static bool TryWriteGuid(Span <byte> name, Guid value, Span <byte> target, out int bytesWritten)
        {
            if (!TryWriteName(name, ref target, out bytesWritten))
            {
                return(false);
            }

            if (Utf8Formatter.TryFormat(value, target, out bytesWritten, 'N'))
            {
                bytesWritten += name.Length + 2;
                return(true);
            }

            bytesWritten = 0;
            return(false);
        }
Exemple #28
0
        private void WriteValueFast(Guid value)
        {
            // Calculated based on the following: ',"value"'
            int bytesNeeded = 3 + JsonConstants.MaximumGuidLength;

            Span <byte> byteBuffer = WriteValue(bytesNeeded, out int idx);

            byteBuffer[idx++] = JsonConstants.Quote;
            bool result = Utf8Formatter.TryFormat(value, byteBuffer.Slice(idx), out int bytesWritten);

            Debug.Assert(result);
            idx += bytesWritten;
            byteBuffer[idx++] = JsonConstants.Quote;

            Advance(idx);
        }
Exemple #29
0
        public static Writer Make(int index, NameWriter name)
        {
            return((reader, buffer) =>
            {
                if (reader.IsDBNull(index))
                {
                    return buffer;
                }

                Comma.Write(ref buffer);
                name.Write(ref buffer);

                Utf8Formatter.TryFormat(reader.GetByte(index), buffer, out var n);
                return buffer.Slice(n);
            });
        }
        internal static Result GetSubFilePath(Span <byte> subFilePathBuffer, ReadOnlySpan <byte> basePath, int index)
        {
            int basePathLen = StringUtils.Copy(subFilePathBuffer, basePath);

            // Make sure we have at least 3 bytes for the sub file name
            if (basePathLen + 3 > PathTools.MaxPathLength)
            {
                return(ResultFs.TooLongPath.Log());
            }

            subFilePathBuffer[basePathLen] = StringTraits.DirectorySeparator;

            Utf8Formatter.TryFormat(index, subFilePathBuffer.Slice(basePathLen + 1), out _, new StandardFormat('D', 2));

            return(Result.Success);
        }