private GetValueLength ( object value ) : int | ||
value | object | Value which should be presented in the formatted string. |
return | int |
/// <summary> /// Iterates through values that will be formatted in order to determine whether /// field length should be set to some specific value in order to align values better. /// </summary> /// <param name="sb"> /// String builder which can be conveniently used to format value. /// This function will return the string builder into present state on output. /// </param> private void UpdateFieldLength(StringBuilder sb) { if (IsMultiLinedFormat && _fieldLength <= 0 && _maxLineLength > 0) { // Field length will be updated only in multi-lined formats and only if not already set to positive value from outside. var maxLength = 0; var totalLength = 0; var enumerator = GetEnumerator(); var sfi = new ScalarFormatInfo(this); sfi.FieldLength = 0; sfi.ShowDataType = false; sfi.ShowInstanceName = false; var isFirstField = true; while (enumerator.MoveNext()) { var fieldLength = sfi.GetValueLength(enumerator.Current, sb); maxLength = Math.Max(maxLength, fieldLength); totalLength += (!isFirstField ? 1 : 0) + fieldLength; // Each but the first value is prefixed with a white space of length 1 isFirstField = false; } if (totalLength > _maxLineLength) { _fieldLength = maxLength; // Field length is set only if total contents of the array will breach the maximum allowed line length } // and consequently values should be aligned } }
/// <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">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; }