예제 #1
0
        public static OperationStatus TryDecode(Stream stream, out VarLong result, out int bytes)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            bytes = 0;
            long count = 0;
            long b;

            do
            {
                if (bytes == MaxEncodedSize)
                {
                    result = default;
                    return(OperationStatus.InvalidData);
                }

                b = stream.ReadByte();
                if (b == -1)
                {
                    result = default;
                    return(OperationStatus.NeedMoreData);
                }

                count |= (b & 0x7F) << (bytes * 7);
                bytes++;
            }while ((b & 0x80) != 0);

            result = (VarLong)count;
            return(OperationStatus.Done);
        }
        public static OperationStatus TryDecode(Stream stream, out VarLong result, out int bytesConsumed)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            result        = default;
            bytesConsumed = 0;
            ulong count = 0;
            ulong b;

            do
            {
                if (bytesConsumed == MaxEncodedSize)
                {
                    return(OperationStatus.InvalidData);
                }
                else
                {
                    b = (ulong)stream.ReadByte();
                    if (b == ulong.MaxValue)
                    {
                        return(OperationStatus.NeedMoreData);
                    }
                }

                count |= (b & 0x7F) << (bytesConsumed * 7);
                bytesConsumed++;
            }while ((b & 0x80) != 0);

            result = (VarLong)count;
            return(OperationStatus.Done);
        }
        public static OperationStatus TryDecode(
            ReadOnlySpan <byte> source, out VarLong result, out int bytesConsumed)
        {
            result        = 0;
            bytesConsumed = 0;
            ulong value = 0;
            ulong b;

            do
            {
                if (bytesConsumed == MaxEncodedSize)
                {
                    return(OperationStatus.InvalidData);
                }
                else if (source.Length - bytesConsumed <= 0)
                {
                    return(OperationStatus.NeedMoreData);
                }

                b = source[bytesConsumed];

                value |= (b & 0x7F) << (bytesConsumed * 7);
                bytesConsumed++;
            }while ((b & 0x80) != 0);

            result = (VarLong)value;
            return(OperationStatus.Done);
        }
 public static TEnum AsEnum <TEnum>(this VarLong value)
     where TEnum : Enum
 {
     return(EnumConverter.ToEnum <TEnum>(value));
 }