public static ReportInfiniteLoop ( StringBuilder sb, object arg, string argName, int &maxLength ) : bool | ||
sb | StringBuilder | String builder to which information about the argument is appended. |
arg | object |
/// Value which has already been appended to |
argName | string | Optional name of the instance which is reported, if available. |
maxLength | int | /// Maximum number of characters allowed to the formatter. Formatting will fail (and return false) /// if this number of characters is breached. Multi-lined formatters will ignore this parameter. /// Negative value indicates that formatter has unlimited space available. On output contains remaining number of characters available. /// |
Résultat | bool |
/// <summary> /// Converts the value of a specified object to an equivalent string representation using /// specified format and culture-specific formatting information. /// </summary> /// <param name="sb">String builder to which formatted string should be appended.</param> /// <param name="format"> /// A format string containing formatting specifications; /// ignored in this instance. /// </param> /// <param name="arg">An object to format.</param> /// <param name="formatProvider">An object that supplies format information about current instance.</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 string representation of <paramref name="arg" /> has been successfully /// appended to <paramref name="sb" /> within given number of allowed characters; otherwise false. /// </returns> internal override bool Format(StringBuilder sb, string format, object arg, IFormatProvider formatProvider, ref int maxLength) { var success = true; var originalLength = sb.Length; if (PushCurrentObject(arg)) { if (success) { success = AppendInstanceTypeName(arg, sb, ref maxLength); } var dimensions = GetDimensions(arg); success = FormatInfoUtils.TryAppendSpaceIfNeeded(this, sb, success, ref maxLength); success = FormatInfoUtils.TryAppendString(this, sb, FirstContainedValuePrefix, success, ref maxLength); if (MaximumDepth > 0) { MaximumDepth--; success = success && RecursiveAppendDimension(sb, (Array)arg, new List <int>(), ref maxLength); MaximumDepth++; } success = FormatInfoUtils.TryAppendString(this, sb, LastContainedValueSuffix, success, ref maxLength); PopCurrentObject(); } else { success = success && FormatInfoUtils.ReportInfiniteLoop(sb, arg, InstanceName, ref maxLength); } if (!success) { sb.Length = originalLength; // Remove any characters appended to string builder in an unsuccessful attempt } return(success); }
/// <summary> /// Converts the value of a specified object to an equivalent string representation using /// specified format and culture-specific formatting information. /// </summary> /// <param name="sb">String builder to which formatted string should be appended.</param> /// <param name="format">A format string containing formatting specifications.</param> /// <param name="arg">An object to format.</param> /// <param name="formatProvider">An object that supplies format information about the current instance.</param> /// <param name="maxLength"> /// Maximum number of characters allowed to the formatter. Formatting will fail (and return false) /// if this number of characters is breached. Multi-lined formatters will 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> internal override bool Format(StringBuilder sb, string format, object arg, IFormatProvider formatProvider, ref int maxLength) { var success = true; var originalLength = sb.Length; if (PushCurrentObject(arg)) { if (ShowDataType) { var instanceType = GetInstanceType(arg); success = success && AppendFriendlyTypeName(instanceType, arg, sb, ref maxLength); } if (sb.Length > originalLength) { success = FormatInfoUtils.TryAppendChar(this, sb, ' ', success, ref maxLength); } success = FormatInfoUtils.TryAppendString(this, sb, FirstContainedValuePrefix, success, ref maxLength); if (arg != null) { IncIndentationLevel(true); var array = (Array)arg; var rows = array.GetLength(0); var cafi = new CompactArrayFormatInfo(this); cafi.ShowItemsOnly = true; cafi.FieldLength = GetMaxValueLength(array); var rowNumberFormatProvider = new ScalarFormatInfo(VerboseFormatInfo.SingleLinedFormat); rowNumberFormatProvider.ShowDataType = false; rowNumberFormatProvider.ShowInstanceName = false; if (IsMultiLinedFormat) { rowNumberFormatProvider.FieldLength = rowNumberFormatProvider.GetValueLength(rows); } for (var i = 0; i < rows; i++) { if (i == rows - 1) { DecIndentationLevel(); IncIndentationLevel(false); // There are no more rows in the matrix cafi.DecIndentationLevel(); cafi.IncIndentationLevel(false); } success = success && FormatLinePrefix(sb, true, i == rows - 1, false, 0, ref maxLength); success = FormatInfoUtils.TryAppendString(this, sb, "Row ", success, ref maxLength); success = success && rowNumberFormatProvider.Format(sb, null, i, rowNumberFormatProvider, ref maxLength); success = FormatInfoUtils.TryAppendChar(this, sb, ' ', success, ref maxLength); success = FormatInfoUtils.TryAppendString(this, sb, FirstContainedValuePrefix, success, ref maxLength); // Now we should append row content, which is obtained differently in case of matrix and in case of jagged array if (array.Rank == 1) { // Array is jagged var row = (Array)array.GetValue(i); cafi.ShowLastDimension = false; success = success && cafi.Format(sb, null, row, cafi, ref maxLength); } else { // Array is a matrix cafi.HeadingIndices = new[] { i }; cafi.ShowLastDimension = true; success = success && cafi.Format(sb, null, array, cafi, ref maxLength); cafi.HeadingIndices = null; } success = FormatInfoUtils.TryAppendString(this, sb, LastContainedValueSuffix, success, ref maxLength); } DecIndentationLevel(); } success = FormatInfoUtils.TryAppendString(this, sb, LastContainedValueSuffix, success, ref maxLength); PopCurrentObject(); } else { success = success && FormatInfoUtils.ReportInfiniteLoop(sb, arg, InstanceName, ref maxLength); } if (!success) { sb.Length = originalLength; } return(success); }
/// <summary> /// Converts the value of a specified object to an equivalent string representation using /// specified format and culture-specific formatting information. /// </summary> /// <param name="sb">String builder to which formatted string should be appended.</param> /// <param name="format">Format string containing formatting specifications.</param> /// <param name="arg">An object to format.</param> /// <param name="formatProvider">An object that supplies format information about the current instance.</param> /// <param name="maxLength"> /// Maximum number of characters allowed to the formatter. Formatting will fail (and return false) /// if this number of characters is breached. Multi-lined formatters will 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> internal override bool Format(StringBuilder sb, string format, object arg, IFormatProvider formatProvider, ref int maxLength) { var success = true; var originalLength = sb.Length; if (PushCurrentObject(arg)) { if (ShowDataType) { success = success && AppendInstanceTypeName(arg, sb, ref maxLength); } if (!string.IsNullOrEmpty(InstanceName)) { var name = string.Format("{0}{1}{2}={2}", sb.Length > originalLength ? " " : "", InstanceName, IsMultiLinedFormat ? " " : ""); success = FormatInfoUtils.TryAppendString(this, sb, name, success, ref maxLength); } else if (sb.Length > originalLength) { success = FormatInfoUtils.TryAppendChar(this, sb, ' ', success, ref maxLength); } var argType = GetInstanceType(arg); if (arg == null) { success = FormatInfoUtils.TryAppendString(this, sb, FormatInfoUtils.DefaultNullFormatted, success, ref maxLength); } else if (arg is char) { success = success && FormatChar(sb, (char)arg, true, false, ref maxLength); } else if (arg is string) { success = success && FormatString(sb, (string)arg, true, ref maxLength); } else if (arg is Boolean) { success = FormatInfoUtils.TryAppendString(this, sb, ((bool)arg) ? "true" : "false", success, ref maxLength); } else if (argType.IsEnum) { var plainName = Enum.GetName(argType, arg); if (plainName != null) { success = FormatInfoUtils.TryAppendString(this, sb, plainName, success, ref maxLength); } else { success = success && AppendEnumFlags((Enum)arg, sb, ref maxLength); // arg is an OR-ed combination of values } } else { success = FormatInfoUtils.TryAppendString(this, sb, arg.ToString(), success, ref maxLength); } while (success && sb.Length - originalLength < _fieldLength) { success = FormatInfoUtils.TryAppendChar(this, sb, ' ', success, ref maxLength); } PopCurrentObject(); } else { success = success && FormatInfoUtils.ReportInfiniteLoop(sb, arg, InstanceName, ref maxLength); } if (!success) { sb.Length = originalLength; } return(success); }
/// <summary> /// Converts the value of a specified object to an equivalent string representation using /// specified format and culture-specific formatting information. /// </summary> /// <param name="sb">String builder to which formatted string will be appended.</param> /// <param name="format">Format string containing formatting specifications.</param> /// <param name="arg">An object to format.</param> /// <param name="formatProvider">An object that supplies format information about the current instance.</param> /// <param name="maxLength"> /// Maximum number of characters allowed to the formatter. Formatting will fail (and return false) /// if this number of characters is breached. Multi-lined formatters will 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> internal override bool Format(StringBuilder sb, string format, object arg, IFormatProvider formatProvider, ref int maxLength) { var success = true; var originalLength = sb.Length; if (PushCurrentObject(arg)) { if (ShowDataType) { success = success && AppendInstanceTypeName(arg, sb, ref maxLength); } if (ShowInstanceName && !string.IsNullOrEmpty(InstanceName)) { success = FormatInfoUtils.TryAppendSpaceIfNeeded(this, sb, success, ref maxLength); success = FormatInfoUtils.TryAppendString(this, sb, InstanceName, success, ref maxLength); if (IsMultiLinedFormat) { success = FormatInfoUtils.TryAppendString(this, sb, " = ", success, ref maxLength); } else { success = FormatInfoUtils.TryAppendChar(this, sb, '=', success, ref maxLength); } } else { success = FormatInfoUtils.TryAppendSpaceIfNeeded(this, sb, success, ref maxLength); } if (arg == null) { success = FormatInfoUtils.TryAppendString(this, sb, FormatInfoUtils.DefaultNullFormatted, success, ref maxLength); } else { success = FormatInfoUtils.TryAppendString(this, sb, FirstContainedValuePrefix, success, ref maxLength); if (MaximumDepth > 0) { MaximumDepth--; var members = ExtractMembers(arg); var vfi = new VerboseFormatInfo(this); vfi.ShowDataType = true; vfi.ShowInstanceName = true; for (var i = 0; success && i < members.Length; i++) { IncIndentationLevel(i < members.Length - 1); vfi.IncIndentationLevel(i < members.Length - 1); success = success && FormatLinePrefix(sb, i == 0, i == members.Length - 1, false, 0, ref maxLength); vfi.InstanceDataType = members[i].DataType; vfi.InstanceName = members[i].Name; success = success && vfi.Format(sb, format, members[i].Value, vfi, ref maxLength); vfi.DecIndentationLevel(); DecIndentationLevel(); } MaximumDepth++; } success = FormatInfoUtils.TryAppendString(this, sb, LastContainedValueSuffix, success, ref maxLength); } PopCurrentObject(); } else { success = success && FormatInfoUtils.ReportInfiniteLoop(sb, arg, InstanceName, ref maxLength); } if (!success) { sb.Length = originalLength; } return(success); }
/// <summary> /// Converts the value of a specified array to an equivalent string representation using /// specified format and culture-specific formatting information. /// </summary> /// <param name="sb">String builder to which formatted string should be appended.</param> /// <param name="format">A format string containing formatting specifications.</param> /// <param name="arg">An object to format.</param> /// <param name="formatProvider">An object that supplies format information about the current instance.</param> /// <param name="maxLength"> /// Maximum number of characters allowed to the formatter. Formatting will fail (and return false) /// if this number of characters is breached. Multi-lined formatters will ignore this parameter. /// Negative value indicates that formatter has unlimited space 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> internal override bool Format(StringBuilder sb, string format, object arg, IFormatProvider formatProvider, ref int maxLength) { var success = true; var originalLength = sb.Length; var array = (Array)arg; var rank = array.Rank; if (rank > 1 || PushCurrentObject(arg)) { _currentInstance = array; var prevFieldLength = _fieldLength; UpdateFieldLength(sb); // Autonomously updates field length if that will help better format values from the array if (!_showItemsOnly && ShowDataType) { success = success && AppendInstanceTypeName(arg, sb, ref maxLength); success = FormatInfoUtils.TryAppendSpaceIfNeeded(this, sb, success, ref maxLength); success = FormatInfoUtils.TryAppendString(this, sb, FirstContainedValuePrefix, success, ref maxLength); } var enumerator = GetEnumerator(); var sfi = new ScalarFormatInfo(this); sfi.ShowDataType = false; sfi.ShowInstanceName = false; sfi.FieldLength = _fieldLength; var linePrefixLength = GetCurrentLineLength(sb); var lineStartPos = sb.Length; var isFirstValueInLine = true; var repeatLastValue = false; while (success && (repeatLastValue || enumerator.MoveNext())) { // At every position it might happen that line should be broken and formatting continued on the following line var prevLineLength = sb.Length - lineStartPos; string delimiter = null; if (isFirstValueInLine) { delimiter = ""; } else if (IsMultiLinedFormat) { delimiter = " "; } else { delimiter = FieldDelimiter; } success = FormatInfoUtils.TryAppendString(this, sb, delimiter, success, ref maxLength); success = success && sfi.Format(sb, null, enumerator.Current, sfi, ref maxLength); isFirstValueInLine = false; repeatLastValue = false; if (IsMultiLinedFormat && _maxLineLength > 0 && sb.Length - lineStartPos > _maxLineLength && !isFirstValueInLine) { // Maximum line length has been breached in the multi-lined format // As a consequence, last value should be deleted from output, new line should be started and last value formatted again sb.Length = prevLineLength + lineStartPos; success = success && FormatLinePrefix(sb, true, false, true, linePrefixLength, ref maxLength); lineStartPos = sb.Length; repeatLastValue = true; isFirstValueInLine = true; } } if (!_showItemsOnly) { success = FormatInfoUtils.TryAppendString(this, sb, LastContainedValueSuffix, success, ref maxLength); } _fieldLength = prevFieldLength; // Restore field length to value before this method was called if (rank == 1) { PopCurrentObject(); } } else { success = success && FormatInfoUtils.ReportInfiniteLoop(sb, arg, InstanceName, ref maxLength); } if (!success) { sb.Length = originalLength; } return(success); }
/// <summary> /// Converts the value of a specified object to an equivalent string representation using /// specified format and culture-specific formatting information. /// </summary> /// <param name="sb">String builder to which formatted string should be appended.</param> /// <param name="format">A format string containing formatting specifications.</param> /// <param name="arg">An object to format.</param> /// <param name="formatProvider">An object that supplies format information about the current instance.</param> /// <param name="maxLength"> /// Maximum number of characters allowed to the formatter. Formatting should fail (and return false) /// if this number of characters is breached. Multi-lined formatters should 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> internal override bool Format(StringBuilder sb, string format, object arg, IFormatProvider formatProvider, ref int maxLength) { var success = true; var originalLength = sb.Length; if (PushCurrentObject(arg)) { if (ShowDataType && !_showValuesOnly) { success = success && AppendInstanceTypeName(arg, sb, ref maxLength); } if (ShowInstanceName && !string.IsNullOrEmpty(InstanceName) && !_showValuesOnly) { success = FormatInfoUtils.TryAppendSpaceIfNeeded(this, sb, success, ref maxLength); success = FormatInfoUtils.TryAppendString(this, sb, InstanceName, success, ref maxLength); } if (!_showValuesOnly) { success = FormatInfoUtils.TryAppendSpaceIfNeeded(this, sb, success, ref maxLength); success = FormatInfoUtils.TryAppendString(this, sb, FirstContainedValuePrefix, success, ref maxLength); } Type enumeratedType = null; var enumerator = GetEnumerator(arg, ref enumeratedType); var itemFormatter = new VerboseFormatInfo(this); var value = new object[2]; var valueFetched = new bool[2]; valueFetched[0] = GetNextValue(enumerator, ref value[0]); if (valueFetched[0]) { valueFetched[1] = GetNextValue(enumerator, ref value[1]); } var itemPos = 0; while (valueFetched[0]) { IncIndentationLevel(valueFetched[1]); itemFormatter.IncIndentationLevel(valueFetched[1]); var valueName = string.Format("Item[{0}]", itemPos++); itemFormatter.InstanceDataType = enumeratedType; itemFormatter.InstanceName = valueName; success = success && FormatLinePrefix(sb, itemPos == 0, !valueFetched[1], false, 0, ref maxLength); success = success && itemFormatter.Format(sb, null, value[0], itemFormatter, ref maxLength); valueFetched[0] = valueFetched[1]; value[0] = value[1]; valueFetched[1] = GetNextValue(enumerator, ref value[1]); itemFormatter.DecIndentationLevel(); DecIndentationLevel(); } if (!_showValuesOnly) { success = FormatInfoUtils.TryAppendString(this, sb, LastContainedValueSuffix, success, ref maxLength); } PopCurrentObject(); } else { success = success && FormatInfoUtils.ReportInfiniteLoop(sb, arg, InstanceName, ref maxLength); } if (!success) { sb.Length = originalLength; } return(success); }
/// <summary> /// Converts the value of a specified object to an equivalent string representation using /// specified format and culture-specific formatting information. /// </summary> /// <param name="sb">String builder to which formatted string should be appended.</param> /// <param name="format">A format string containing formatting specifications.</param> /// <param name="arg">An object to format.</param> /// <param name="formatProvider">An object that supplies format information about the current instance.</param> /// <param name="maxLength"> /// Maximum number of characters allowed to the formatter. Formatting should fail (and return false) /// if this number of characters is breached. Multi-lined formatters should 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> internal override bool Format(StringBuilder sb, string format, object arg, IFormatProvider formatProvider, ref int maxLength) { var success = true; var originalLength = sb.Length; if (PushCurrentObject(arg)) { if (arg == null) { success = FormatInfoUtils.TryAppendString(this, sb, FormatInfoUtils.DefaultNullFormatted, success, ref maxLength); } else { var keys = GetKeysCollection(arg); var keyType = GetDeclaredKeyValueType(arg, true); var valueType = GetDeclaredKeyValueType(arg, false); var sfi = new ScalarFormatInfo(this); success = success && sfi.AppendInstanceTypeName(arg, sb, ref maxLength); // Using scalar format info to append type name ensures that scalar types will be // represented with their short forms rather than CTS names which are less readable if (!string.IsNullOrEmpty(InstanceName)) { success = FormatInfoUtils.TryAppendSpaceIfNeeded(this, sb, success, ref maxLength); success = FormatInfoUtils.TryAppendString(this, sb, InstanceName, success, ref maxLength); } if (IsMultiLinedFormat) { success = FormatInfoUtils.TryAppendSpaceIfNeeded(this, sb, success, ref maxLength); success = FormatInfoUtils.TryAppendString(this, sb, "= ", success, ref maxLength); } else { success = FormatInfoUtils.TryAppendChar(this, sb, '=', success, ref maxLength); } success = FormatInfoUtils.TryAppendString(this, sb, FirstContainedValuePrefix, success, ref maxLength); if (MaximumDepth > 0) { MaximumDepth--; var enumerator = keys.GetEnumerator(); var isFirstValue = true; var key = new object[2]; var keyFetched = new bool[2]; keyFetched[0] = enumerator.MoveNext(); if (keyFetched[0]) { key[0] = enumerator.Current; keyFetched[1] = enumerator.MoveNext(); if (keyFetched[1]) { key[1] = enumerator.Current; } } VerboseFormatInfoBase containedItemsFormat = new VerboseFormatInfo(this); var itemPos = 0; while (keyFetched[0]) { IncIndentationLevel(keyFetched[1]); containedItemsFormat.IncIndentationLevel(keyFetched[1]); success = success && FormatLinePrefix(sb, isFirstValue, !keyFetched[1], false, 0, ref maxLength); isFirstValue = false; success = FormatInfoUtils.TryAppendString(this, sb, "Item[", success, ref maxLength); success = FormatInfoUtils.TryAppendString(this, sb, itemPos.ToString("0"), success, ref maxLength); itemPos++; success = FormatInfoUtils.TryAppendString(this, sb, "] = ", success, ref maxLength); success = FormatInfoUtils.TryAppendString(this, sb, FirstContainedValuePrefix, success, ref maxLength); containedItemsFormat.IncIndentationLevel(true); IncIndentationLevel(true); success = success && FormatLinePrefix(sb, true, false, false, 0, ref maxLength); containedItemsFormat.InstanceDataType = keyType; containedItemsFormat.InstanceName = "Key"; success = success && containedItemsFormat.Format(sb, null, key[0], containedItemsFormat, ref maxLength); DecIndentationLevel(); containedItemsFormat.DecIndentationLevel(); containedItemsFormat.IncIndentationLevel(false); IncIndentationLevel(false); var value = GetValue(arg, key[0]); success = success && FormatLinePrefix(sb, false, true, false, 0, ref maxLength); containedItemsFormat.InstanceDataType = valueType; containedItemsFormat.InstanceName = "Value"; success = success && containedItemsFormat.Format(sb, null, value, containedItemsFormat, ref maxLength); DecIndentationLevel(); containedItemsFormat.DecIndentationLevel(); success = FormatInfoUtils.TryAppendString(this, sb, LastContainedValueSuffix, success, ref maxLength); key[0] = key[1]; keyFetched[0] = keyFetched[1]; if (keyFetched[0]) { keyFetched[1] = enumerator.MoveNext(); if (keyFetched[1]) { key[1] = enumerator.Current; } } containedItemsFormat.DecIndentationLevel(); DecIndentationLevel(); } MaximumDepth++; } // if (MaximumDepth > 0) success = FormatInfoUtils.TryAppendString(this, sb, LastContainedValueSuffix, success, ref maxLength); } PopCurrentObject(); } else { success = success && FormatInfoUtils.ReportInfiniteLoop(sb, arg, InstanceName, ref maxLength); } if (!success) { sb.Length = originalLength; } return(success); }