コード例 #1
0
        public int ReadProtobufVarInt()
        {
            var availableBits = BitsInBuffer + (SLED * 8) - Offset;
            // Start by overflowingly reading 32 bits.
            // Reading beyond the buffer contents is safe in this case,
            // because the sled ensures that we stay inside of the buffer.
            var buf = PeekInt(32, true);

            // always take the first bytes; others if necessary
            var result = buf & MSK_1;

            BitStreamUtil.AssertMaxBits(availableBits, 1 * 8);
            if ((buf & MSB_1) != 0)
            {
                result |= (buf & MSK_2) >> 1;
                BitStreamUtil.AssertMaxBits(availableBits, 1 * 8);
                if ((buf & MSB_2) != 0)
                {
                    result |= (buf & MSK_3) >> 2;
                    BitStreamUtil.AssertMaxBits(availableBits, 2 * 8);
                    if ((buf & MSB_3) != 0)
                    {
                        result |= (buf & MSK_4) >> 3;
                        BitStreamUtil.AssertMaxBits(availableBits, 3 * 8);
                        if ((buf & MSB_4) != 0)
                        {
                            // dammit, it's too large (probably negative)
                            // fall back to the slow implementation, that's rare
                            return(BitStreamUtil.ReadProtobufVarIntStub(this));
                        }
                        if (TryAdvance(4 * 8))
                        {
                            RefillBuffer();
                        }
                    }
                    else if (TryAdvance(3 * 8))
                    {
                        RefillBuffer();
                    }
                }
                else if (TryAdvance(2 * 8))
                {
                    RefillBuffer();
                }
            }
            else if (TryAdvance(1 * 8))
            {
                RefillBuffer();
            }

            return(unchecked ((int)result));
        }
コード例 #2
0
 public int ReadProtobufVarInt()
 {
     return(BitStreamUtil.ReadProtobufVarIntStub(this));
 }