Beispiel #1
0
        public static void BitStackPushPop(int bitLength)
        {
            BitStack bitStack = default;

            Assert.Equal(0, bitStack.CurrentDepth);

            var values = new bool[bitLength];

            for (int i = 0; i < bitLength; i++)
            {
                values[i] = s_random.NextDouble() >= 0.5;
            }

            for (int i = 0; i < bitLength; i++)
            {
                if (values[i])
                {
                    bitStack.PushTrue();
                }
                else
                {
                    bitStack.PushFalse();
                }
                Assert.Equal(i + 1, bitStack.CurrentDepth);
            }

            // Loop backwards when popping.
            for (int i = bitLength - 1; i > 0; i--)
            {
                // We need the value at the top *after* popping off the last one.
                Assert.Equal(values[i - 1], bitStack.Pop());
                Assert.Equal(i, bitStack.CurrentDepth);
            }
        }
Beispiel #2
0
 void Join()
 {
     if (count > 0 && BitStack.Peek(ref groupUsage))
     {
         buffer.Append(BitStack.Peek(ref groupOperators) ? AndString : OrString);
     }
 }
Beispiel #3
0
        public static void SetResetFirstBit()
        {
            BitStack bitStack = default;

            Assert.Equal(0, bitStack.CurrentDepth);
            bitStack.SetFirstBit();
            Assert.Equal(1, bitStack.CurrentDepth);
            Assert.False(bitStack.Pop());
            Assert.Equal(0, bitStack.CurrentDepth);

            bitStack = default;
            Assert.Equal(0, bitStack.CurrentDepth);
            bitStack.ResetFirstBit();
            Assert.Equal(1, bitStack.CurrentDepth);
            Assert.False(bitStack.Pop());
            Assert.Equal(0, bitStack.CurrentDepth);

            bitStack = default;
            Assert.Equal(0, bitStack.CurrentDepth);
            bitStack.SetFirstBit();
            Assert.Equal(1, bitStack.CurrentDepth);
            Assert.False(bitStack.Pop());
            Assert.Equal(0, bitStack.CurrentDepth);
            bitStack.ResetFirstBit();
            Assert.Equal(1, bitStack.CurrentDepth);
            Assert.False(bitStack.Pop());
            Assert.Equal(0, bitStack.CurrentDepth);
        }
Beispiel #4
0
        /// <summary>
        /// Constructs a new <see cref="JsonReaderState"/> instance.
        /// </summary>
        /// <param name="maxDepth">Sets the maximum depth allowed when reading JSON, with the default set as 64.
        /// Reading past this depth will throw a <exception cref="JsonReaderException"/>.</param>
        /// <param name="options">Defines the customized behavior of the <see cref="Utf8JsonReader"/>
        /// that is different from the JSON RFC (for example how to handle comments).
        /// By default, the <see cref="Utf8JsonReader"/> follows the JSON RFC strictly (i.e. comments within the JSON are invalid).</param>
        /// <exception cref="ArgumentException">
        /// Thrown when the max depth is set to a non-positive value (&lt;= 0)
        /// </exception>
        /// <remarks>
        /// An instance of this state must be passed to the <see cref="Utf8JsonReader"/> ctor with the JSON data.
        /// Unlike the <see cref="Utf8JsonReader"/>, which is a ref struct, the state can survive
        /// across async/await boundaries and hence this type is required to provide support for reading
        /// in more data asynchronously before continuing with a new instance of the <see cref="Utf8JsonReader"/>.
        /// </remarks>
        public JsonReaderState(int maxDepth = DefaultMaxDepth, JsonReaderOptions options = default)
        {
            if (maxDepth <= 0)
            {
                throw ThrowHelper.GetArgumentException_MaxDepthMustBePositive();
            }

            _lineNumber         = default;
            _bytePositionInLine = default;
            _bytesConsumed      = default;
            _maxDepth           = maxDepth;
            _inObject           = default;
            _isNotPrimitive     = default;
            _numberFormat       = default;
            _stringHasEscaping  = default;
            _tokenType          = default;
            _previousTokenType  = default;
            _readerOptions      = options;

            // Only allocate if the user reads a JSON payload beyond the depth that the _allocationFreeContainer can handle.
            // This way we avoid allocations in the common, default cases, and allocate lazily.
            _bitStack = default;

            _sequencePosition = default;
        }
Beispiel #5
0
 public AnsState(int capacity, ReversedMemoryStream outputStream, uint initialState)
 {
     Underlying = initialState;
     Capacity   = capacity;
     //Contained = Capacity;
     Output   = outputStream;
     BitStack = new BitStack();
 }
Beispiel #6
0
        void OpenGroup(bool value)
        {
            count++;
            Join();

            BitStack.Push(ref groupOperators);
            BitStack.SetCurrent(ref groupOperators, value);
            BitStack.Push(ref groupUsage);

            buffer.Append('(');
        }
        private WhitespaceRuleReader(XmlReader baseReader, WhitespaceRuleLookup wsRules) : base(baseReader)
        {
            Debug.Assert(wsRules != null);

            _val              = null;
            _stkStrip         = new BitStack();
            _shouldStrip      = false;
            _preserveAdjacent = false;

            _wsRules = wsRules;
            _wsRules.Atomize(baseReader.NameTable);
        }
Beispiel #8
0
        public JsonWriterState(JsonWriterOptions options = default)
        {
            _bytesWritten   = default;
            _bytesCommitted = default;
            _inObject       = default;
            _isNotPrimitive = default;
            _tokenType      = default;
            _writerOptions  = options;

            // Only allocate if the user writes a JSON payload beyond the depth that the _allocationFreeContainer can handle.
            // This way we avoid allocations in the common, default cases, and allocate lazily.
            _bitStack = default;
        }
Beispiel #9
0
        /// <summary>
        /// Constructs a new <see cref="JsonReaderState"/> instance.
        /// </summary>
        /// <param name="options">Defines the customized behavior of the <see cref="Utf8JsonReader"/>
        /// that is different from the JSON RFC (for example how to handle comments or maximum depth allowed when reading).
        /// By default, the <see cref="Utf8JsonReader"/> follows the JSON RFC strictly (i.e. comments within the JSON are invalid) and reads up to a maximum depth of 64.</param>
        /// <remarks>
        /// An instance of this state must be passed to the <see cref="Utf8JsonReader"/> ctor with the JSON data.
        /// Unlike the <see cref="Utf8JsonReader"/>, which is a ref struct, the state can survive
        /// across async/await boundaries and hence this type is required to provide support for reading
        /// in more data asynchronously before continuing with a new instance of the <see cref="Utf8JsonReader"/>.
        /// </remarks>
        public JsonReaderState(JsonReaderOptions options = default)
        {
            _lineNumber                 = default;
            _bytePositionInLine         = default;
            _inObject                   = default;
            _isNotPrimitive             = default;
            _valueIsEscaped             = default;
            _trailingCommaBeforeComment = default;
            _tokenType                  = default;
            _previousTokenType          = default;
            _readerOptions              = options;

            // Only allocate if the user reads a JSON payload beyond the depth that the _allocationFreeContainer can handle.
            // This way we avoid allocations in the common, default cases, and allocate lazily.
            _bitStack = default;
        }
Beispiel #10
0
        public void Condition(string condition, params string[] others)
        {
            Join();

            BitStack.SetCurrent(ref groupUsage, true);

            buffer.Append(condition);
            if (others != null)
            {
                foreach (var other in others)
                {
                    buffer.Append(other);
                }
            }

            lastGoodClose = buffer.Length;
        }
Beispiel #11
0
        /// <summary>
        /// Constructs a new <see cref="JsonReaderState"/> instance.
        /// </summary>
        /// <param name="options">Defines the customized behavior of the <see cref="Utf8JsonReader"/>
        /// that is different from the JSON RFC (for example how to handle comments or maximum depth allowed when reading).
        /// By default, the <see cref="Utf8JsonReader"/> follows the JSON RFC strictly (i.e. comments within the JSON are invalid) and reads up to a maximum depth of 64.</param>
        /// <exception cref="ArgumentException">
        /// Thrown when the max depth is set to a non-positive value (&lt; 0)
        /// </exception>
        /// <remarks>
        /// An instance of this state must be passed to the <see cref="Utf8JsonReader"/> ctor with the JSON data.
        /// Unlike the <see cref="Utf8JsonReader"/>, which is a ref struct, the state can survive
        /// across async/await boundaries and hence this type is required to provide support for reading
        /// in more data asynchronously before continuing with a new instance of the <see cref="Utf8JsonReader"/>.
        /// </remarks>
        public JsonReaderState(JsonReaderOptions options = default)
        {
            _lineNumber         = default;
            _bytePositionInLine = default;
            _bytesConsumed      = default;
            _inObject           = default;
            _isNotPrimitive     = default;
            _numberFormat       = default;
            _stringHasEscaping  = default;
            _tokenType          = default;
            _previousTokenType  = default;
            _readerOptions      = options;

            // Only allocate if the user reads a JSON payload beyond the depth that the _allocationFreeContainer can handle.
            // This way we avoid allocations in the common, default cases, and allocate lazily.
            _bitStack = default;

            _sequencePosition = default;
        }
        public JsonWriterState(int maxDepth = DefaultMaxDepth, JsonWriterOptions options = default)
        {
            //TODO: Use ThrowHelper: GetArgumentException_MaxDepthMustBePositive
            if (maxDepth <= 0)
            {
                throw new ArgumentException("max depth must be positive");
            }

            _bytesWritten   = default;
            _bytesCommitted = default;
            _maxDepth       = maxDepth;
            _inObject       = default;
            _isNotPrimitive = default;
            _tokenType      = default;
            _writerOptions  = options;

            // Only allocate if the user writes a JSON payload beyond the depth that the _allocationFreeContainer can handle.
            // This way we avoid allocations in the common, default cases, and allocate lazily.
            _bitStack = default;
        }
Beispiel #13
0
        public void Pop()
        {
            count--;
            if (BitStack.Pop(ref groupUsage))
            {
                buffer.Append(')');
                lastGoodClose = buffer.Length;

                if (count > 0)
                {
                    BitStack.SetCurrent(ref groupUsage, true);
                }
            }
            else if (lastGoodClose > 0)
            {
                buffer.Length = lastGoodClose;
            }
            else
            {
                buffer.Length--;
            }

            BitStack.Pop(ref groupOperators);
        }
Beispiel #14
0
        [InlineData(int.MaxValue / 32 + 1)]    // 67_108_864
        public static void BitStackPushPopLarge(int bitLength)
        {
            BitStack bitStack = default;

            Assert.Equal(0, bitStack.CurrentDepth);

            var values = new bool[bitLength];

            for (int i = 0; i < bitLength; i++)
            {
                values[i] = s_random.NextDouble() >= 0.5;
            }

            const int IterationCapacity = 1_600_000;

            int expectedDepth = 0;

            // Only set and compare the first and last few (otherwise, the test takes too long)
            for (int i = 0; i < IterationCapacity; i++)
            {
                if (values[i])
                {
                    bitStack.PushTrue();
                }
                else
                {
                    bitStack.PushFalse();
                }
                expectedDepth++;
                Assert.Equal(expectedDepth, bitStack.CurrentDepth);
            }
            for (int i = bitLength - IterationCapacity; i < bitLength; i++)
            {
                if (values[i])
                {
                    bitStack.PushTrue();
                }
                else
                {
                    bitStack.PushFalse();
                }
                expectedDepth++;
                Assert.Equal(expectedDepth, bitStack.CurrentDepth);
            }

            Assert.Equal(expectedDepth, IterationCapacity * 2);

            // Loop backwards when popping.
            for (int i = bitLength - 1; i >= bitLength - IterationCapacity; i--)
            {
                // We need the value at the top *after* popping off the last one.
                Assert.Equal(values[i - 1], bitStack.Pop());

                expectedDepth--;
                Assert.Equal(expectedDepth, bitStack.CurrentDepth);
            }
            for (int i = IterationCapacity - 1; i > 0; i--)
            {
                // We need the value at the top *after* popping off the last one.
                Assert.Equal(values[i - 1], bitStack.Pop());

                expectedDepth--;
                Assert.Equal(expectedDepth, bitStack.CurrentDepth);
            }
        }
Beispiel #15
0
        public static void DefaultBitStack()
        {
            BitStack bitStack = default;

            Assert.Equal(0, bitStack.CurrentDepth);
        }
Beispiel #16
0
 public ReversedMemoryStream()
 {
     Buffer      = new LinkedList <byte>();
     BufferStack = new BitStack();
 }