Example #1
0
 /// <summary>
 ///     Combines maximum formatted string length from this instance with given length and sets new maximum length for this instance.
 /// </summary>
 /// <param name="length">
 ///     Length with which maximum allowed length of the string created by this formatter is combined;
 ///     negative value indicates infinite allowed length.
 /// </param>
 /// <returns>
 ///     New value of the maximum allowed string length for this formatter, which equals value that will be returned by
 ///     MaximumFormattedLength property after this method returns. Note that return value is affected by IsMultiLinedFormat property
 ///     value in sense that whenever this formatter is multi-lined, this method will return value -1 regardless of actual
 ///     internal setting of maximum string length.
 /// </returns>
 public int CombineMaximumFormattedLength(int length)
 {
     _maximumFormattedLength = FormatInfoUtils.CombineMaxFormattedLengths(_maximumFormattedLength, length);
     return(MaximumFormattedLength);
 }
Example #2
0
        /// <summary>
        ///     Appends formatted string representing part of the array provided, starting from specific location within the array.
        /// </summary>
        /// <param name="sb">String builder to which formatted string will be appended.</param>
        /// <param name="arg">Array which should be formatted into string which is appended to <paramref name="sb" />.</param>
        /// <param name="indices">
        ///     List of index values for dimensions iterated this far. In every call, this method will continue
        ///     from location within the array indicated by this parameter.
        /// </param>
        /// <param name="maxLength">
        ///     Maximum number of characters allowed to the formatter.
        ///     Single-lined formatting will fail (and return false) if this number of characters is breached.
        ///     Multi-lined formatters ignore this parameter. Negative value indicates
        ///     that formatter has unlimited space available. On output contains remaining number of characters available.
        /// </param>
        /// <returns>
        ///     true if representation of <paramref name="arg" /> has been successfully appended to <paramref name="sb" />
        ///     within given number of allowed characters; otherwise false.
        /// </returns>
        private bool RecursiveAppendDimension(StringBuilder sb, Array arg, List <int> indices, ref int maxLength)
        {
            var success        = true;
            var originalLength = sb.Length;

            var dimension = arg.GetLength(indices.Count);

            if (dimension > 0)
            {
                for (var i = 0; i < dimension; i++)
                {
                    indices.Add(i);

                    IncIndentationLevel(i < dimension - 1);

                    success = success && FormatLinePrefix(sb, true, i == dimension - 1, false, 0, ref maxLength);
                    success = FormatInfoUtils.TryAppendString(this, sb, "Item[", success, ref maxLength);

                    var firstIndex = true;
                    foreach (var index in indices)
                    {
                        if (!firstIndex)
                        {
                            success = FormatInfoUtils.TryAppendString(this, sb, ", ", success, ref maxLength);
                        }
                        firstIndex = false;
                        success    = FormatInfoUtils.TryAppendString(this, sb, index.ToString("0"), success, ref maxLength);
                    }

                    success = FormatInfoUtils.TryAppendString(this, sb, "] ", success, ref maxLength);
                    success = FormatInfoUtils.TryAppendString(this, sb, FirstContainedValuePrefix, success, ref maxLength);

                    if (indices.Count < arg.Rank)
                    {
                        success = success && RecursiveAppendDimension(sb, arg, indices, ref maxLength);
                    }
                    else
                    {
                        var value = arg.GetValue(indices.ToArray());

                        var vfi = new VerboseFormatInfo(this);
                        vfi.MaximumFormattedLength =
                            FormatInfoUtils.CombineMaxFormattedLengths(
                                RawMaximumFormattedLength,
                                FormatInfoUtils.DefaultMaxFormattedLength);

                        success = FormatInfoUtils.TryAppendChar(this, sb, ' ', success, ref maxLength);
                        success = success && vfi.Format(sb, null, value, vfi, ref maxLength);
                    }

                    success = FormatInfoUtils.TryAppendString(this, sb, LastContainedValueSuffix, success, ref maxLength);

                    DecIndentationLevel();

                    indices.RemoveAt(indices.Count - 1);
                }
            }

            if (!success)
            {
                sb.Length = originalLength;
            }

            return(success);
        }