Beispiel #1
0
        private static bool TryReadDurationFast(ref ProtoReader.State state, ref Duration value)
        {
            int offset       = state.OffsetInCurrent;
            var span         = state.Span;
            int prefixLength = state.ParseVarintUInt32(span, offset, out var len);

            offset += prefixLength;
            if (len == 0)
            {
                return(true);
            }

            if ((prefixLength + len) > state.RemainingInCurrent)
            {
                return(false);                                                 // don't have entire submessage
            }
            if (span[offset] != (1 << 3))
            {
                return(false);                          // expected field 1
            }
            var msgOffset = 1 + ProtoReader.State.TryParseUInt64Varint(span, 1 + offset, out var seconds);
            var nanos     = value.Nanoseconds;

            if (msgOffset < len)
            {
                if (span[msgOffset++ + offset] != (2 << 3))
                {
                    return(false);                                        // expected field 2
                }
                msgOffset += ProtoReader.State.TryParseUInt64Varint(span, msgOffset + offset, out var tmp);
                nanos      = (int)(long)tmp;
            }
            if (msgOffset != len)
            {
                return(false);                  // expected no more fields
            }
            state.Skip(prefixLength + (int)len);
            state.Advance(prefixLength + len);

            value = new Duration((long)seconds, nanos);
            return(true);
        }