public static bool TryParse(ReadOnlySpan <byte> data, out IBObject bObject, bool strictDictionaryOrder = true)
        {
            var             reader = new Utf8BEncodingReader(data);
            List <IBObject> stack  = null;

            bObject = null;
            BString key = null;

            while (reader.Read())
            {
                IBObject value;
                switch (reader.TokenType)
                {
                case BEncodingTokenType.String:
                    value = new BString(reader.ValueSpan);
                    break;

                case BEncodingTokenType.Integer:
                    if (!reader.TryGet(out BigInteger integer))
                    {
                        return(false);
                    }

                    value = new BInteger(integer);
                    break;

                case BEncodingTokenType.End:
                    if (stack is null || !(key is null))
                    {
                        return(false);
                    }

                    stack[stack.Count - 1].SpanEnd = reader.Consumed;

                    if (stack.Count == 1)
                    {
                        return(reader.IsEmpty);
                    }

                    Debug.Assert(stack.Count != 0);

                    stack.RemoveAt(stack.Count - 1);
                    continue;

                default:
                    Debug.Assert(reader.TokenType == BEncodingTokenType.StartDictionary || reader.TokenType == BEncodingTokenType.StartList);
                    value = reader.TokenType == BEncodingTokenType.StartDictionary
                            ? (IBObject) new BDictionary()
                            : (IBObject) new BList();

                    if (stack is null)
                    {
                        Debug.Assert(key is null);
                        stack   = new List <IBObject>(4);
                        bObject = value;
                        stack.Add(bObject);
                        continue;
                    }
                    else
                    {
                        if (stack.Count == BEncodingConstants.DepthMax)
                        {
                            return(false);
                        }
                        break;
                    }
                }

                if (stack is null)
                {
                    Debug.Assert(bObject is null && key is null);
                    Debug.Assert(reader.TokenType != BEncodingTokenType.StartDictionary);
                    Debug.Assert(reader.TokenType != BEncodingTokenType.StartList);
                    bObject = value;
                    return(reader.IsEmpty);
                }

                if (stack.Count == 0)
                {
                    return(false);
                }

                var top = stack[^ 1];
Beispiel #2
0
 public bool TryGet(string key, out BInteger integer)
 {
     TryGet(key, out IBObject bObject);
     integer = bObject as BInteger;
     return(!(integer is null));
 }