Esempio n. 1
0
 public void Add(Int32 amount)
 {
     if (!TryAdd(amount))
     {
         throw new InvalidOperationException(ExpressionResources.ExceededAllowedMemory(m_node?.ConvertToExpression()));
     }
 }
        protected sealed override Object EvaluateCore(
            EvaluationContext context,
            out ResultMemory resultMemory)
        {
            resultMemory = null;
            var format = Parameters[0].Evaluate(context).ConvertToString();
            var index  = 0;
            var result = new FormatResultBuilder(this, context, CreateMemoryCounter(context));

            while (index < format.Length)
            {
                var lbrace = format.IndexOf('{', index);
                var rbrace = format.IndexOf('}', index);

                // Left brace
                if (lbrace >= 0 && (rbrace < 0 || rbrace > lbrace))
                {
                    // Escaped left brace
                    if (SafeCharAt(format, lbrace + 1) == '{')
                    {
                        result.Append(format.Substring(index, lbrace - index + 1));
                        index = lbrace + 2;
                    }
                    // Left brace, number, optional format specifiers, right brace
                    else if (rbrace > lbrace + 1 &&
                             ReadArgIndex(format, lbrace + 1, out Byte argIndex, out Int32 endArgIndex) &&
                             ReadFormatSpecifiers(format, endArgIndex + 1, out String formatSpecifiers, out rbrace))
                    {
                        // Check parameter count
                        if (argIndex > Parameters.Count - 2)
                        {
                            throw new FormatException(ExpressionResources.InvalidFormatArgIndex(format));
                        }

                        // Append the portion before the left brace
                        if (lbrace > index)
                        {
                            result.Append(format.Substring(index, lbrace - index));
                        }

                        // Append the arg
                        result.Append(argIndex, formatSpecifiers);
                        index = rbrace + 1;
                    }
                    else
                    {
                        throw new FormatException(ExpressionResources.InvalidFormatString(format));
                    }
                }
Esempio n. 3
0
        internal void AddAmount(
            Int32 depth,
            Int32 bytes,
            Boolean trimDepth = false)
        {
            // Trim deeper depths
            if (trimDepth)
            {
                while (m_maxActiveDepth > depth)
                {
                    var amount = m_depths[m_maxActiveDepth];

                    if (amount > 0)
                    {
                        // Sanity check
                        if (amount > m_totalAmount)
                        {
                            throw new InvalidOperationException("Bytes to subtract exceeds total bytes");
                        }

                        // Subtract from the total
                        checked
                        {
                            m_totalAmount -= amount;
                        }

                        // Reset the amount
                        m_depths[m_maxActiveDepth] = 0;
                    }

                    m_maxActiveDepth--;
                }
            }

            // Grow the depths
            if (depth > m_maxActiveDepth)
            {
                // Grow the list
                while (m_depths.Count <= depth)
                {
                    m_depths.Add(0);
                }

                // Adjust the max active depth
                m_maxActiveDepth = depth;
            }

            checked
            {
                // Add to the depth
                m_depths[depth] += bytes;

                // Add to the total
                m_totalAmount += bytes;
            }

            // Check max
            if (m_totalAmount > m_maxAmount)
            {
                throw new InvalidOperationException(ExpressionResources.ExceededAllowedMemory(m_node?.ConvertToExpression()));
            }
        }