public void DefaultConstructor()
        {
            IndirectValue d = default;

            Assert.Null(d.LoadedValue);
            Assert.Throws <InvalidOperationException>(() => d.Fingerprint);
            Assert.Throws <ArgumentNullException>(() => d.GetValue(null));
            Assert.Throws <InvalidOperationException>(() => d.GetValue(_ => _list));
        }
        public void Constructors()
        {
            var loaded = new IndirectValue(_list);

            Assert.Equal(_list, loaded.LoadedValue);
            Assert.Equal(_list, loaded.GetValue(null));

            var unloaded = new IndirectValue(_dict.Fingerprint);

            Assert.Null(unloaded.LoadedValue);
            Assert.Equal(_dict.Fingerprint, unloaded.Fingerprint);
        }
Beispiel #3
0
        private IValue DecodeValue()
        {
            const byte e     = 0x65; // 'e'
            const byte indir = 0x2a; // '*'

            switch (ReadByte())
            {
            case null:
                throw new DecodingException(
                          $"The byte stream terminates unexpectedly at {_offset}."
                          );

            case 0x6e:     // 'n'
#pragma warning disable SA1129
                return(new Null());

#pragma warning restore SA1129

            case 0x74:     // 't'
                return(new Bencodex.Types.Boolean(true));

            case 0x66:     // 'f'
                return(new Bencodex.Types.Boolean(false));

            case 0x69:     // 'i'
                BigInteger integer = ReadDigits(true, e, BigInteger.Parse);
                return(new Integer(integer));

            case 0x75:     // 'u'
                return(ReadTextAfterPrefix());

            case 0x6c:     // 'l'
                var indirElements = new List <IndirectValue>();
                while (true)
                {
                    byte b = ReadByte() ?? throw new DecodingException(
                                       $"The byte stream terminates unexpectedly at {_offset}."
                                       );
                    if (b == e)
                    {
                        break;
                    }
                    else if (b == indir)
                    {
                        Fingerprint fp = DecodeFingerprint();
                        indirElements.Add(new IndirectValue(fp));
                        continue;
                    }

                    Back();
                    IValue element = DecodeValue();
                    indirElements.Add(new IndirectValue(element));
                }

                return(new Bencodex.Types.List(
                           indirElements.ToImmutableArray(),
                           _indirectValueLoader
                           ));

            case 0x64:     // 'd'
                var pairs = new List <KeyValuePair <IKey, IndirectValue> >();
                while (true)
                {
                    byte b = ReadByte() ?? throw new DecodingException(
                                       $"The byte stream terminates unexpectedly at {_offset}."
                                       );
                    if (b == e)
                    {
                        break;
                    }

                    Back();
                    IKey key = DecodeKey();
                    if (_indirectValueLoader is { })
                    {
                        b = ReadByte() ?? throw new DecodingException(
                                      $"The byte stream terminates unexpectedly at {_offset}."
                                      );
                        if (b == indir)
                        {
                            Fingerprint fp         = DecodeFingerprint();
                            var         indirValue = new IndirectValue(fp);
                            pairs.Add(new KeyValuePair <IKey, IndirectValue>(key, indirValue));
                            continue;
                        }

                        Back();
                    }

                    IValue value = DecodeValue();
                    pairs.Add(
                        new KeyValuePair <IKey, IndirectValue>(key, new IndirectValue(value))
                        );
                }