Skip to content

Commit

Permalink
Add option to specify prefix, suffix, and separator strings for Debug…
Browse files Browse the repository at this point in the history
…Utility.Format()
  • Loading branch information
Exanite committed Oct 26, 2024
1 parent 07cd9de commit 1683893
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions Utilities/DebugUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static T Dump<T>(this T value, string name)
/// <summary>
/// Formats collections (and IEnumerables) in an array-like format.
/// </summary>
public static string Format(object? value)
public static string Format(object? value, string separator = ", ", string prefix = "[", string suffix = "]")
{
if (value == null)
{
Expand All @@ -76,30 +76,30 @@ public static string Format(object? value)
if (value is IEnumerable enumerable && value is not string)
{
var stringBuilder = new StringBuilder();
FormatEnumerable(enumerable, stringBuilder);
FormatEnumerable(enumerable, stringBuilder, separator, prefix, suffix);

return stringBuilder.ToString();
}

return value.ToString() ?? "Null";
}

private static void FormatEnumerable(IEnumerable enumerable, StringBuilder stringBuilder)
private static void FormatEnumerable(IEnumerable enumerable, StringBuilder stringBuilder, string separator, string prefix, string suffix)
{
stringBuilder.Append("[");
stringBuilder.Append(prefix);

var isFirst = true;

foreach (var value in enumerable)
{
if (!isFirst)
{
stringBuilder.Append(", ");
stringBuilder.Append(separator);
}

if (value is IEnumerable nestedEnumerable && value is not string)
{
FormatEnumerable(nestedEnumerable, stringBuilder);
FormatEnumerable(nestedEnumerable, stringBuilder, separator, prefix, suffix);
}
else
{
Expand All @@ -109,7 +109,7 @@ private static void FormatEnumerable(IEnumerable enumerable, StringBuilder strin
isFirst = false;
}

stringBuilder.Append("]");
stringBuilder.Append(suffix);
}

/// <summary>
Expand Down

0 comments on commit 1683893

Please sign in to comment.