internal TraceParent()

in src/StreamJsonRpc/Protocol/TraceParent.cs [26:87]


        internal TraceParent(string? traceparent)
        {
            if (traceparent is null)
            {
                this.Version = 0;
                this.Flags = TraceFlags.None;
                return;
            }

            ReadOnlySpan<char> traceparentChars = traceparent.AsSpan();

            // Decode version
            ReadOnlySpan<char> slice = Consume(ref traceparentChars, VersionByteCount * 2);
            fixed (byte* pVersion = &this.Version)
            {
                Hex.Decode(slice, new Span<byte>(pVersion, 1));
            }

            ConsumeHyphen(ref traceparentChars);

            // Decode traceid
            slice = Consume(ref traceparentChars, TraceIdByteCount * 2);
            fixed (byte* pTraceId = this.TraceId)
            {
                Hex.Decode(slice, new Span<byte>(pTraceId, TraceIdByteCount));
            }

            ConsumeHyphen(ref traceparentChars);

            // Decode parentid
            slice = Consume(ref traceparentChars, ParentIdByteCount * 2);
            fixed (byte* pParentId = this.ParentId)
            {
                Hex.Decode(slice, new Span<byte>(pParentId, ParentIdByteCount));
            }

            ConsumeHyphen(ref traceparentChars);

            // Decode flags
            slice = Consume(ref traceparentChars, FlagsByteCount * 2);
            fixed (TraceFlags* pFlags = &this.Flags)
            {
                Hex.Decode(slice, new Span<byte>(pFlags, 1));
            }

            static void ConsumeHyphen(ref ReadOnlySpan<char> value)
            {
                if (value[0] != '-')
                {
                    Requires.Fail("Invalid format.");
                }

                value = value.Slice(1);
            }

            ReadOnlySpan<char> Consume(ref ReadOnlySpan<char> buffer, int length)
            {
                ReadOnlySpan<char> result = buffer.Slice(0, length);
                buffer = buffer.Slice(length);
                return result;
            }
        }