| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="StringBuilder"/> |
| | 5 | | /// </summary> |
| | 6 | | public static class StringBuilderExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Appends the label with value. |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="builder">The builder.</param> |
| | 12 | | /// <param name="name">The name.</param> |
| | 13 | | /// <param name="value">The value.</param> |
| | 14 | | public static void AppendLabelWithValue(this StringBuilder? builder, string name, object? value) => |
| 0 | 15 | | builder.AppendLabelWithValue(name, value, defaultValue: null, hasLineBreak: false); |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Appends the label with value. |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="builder">The builder.</param> |
| | 21 | | /// <param name="name">The name.</param> |
| | 22 | | /// <param name="value">The value.</param> |
| | 23 | | /// <param name="defaultValue">The default value.</param> |
| | 24 | | public static void AppendLabelWithValue(this StringBuilder? builder, string name, object? value, |
| 0 | 25 | | string? defaultValue) => builder.AppendLabelWithValue(name, value, defaultValue, hasLineBreak: false); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Appends the label with value. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="builder">The builder.</param> |
| | 31 | | /// <param name="name">The name.</param> |
| | 32 | | /// <param name="value">The value.</param> |
| | 33 | | /// <param name="defaultValue">The default value.</param> |
| | 34 | | /// <param name="hasLineBreak">When <c>true</c> add <see cref="Environment.NewLine" /> between label and value.</par |
| | 35 | | /// <remarks> |
| | 36 | | /// This method will append <c>name: value</c> to the appending <see cref="StringBuilder"/>. |
| | 37 | | /// This is useful when overriding <see cref="object.ToString"/>. |
| | 38 | | /// </remarks> |
| | 39 | | public static void AppendLabelWithValue(this StringBuilder? builder, string name, object? value, |
| | 40 | | string? defaultValue, bool hasLineBreak) |
| 0 | 41 | | { |
| 0 | 42 | | if (builder == null) return; |
| 0 | 43 | | if (value == null && string.IsNullOrWhiteSpace(defaultValue)) return; |
| | 44 | |
|
| 0 | 45 | | if (hasLineBreak) |
| 0 | 46 | | { |
| 0 | 47 | | builder.Append($"{name}:"); |
| 0 | 48 | | builder.AppendLine(); |
| 0 | 49 | | builder.Append($"{value ?? defaultValue}"); |
| 0 | 50 | | } |
| | 51 | | else |
| 0 | 52 | | { |
| 0 | 53 | | builder.Append($"{name}: {value ?? defaultValue}"); |
| 0 | 54 | | } |
| | 55 | |
|
| 0 | 56 | | builder.AppendLine(); |
| 0 | 57 | | } |
| | 58 | | } |