| | | 1 | | namespace SwiftCollections.Diagnostics; |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Builds diagnostic messages for enabled diagnostic levels while allowing disabled levels |
| | | 9 | | /// to skip formatted interpolation expression evaluation. |
| | | 10 | | /// </summary> |
| | | 11 | | [InterpolatedStringHandler] |
| | | 12 | | public ref struct DiagnosticInterpolatedStringHandler |
| | | 13 | | { |
| | | 14 | | private readonly bool _isEnabled; |
| | | 15 | | #if NET6_0_OR_GREATER |
| | | 16 | | private DefaultInterpolatedStringHandler _builder; |
| | | 17 | | #else |
| | | 18 | | private StringBuilder? _builder; |
| | | 19 | | #endif |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new instance of the <see cref="DiagnosticInterpolatedStringHandler"/> struct. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="literalLength">The combined length of literal portions in the interpolated string.</param> |
| | | 25 | | /// <param name="formattedCount">The number of formatted expressions in the interpolated string.</param> |
| | | 26 | | /// <param name="channel">The diagnostic channel that will receive the message.</param> |
| | | 27 | | /// <param name="level">The severity level being evaluated.</param> |
| | | 28 | | /// <param name="isEnabled"> |
| | | 29 | | /// Set to <c>true</c> when formatted expressions should be evaluated; otherwise, <c>false</c>. |
| | | 30 | | /// </param> |
| | | 31 | | public DiagnosticInterpolatedStringHandler( |
| | | 32 | | int literalLength, |
| | | 33 | | int formattedCount, |
| | | 34 | | DiagnosticChannel channel, |
| | | 35 | | DiagnosticLevel level, |
| | | 36 | | out bool isEnabled) |
| | | 37 | | { |
| | 15 | 38 | | isEnabled = channel != null && channel.IsEnabled(level); |
| | 15 | 39 | | _isEnabled = isEnabled; |
| | | 40 | | #if NET6_0_OR_GREATER |
| | 15 | 41 | | _builder = isEnabled ? new DefaultInterpolatedStringHandler(literalLength, formattedCount) : default; |
| | | 42 | | #else |
| | | 43 | | _ = formattedCount; |
| | | 44 | | _builder = isEnabled ? new StringBuilder(literalLength) : null; |
| | | 45 | | #endif |
| | 15 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets a value indicating whether this handler is actively building a diagnostic message. |
| | | 50 | | /// </summary> |
| | 18 | 51 | | public bool IsEnabled => _isEnabled; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Appends a literal string segment. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="value">The literal string segment.</param> |
| | | 57 | | public void AppendLiteral(string value) |
| | | 58 | | { |
| | 25 | 59 | | if (!_isEnabled) |
| | 2 | 60 | | return; |
| | | 61 | | |
| | | 62 | | #if NET6_0_OR_GREATER |
| | 23 | 63 | | _builder.AppendLiteral(value); |
| | | 64 | | #else |
| | | 65 | | _builder!.Append(value); |
| | | 66 | | #endif |
| | 23 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Appends a formatted value. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 73 | | /// <param name="value">The value to append.</param> |
| | | 74 | | public void AppendFormatted<T>(T value) |
| | | 75 | | { |
| | 6 | 76 | | if (!_isEnabled) |
| | 1 | 77 | | return; |
| | | 78 | | |
| | | 79 | | #if NET6_0_OR_GREATER |
| | 5 | 80 | | _builder.AppendFormatted(value); |
| | | 81 | | #else |
| | | 82 | | _builder!.Append(value); |
| | | 83 | | #endif |
| | 5 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Appends a formatted value using the specified format string. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 90 | | /// <param name="value">The value to append.</param> |
| | | 91 | | /// <param name="format">The format string to apply.</param> |
| | | 92 | | public void AppendFormatted<T>(T value, string? format) |
| | | 93 | | { |
| | 3 | 94 | | if (!_isEnabled) |
| | 1 | 95 | | return; |
| | | 96 | | |
| | | 97 | | #if NET6_0_OR_GREATER |
| | 2 | 98 | | _builder.AppendFormatted(value, format); |
| | | 99 | | #else |
| | | 100 | | AppendFormattedValue(value, 0, format); |
| | | 101 | | #endif |
| | 2 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Appends a formatted value with the specified alignment. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 108 | | /// <param name="value">The value to append.</param> |
| | | 109 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 110 | | public void AppendFormatted<T>(T value, int alignment) |
| | | 111 | | { |
| | 3 | 112 | | if (!_isEnabled) |
| | 1 | 113 | | return; |
| | | 114 | | |
| | | 115 | | #if NET6_0_OR_GREATER |
| | 2 | 116 | | _builder.AppendFormatted(value, alignment); |
| | | 117 | | #else |
| | | 118 | | AppendFormattedValue(value, alignment, null); |
| | | 119 | | #endif |
| | 2 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Appends a formatted value with the specified alignment and format string. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 126 | | /// <param name="value">The value to append.</param> |
| | | 127 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 128 | | /// <param name="format">The format string to apply.</param> |
| | | 129 | | public void AppendFormatted<T>(T value, int alignment, string? format) |
| | | 130 | | { |
| | 3 | 131 | | if (!_isEnabled) |
| | 1 | 132 | | return; |
| | | 133 | | |
| | | 134 | | #if NET6_0_OR_GREATER |
| | 2 | 135 | | _builder.AppendFormatted(value, alignment, format); |
| | | 136 | | #else |
| | | 137 | | AppendFormattedValue(value, alignment, format); |
| | | 138 | | #endif |
| | 2 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Appends a string value. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <param name="value">The value to append.</param> |
| | | 145 | | public void AppendFormatted(string? value) |
| | | 146 | | { |
| | 3 | 147 | | if (!_isEnabled) |
| | 1 | 148 | | return; |
| | | 149 | | |
| | | 150 | | #if NET6_0_OR_GREATER |
| | 2 | 151 | | _builder.AppendFormatted(value); |
| | | 152 | | #else |
| | | 153 | | _builder!.Append(value); |
| | | 154 | | #endif |
| | 2 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Appends a string value with the specified alignment. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <param name="value">The value to append.</param> |
| | | 161 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 162 | | public void AppendFormatted(string? value, int alignment) |
| | | 163 | | { |
| | 2 | 164 | | if (!_isEnabled) |
| | 1 | 165 | | return; |
| | | 166 | | |
| | | 167 | | #if NET6_0_OR_GREATER |
| | 1 | 168 | | _builder.AppendFormatted(value, alignment); |
| | | 169 | | #else |
| | | 170 | | AppendAligned(value, alignment); |
| | | 171 | | #endif |
| | 1 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Appends a string value with the specified alignment and format string. |
| | | 176 | | /// </summary> |
| | | 177 | | /// <param name="value">The value to append.</param> |
| | | 178 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 179 | | /// <param name="format">The format string to apply.</param> |
| | | 180 | | public void AppendFormatted(string? value, int alignment, string? format) |
| | | 181 | | { |
| | 2 | 182 | | if (!_isEnabled) |
| | 1 | 183 | | return; |
| | | 184 | | |
| | | 185 | | #if NET6_0_OR_GREATER |
| | 1 | 186 | | _builder.AppendFormatted(value, alignment, format); |
| | | 187 | | #else |
| | | 188 | | AppendAligned(value, alignment); |
| | | 189 | | #endif |
| | 1 | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Appends a character span. |
| | | 194 | | /// </summary> |
| | | 195 | | /// <param name="value">The span to append.</param> |
| | | 196 | | public void AppendFormatted(ReadOnlySpan<char> value) |
| | | 197 | | { |
| | 2 | 198 | | if (!_isEnabled) |
| | 1 | 199 | | return; |
| | | 200 | | |
| | | 201 | | #if NET6_0_OR_GREATER |
| | 1 | 202 | | _builder.AppendFormatted(value); |
| | | 203 | | #else |
| | | 204 | | _builder!.Append(value); |
| | | 205 | | #endif |
| | 1 | 206 | | } |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Appends a character span with the specified alignment. |
| | | 210 | | /// </summary> |
| | | 211 | | /// <param name="value">The span to append.</param> |
| | | 212 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 213 | | public void AppendFormatted(ReadOnlySpan<char> value, int alignment) |
| | | 214 | | { |
| | 2 | 215 | | if (!_isEnabled) |
| | 1 | 216 | | return; |
| | | 217 | | |
| | | 218 | | #if NET6_0_OR_GREATER |
| | 1 | 219 | | _builder.AppendFormatted(value, alignment); |
| | | 220 | | #else |
| | | 221 | | AppendAligned(value, alignment); |
| | | 222 | | #endif |
| | 1 | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Appends a character span with the specified alignment and format string. |
| | | 227 | | /// </summary> |
| | | 228 | | /// <param name="value">The span to append.</param> |
| | | 229 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 230 | | /// <param name="format">The format string to apply.</param> |
| | | 231 | | public void AppendFormatted(ReadOnlySpan<char> value, int alignment, string? format) |
| | | 232 | | { |
| | 2 | 233 | | if (!_isEnabled) |
| | 1 | 234 | | return; |
| | | 235 | | |
| | | 236 | | #if NET6_0_OR_GREATER |
| | 1 | 237 | | _builder.AppendFormatted(value, alignment, format); |
| | | 238 | | #else |
| | | 239 | | AppendAligned(value, alignment); |
| | | 240 | | #endif |
| | 1 | 241 | | } |
| | | 242 | | |
| | | 243 | | internal string GetFormattedText() |
| | | 244 | | { |
| | | 245 | | #if NET6_0_OR_GREATER |
| | 10 | 246 | | return _builder.ToStringAndClear(); |
| | | 247 | | #else |
| | | 248 | | return _builder?.ToString() ?? string.Empty; |
| | | 249 | | #endif |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | #if !NET6_0_OR_GREATER |
| | | 253 | | private void AppendFormattedValue<T>(T value, int alignment, string? format) |
| | | 254 | | { |
| | | 255 | | string? text = value is IFormattable formattable |
| | | 256 | | ? formattable.ToString(format, null) |
| | | 257 | | : value?.ToString(); |
| | | 258 | | |
| | | 259 | | AppendAligned(text, alignment); |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | private void AppendAligned(string? value, int alignment) |
| | | 263 | | { |
| | | 264 | | value ??= string.Empty; |
| | | 265 | | |
| | | 266 | | if (alignment == 0) |
| | | 267 | | { |
| | | 268 | | _builder!.Append(value); |
| | | 269 | | return; |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | int width = alignment < 0 ? -alignment : alignment; |
| | | 273 | | int padding = width - value.Length; |
| | | 274 | | |
| | | 275 | | if (padding <= 0) |
| | | 276 | | { |
| | | 277 | | _builder!.Append(value); |
| | | 278 | | return; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | if (alignment > 0) |
| | | 282 | | _builder!.Append(' ', padding); |
| | | 283 | | |
| | | 284 | | _builder!.Append(value); |
| | | 285 | | |
| | | 286 | | if (alignment < 0) |
| | | 287 | | _builder!.Append(' ', padding); |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | private void AppendAligned(ReadOnlySpan<char> value, int alignment) |
| | | 291 | | { |
| | | 292 | | if (alignment == 0) |
| | | 293 | | { |
| | | 294 | | _builder!.Append(value); |
| | | 295 | | return; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | int width = alignment < 0 ? -alignment : alignment; |
| | | 299 | | int padding = width - value.Length; |
| | | 300 | | |
| | | 301 | | if (padding <= 0) |
| | | 302 | | { |
| | | 303 | | _builder!.Append(value); |
| | | 304 | | return; |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | if (alignment > 0) |
| | | 308 | | _builder!.Append(' ', padding); |
| | | 309 | | |
| | | 310 | | _builder!.Append(value); |
| | | 311 | | |
| | | 312 | | if (alignment < 0) |
| | | 313 | | _builder!.Append(' ', padding); |
| | | 314 | | } |
| | | 315 | | #endif |
| | | 316 | | } |