| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftThrowHelper.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | namespace SwiftCollections.Diagnostics; |
| | | 9 | | |
| | | 10 | | using System; |
| | | 11 | | using System.Collections.Generic; |
| | | 12 | | using System.Diagnostics.CodeAnalysis; |
| | | 13 | | using System.Runtime.CompilerServices; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Provides allocation-conscious guard helpers for throwing common exceptions. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// Validation methods keep the success path small and route exception creation through no-inline throw helpers. |
| | | 20 | | /// Interpolated custom messages are condition-gated so formatted expressions are not evaluated unless the guard throws. |
| | | 21 | | /// </remarks> |
| | | 22 | | public static class SwiftThrowHelper |
| | | 23 | | { |
| | | 24 | | #region Null Argument Validation |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Throws an <see cref="ArgumentNullException"/> if the provided argument is null. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="argument">The argument to check for null.</param> |
| | | 30 | | /// <param name="paramName">The name of the parameter that caused the exception.</param> |
| | | 31 | | /// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception> |
| | | 32 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 33 | | public static void ThrowIfNull( |
| | | 34 | | [NotNull] object? argument, |
| | | 35 | | [CallerArgumentExpression(nameof(argument))] string? paramName = null) |
| | | 36 | | { |
| | 214510 | 37 | | if (argument is null) |
| | 31 | 38 | | ThrowArgumentNullException(paramName); |
| | 214479 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Throws an <see cref="ArgumentNullException"/> if the provided generic argument is null. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <typeparam name="T">The type of the argument to check.</typeparam> |
| | | 45 | | /// <param name="argument">The argument to check for null.</param> |
| | | 46 | | /// <param name="paramName">The name of the parameter that caused the exception.</param> |
| | | 47 | | /// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception> |
| | | 48 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 49 | | public static void ThrowIfNullGeneric<T>( |
| | | 50 | | [NotNull] T argument, |
| | | 51 | | [CallerArgumentExpression(nameof(argument))] string? paramName = null) |
| | | 52 | | { |
| | 341007 | 53 | | if (argument is null) |
| | 4 | 54 | | ThrowArgumentNullException(paramName); |
| | 341003 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Throws an <see cref="ArgumentNullException"/> if the specified value is null and nulls are not legal for <typepa |
| | | 59 | | /// </summary> |
| | | 60 | | /// <typeparam name="TValue">The value type used to determine whether null is legal.</typeparam> |
| | | 61 | | /// <param name="value">The value to check.</param> |
| | | 62 | | /// <param name="defaultValue">A default value of type <typeparamref name="TValue"/> used to determine if nulls are |
| | | 63 | | /// <param name="paramName">The name of the parameter that caused the exception.</param> |
| | | 64 | | /// <exception cref="ArgumentNullException">Thrown when the value is null and nulls are illegal.</exception> |
| | | 65 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 66 | | public static void ThrowIfNullAndNullsAreIllegal<TValue>( |
| | | 67 | | object? value, |
| | | 68 | | TValue? defaultValue, |
| | | 69 | | [CallerArgumentExpression(nameof(value))] string? paramName = null) |
| | | 70 | | { |
| | 15 | 71 | | if (value is null && defaultValue is not null) |
| | 1 | 72 | | ThrowArgumentNullException(paramName); |
| | 14 | 73 | | } |
| | | 74 | | |
| | | 75 | | [DoesNotReturn] |
| | | 76 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 77 | | private static void ThrowArgumentNullException(string? paramName) |
| | | 78 | | { |
| | 36 | 79 | | paramName = NormalizeParamName(paramName); |
| | | 80 | | |
| | 36 | 81 | | if (string.IsNullOrEmpty(paramName)) |
| | 0 | 82 | | throw new ArgumentNullException(null, "Value cannot be null."); |
| | | 83 | | |
| | 36 | 84 | | throw new ArgumentNullException(paramName); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | #endregion |
| | | 88 | | |
| | | 89 | | #region Out of Range Validation |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Throws an <see cref="ArgumentOutOfRangeException"/> if the specified value is negative. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="value">The value to check.</param> |
| | | 95 | | /// <param name="paramName">The name of the parameter that caused the exception.</param> |
| | | 96 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the value is negative.</exception> |
| | | 97 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 98 | | public static void ThrowIfNegative( |
| | | 99 | | int value, |
| | | 100 | | [CallerArgumentExpression(nameof(value))] string? paramName = null) |
| | | 101 | | { |
| | 356 | 102 | | if (value < 0) |
| | | 103 | | { |
| | 5 | 104 | | paramName = NormalizeParamName(paramName); |
| | 5 | 105 | | ThrowArgumentOutOfRangeException(paramName, value, GetNonNegativeMessage(paramName)); |
| | | 106 | | } |
| | 351 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Throws an <see cref="ArgumentOutOfRangeException"/> if the specified value is negative or zero. |
| | | 111 | | /// </summary> |
| | | 112 | | /// <param name="value">The value to check.</param> |
| | | 113 | | /// <param name="paramName">The name of the parameter that caused the exception.</param> |
| | | 114 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the value is negative or zero.</exception> |
| | | 115 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 116 | | public static void ThrowIfNegativeOrZero( |
| | | 117 | | int value, |
| | | 118 | | [CallerArgumentExpression(nameof(value))] string? paramName = null) |
| | | 119 | | { |
| | 102 | 120 | | if (value <= 0) |
| | | 121 | | { |
| | 2 | 122 | | paramName = NormalizeParamName(paramName); |
| | 2 | 123 | | ThrowArgumentOutOfRangeException(paramName, value, GetPositiveMessage(paramName)); |
| | | 124 | | } |
| | 100 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Throws an <see cref="ArgumentOutOfRangeException"/> if the specified condition is true. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 131 | | /// <param name="actualValue">The value that caused the exception.</param> |
| | | 132 | | /// <param name="paramName">The name of the parameter that caused the exception.</param> |
| | | 133 | | /// <param name="message">An optional message to include in the exception.</param> |
| | | 134 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the condition is true.</exception> |
| | | 135 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 136 | | public static void ThrowIfArgumentOutOfRange( |
| | | 137 | | [DoesNotReturnIf(true)] bool condition, |
| | | 138 | | int? actualValue, |
| | | 139 | | [CallerArgumentExpression(nameof(actualValue))] string? paramName = null, |
| | | 140 | | string? message = null) |
| | | 141 | | { |
| | 170 | 142 | | if (condition) |
| | | 143 | | { |
| | 4 | 144 | | paramName = NormalizeParamName(paramName); |
| | 4 | 145 | | ThrowArgumentOutOfRangeException(paramName, actualValue, message ?? GetArgumentOutOfRangeMessage(paramName)) |
| | | 146 | | } |
| | 166 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Throws an <see cref="ArgumentOutOfRangeException"/> with a lazily formatted message if the specified condition i |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 153 | | /// <param name="actualValue">The value that caused the exception.</param> |
| | | 154 | | /// <param name="message">The interpolated exception message.</param> |
| | | 155 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the condition is true.</exception> |
| | | 156 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 157 | | public static void ThrowIfArgumentOutOfRange( |
| | | 158 | | [DoesNotReturnIf(true)] bool condition, |
| | | 159 | | int? actualValue, |
| | | 160 | | [InterpolatedStringHandlerArgument(nameof(condition))] SwiftThrowInterpolatedStringHandler message) |
| | | 161 | | { |
| | 0 | 162 | | if (condition) |
| | 0 | 163 | | ThrowArgumentOutOfRangeException(null, actualValue, message.GetFormattedText()); |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Throws an <see cref="ArgumentOutOfRangeException"/> with a lazily formatted message if the specified condition i |
| | | 168 | | /// </summary> |
| | | 169 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 170 | | /// <param name="actualValue">The value that caused the exception.</param> |
| | | 171 | | /// <param name="paramName">The name of the parameter that caused the exception.</param> |
| | | 172 | | /// <param name="message">The interpolated exception message.</param> |
| | | 173 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the condition is true.</exception> |
| | | 174 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 175 | | public static void ThrowIfArgumentOutOfRange( |
| | | 176 | | [DoesNotReturnIf(true)] bool condition, |
| | | 177 | | int? actualValue, |
| | | 178 | | string? paramName, |
| | | 179 | | [InterpolatedStringHandlerArgument(nameof(condition))] SwiftThrowInterpolatedStringHandler message) |
| | | 180 | | { |
| | 1 | 181 | | if (condition) |
| | | 182 | | { |
| | 0 | 183 | | paramName = NormalizeParamName(paramName); |
| | 0 | 184 | | ThrowArgumentOutOfRangeException(paramName, actualValue, message.GetFormattedText()); |
| | | 185 | | } |
| | 1 | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Throws an <see cref="ArgumentOutOfRangeException"/> if a copy destination index is outside [0, length]. |
| | | 190 | | /// </summary> |
| | | 191 | | /// <param name="index">The destination index to check.</param> |
| | | 192 | | /// <param name="length">The destination length.</param> |
| | | 193 | | /// <param name="paramName">The name of the parameter that caused the exception.</param> |
| | | 194 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is outside [0, length].</exception> |
| | | 195 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 196 | | public static void ThrowIfArrayIndexInvalid( |
| | | 197 | | int index, |
| | | 198 | | int length, |
| | | 199 | | [CallerArgumentExpression(nameof(index))] string? paramName = null) |
| | | 200 | | { |
| | 194 | 201 | | if ((uint)index > (uint)length) |
| | | 202 | | { |
| | 7 | 203 | | paramName = NormalizeParamName(paramName); |
| | 7 | 204 | | ThrowArgumentOutOfRangeException(paramName, index, "Array index is out of range."); |
| | | 205 | | } |
| | 187 | 206 | | } |
| | | 207 | | |
| | | 208 | | [DoesNotReturn] |
| | | 209 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 210 | | private static void ThrowArgumentOutOfRangeException(string? paramName, object? actualValue, string message) => |
| | 18 | 211 | | throw new ArgumentOutOfRangeException(paramName, actualValue, message); |
| | | 212 | | |
| | | 213 | | #endregion |
| | | 214 | | |
| | | 215 | | #region Invalid State Validation |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Throws an <see cref="ArgumentException"/> if the specified condition is true. |
| | | 219 | | /// </summary> |
| | | 220 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 221 | | /// <param name="paramName">The name of the parameter that caused the exception.</param> |
| | | 222 | | /// <param name="message">An optional message to include in the exception.</param> |
| | | 223 | | /// <exception cref="ArgumentException">Thrown when the condition is true.</exception> |
| | | 224 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 225 | | public static void ThrowIfArgument( |
| | | 226 | | [DoesNotReturnIf(true)] bool condition, |
| | | 227 | | string? paramName = null, |
| | | 228 | | string? message = null) |
| | | 229 | | { |
| | 219 | 230 | | if (condition) |
| | | 231 | | { |
| | 31 | 232 | | paramName = NormalizeParamName(paramName); |
| | 31 | 233 | | ThrowArgumentException(paramName, message ?? GetArgumentMessage(paramName)); |
| | | 234 | | } |
| | 188 | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Throws an <see cref="ArgumentException"/> with a lazily formatted message if the specified condition is true. |
| | | 239 | | /// </summary> |
| | | 240 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 241 | | /// <param name="message">The interpolated exception message.</param> |
| | | 242 | | /// <exception cref="ArgumentException">Thrown when the condition is true.</exception> |
| | | 243 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 244 | | public static void ThrowIfArgument( |
| | | 245 | | [DoesNotReturnIf(true)] bool condition, |
| | | 246 | | [InterpolatedStringHandlerArgument(nameof(condition))] SwiftThrowInterpolatedStringHandler message) |
| | | 247 | | { |
| | 0 | 248 | | if (condition) |
| | 0 | 249 | | ThrowArgumentException(null, message.GetFormattedText()); |
| | 0 | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Throws an <see cref="ArgumentException"/> with a lazily formatted message if the specified condition is true. |
| | | 254 | | /// </summary> |
| | | 255 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 256 | | /// <param name="paramName">The name of the parameter that caused the exception.</param> |
| | | 257 | | /// <param name="message">The interpolated exception message.</param> |
| | | 258 | | /// <exception cref="ArgumentException">Thrown when the condition is true.</exception> |
| | | 259 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 260 | | public static void ThrowIfArgument( |
| | | 261 | | [DoesNotReturnIf(true)] bool condition, |
| | | 262 | | string? paramName, |
| | | 263 | | [InterpolatedStringHandlerArgument(nameof(condition))] SwiftThrowInterpolatedStringHandler message) |
| | | 264 | | { |
| | 2 | 265 | | if (condition) |
| | | 266 | | { |
| | 1 | 267 | | paramName = NormalizeParamName(paramName); |
| | 1 | 268 | | ThrowArgumentException(paramName, message.GetFormattedText()); |
| | | 269 | | } |
| | 1 | 270 | | } |
| | | 271 | | |
| | | 272 | | [DoesNotReturn] |
| | | 273 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 274 | | private static void ThrowArgumentException(string? paramName, string message) => |
| | 32 | 275 | | throw new ArgumentException(message, paramName); |
| | | 276 | | |
| | | 277 | | /// <summary> |
| | | 278 | | /// Throws an <see cref="InvalidOperationException"/> if the specified condition is true. |
| | | 279 | | /// </summary> |
| | | 280 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 281 | | /// <param name="objectName">The name of the object in an invalid state.</param> |
| | | 282 | | /// <param name="message">An optional message to include in the exception.</param> |
| | | 283 | | /// <exception cref="InvalidOperationException">Thrown when the condition is true.</exception> |
| | | 284 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 285 | | public static void ThrowIfTrue( |
| | | 286 | | [DoesNotReturnIf(true)] bool condition, |
| | | 287 | | string? objectName = null, |
| | | 288 | | string? message = null) |
| | | 289 | | { |
| | 1948 | 290 | | if (condition) |
| | 39 | 291 | | ThrowInvalidOperationException(message ?? GetInvalidOperationMessage(objectName)); |
| | 1909 | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// Throws an <see cref="InvalidOperationException"/> with a lazily formatted message if the specified condition is |
| | | 296 | | /// </summary> |
| | | 297 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 298 | | /// <param name="message">The interpolated exception message.</param> |
| | | 299 | | /// <exception cref="InvalidOperationException">Thrown when the condition is true.</exception> |
| | | 300 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 301 | | public static void ThrowIfTrue( |
| | | 302 | | [DoesNotReturnIf(true)] bool condition, |
| | | 303 | | [InterpolatedStringHandlerArgument(nameof(condition))] SwiftThrowInterpolatedStringHandler message) |
| | | 304 | | { |
| | 0 | 305 | | if (condition) |
| | 0 | 306 | | ThrowInvalidOperationException(message.GetFormattedText()); |
| | 0 | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary> |
| | | 310 | | /// Throws an <see cref="InvalidOperationException"/> with a lazily formatted message if the specified condition is |
| | | 311 | | /// </summary> |
| | | 312 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 313 | | /// <param name="objectName">The name of the object in an invalid state.</param> |
| | | 314 | | /// <param name="message">The interpolated exception message.</param> |
| | | 315 | | /// <exception cref="InvalidOperationException">Thrown when the condition is true.</exception> |
| | | 316 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 317 | | public static void ThrowIfTrue( |
| | | 318 | | [DoesNotReturnIf(true)] bool condition, |
| | | 319 | | string? objectName, |
| | | 320 | | [InterpolatedStringHandlerArgument(nameof(condition))] SwiftThrowInterpolatedStringHandler message) |
| | | 321 | | { |
| | 0 | 322 | | if (condition) |
| | 0 | 323 | | ThrowInvalidOperationException(message.GetFormattedText()); |
| | 0 | 324 | | } |
| | | 325 | | |
| | | 326 | | [DoesNotReturn] |
| | | 327 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 328 | | private static void ThrowInvalidOperationException(string message) => |
| | 39 | 329 | | throw new InvalidOperationException(message); |
| | | 330 | | |
| | | 331 | | /// <summary> |
| | | 332 | | /// Throws an <see cref="ObjectDisposedException"/> if the specified condition is true. |
| | | 333 | | /// </summary> |
| | | 334 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 335 | | /// <param name="objectName">The name of the object that has been disposed.</param> |
| | | 336 | | /// <param name="message">An optional message to include in the exception.</param> |
| | | 337 | | /// <exception cref="ObjectDisposedException">Thrown when the condition is true.</exception> |
| | | 338 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 339 | | public static void ThrowIfDisposed( |
| | | 340 | | [DoesNotReturnIf(true)] bool condition, |
| | | 341 | | string? objectName = null, |
| | | 342 | | string? message = null) |
| | | 343 | | { |
| | 235 | 344 | | if (condition) |
| | 20 | 345 | | ThrowObjectDisposedException(objectName, message ?? GetObjectDisposedMessage(objectName)); |
| | 215 | 346 | | } |
| | | 347 | | |
| | | 348 | | /// <summary> |
| | | 349 | | /// Throws an <see cref="ObjectDisposedException"/> with a lazily formatted message if the specified condition is tr |
| | | 350 | | /// </summary> |
| | | 351 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 352 | | /// <param name="message">The interpolated exception message.</param> |
| | | 353 | | /// <exception cref="ObjectDisposedException">Thrown when the condition is true.</exception> |
| | | 354 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 355 | | public static void ThrowIfDisposed( |
| | | 356 | | [DoesNotReturnIf(true)] bool condition, |
| | | 357 | | [InterpolatedStringHandlerArgument(nameof(condition))] SwiftThrowInterpolatedStringHandler message) |
| | | 358 | | { |
| | 0 | 359 | | if (condition) |
| | 0 | 360 | | ThrowObjectDisposedException(null, message.GetFormattedText()); |
| | 0 | 361 | | } |
| | | 362 | | |
| | | 363 | | /// <summary> |
| | | 364 | | /// Throws an <see cref="ObjectDisposedException"/> with a lazily formatted message if the specified condition is tr |
| | | 365 | | /// </summary> |
| | | 366 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 367 | | /// <param name="objectName">The name of the object that has been disposed.</param> |
| | | 368 | | /// <param name="message">The interpolated exception message.</param> |
| | | 369 | | /// <exception cref="ObjectDisposedException">Thrown when the condition is true.</exception> |
| | | 370 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 371 | | public static void ThrowIfDisposed( |
| | | 372 | | [DoesNotReturnIf(true)] bool condition, |
| | | 373 | | string? objectName, |
| | | 374 | | [InterpolatedStringHandlerArgument(nameof(condition))] SwiftThrowInterpolatedStringHandler message) |
| | | 375 | | { |
| | 0 | 376 | | if (condition) |
| | 0 | 377 | | ThrowObjectDisposedException(objectName, message.GetFormattedText()); |
| | 0 | 378 | | } |
| | | 379 | | |
| | | 380 | | [DoesNotReturn] |
| | | 381 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 382 | | private static void ThrowObjectDisposedException(string? objectName, string message) => |
| | 20 | 383 | | throw new ObjectDisposedException(objectName, message); |
| | | 384 | | |
| | | 385 | | /// <summary> |
| | | 386 | | /// Throws a <see cref="KeyNotFoundException"/> if the specified index is negative. |
| | | 387 | | /// </summary> |
| | | 388 | | /// <param name="index">The index to check.</param> |
| | | 389 | | /// <param name="key">The key associated with the index.</param> |
| | | 390 | | /// <exception cref="KeyNotFoundException">Thrown when the index is negative.</exception> |
| | | 391 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 392 | | public static void ThrowIfKeyInvalid(int index, object? key = null) |
| | | 393 | | { |
| | 4341 | 394 | | if (index < 0) |
| | 3 | 395 | | ThrowKeyNotFoundException(GetKeyNotFoundMessage(key)); |
| | 4338 | 396 | | } |
| | | 397 | | |
| | | 398 | | /// <summary> |
| | | 399 | | /// Throws a <see cref="KeyNotFoundException"/> if the specified condition is true. |
| | | 400 | | /// </summary> |
| | | 401 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 402 | | /// <param name="key">The key associated with the lookup.</param> |
| | | 403 | | /// <param name="message">An optional message to include in the exception.</param> |
| | | 404 | | /// <exception cref="KeyNotFoundException">Thrown when the condition is true.</exception> |
| | | 405 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 406 | | public static void ThrowIfKeyNotFound( |
| | | 407 | | [DoesNotReturnIf(true)] bool condition, |
| | | 408 | | object? key = null, |
| | | 409 | | string? message = null) |
| | | 410 | | { |
| | 42 | 411 | | if (condition) |
| | 3 | 412 | | ThrowKeyNotFoundException(message ?? GetKeyNotFoundMessage(key)); |
| | 39 | 413 | | } |
| | | 414 | | |
| | | 415 | | /// <summary> |
| | | 416 | | /// Throws a <see cref="KeyNotFoundException"/> with a lazily formatted message if the specified condition is true. |
| | | 417 | | /// </summary> |
| | | 418 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 419 | | /// <param name="message">The interpolated exception message.</param> |
| | | 420 | | /// <exception cref="KeyNotFoundException">Thrown when the condition is true.</exception> |
| | | 421 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 422 | | public static void ThrowIfKeyNotFound( |
| | | 423 | | [DoesNotReturnIf(true)] bool condition, |
| | | 424 | | [InterpolatedStringHandlerArgument(nameof(condition))] SwiftThrowInterpolatedStringHandler message) |
| | | 425 | | { |
| | 0 | 426 | | if (condition) |
| | 0 | 427 | | ThrowKeyNotFoundException(message.GetFormattedText()); |
| | 0 | 428 | | } |
| | | 429 | | |
| | | 430 | | /// <summary> |
| | | 431 | | /// Throws a <see cref="KeyNotFoundException"/> with a lazily formatted message if the specified condition is true. |
| | | 432 | | /// </summary> |
| | | 433 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 434 | | /// <param name="key">The key associated with the lookup.</param> |
| | | 435 | | /// <param name="message">The interpolated exception message.</param> |
| | | 436 | | /// <exception cref="KeyNotFoundException">Thrown when the condition is true.</exception> |
| | | 437 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 438 | | public static void ThrowIfKeyNotFound( |
| | | 439 | | [DoesNotReturnIf(true)] bool condition, |
| | | 440 | | object? key, |
| | | 441 | | [InterpolatedStringHandlerArgument(nameof(condition))] SwiftThrowInterpolatedStringHandler message) |
| | | 442 | | { |
| | 0 | 443 | | if (condition) |
| | 0 | 444 | | ThrowKeyNotFoundException(message.GetFormattedText()); |
| | 0 | 445 | | } |
| | | 446 | | |
| | | 447 | | [DoesNotReturn] |
| | | 448 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 449 | | private static void ThrowKeyNotFoundException(string message) => |
| | 6 | 450 | | throw new KeyNotFoundException(message); |
| | | 451 | | |
| | | 452 | | /// <summary> |
| | | 453 | | /// Throws an <see cref="IndexOutOfRangeException"/> if the specified index is outside the valid range defined by co |
| | | 454 | | /// </summary> |
| | | 455 | | /// <param name="index">The index to check.</param> |
| | | 456 | | /// <param name="count">The total number of elements in the collection.</param> |
| | | 457 | | /// <exception cref="IndexOutOfRangeException">Thrown when the index is outside the valid range.</exception> |
| | | 458 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 459 | | public static void ThrowIfListIndexInvalid( |
| | | 460 | | int index, |
| | | 461 | | int count) |
| | | 462 | | { |
| | 8195 | 463 | | if ((uint)index >= (uint)count) |
| | 11 | 464 | | ThrowIndexOutOfRangeException(index); |
| | 8184 | 465 | | } |
| | | 466 | | |
| | | 467 | | [DoesNotReturn] |
| | | 468 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 469 | | private static void ThrowIndexOutOfRangeException(int value) => |
| | 11 | 470 | | throw new IndexOutOfRangeException($"Index out of range: {value}"); |
| | | 471 | | |
| | | 472 | | #endregion |
| | | 473 | | |
| | | 474 | | #region Message Helpers |
| | | 475 | | |
| | | 476 | | private static string GetNonNegativeMessage(string? paramName) => |
| | 5 | 477 | | string.IsNullOrEmpty(paramName) |
| | 5 | 478 | | ? "Value must be non-negative." |
| | 5 | 479 | | : $"{paramName} must be non-negative."; |
| | | 480 | | |
| | | 481 | | private static string GetPositiveMessage(string? paramName) => |
| | 2 | 482 | | string.IsNullOrEmpty(paramName) |
| | 2 | 483 | | ? "Value must be greater than zero." |
| | 2 | 484 | | : $"{paramName} must be greater than zero."; |
| | | 485 | | |
| | | 486 | | private static string GetArgumentOutOfRangeMessage(string? paramName) => |
| | 2 | 487 | | string.IsNullOrEmpty(paramName) |
| | 2 | 488 | | ? "Specified argument was out of range." |
| | 2 | 489 | | : $"{paramName} is out of range."; |
| | | 490 | | |
| | | 491 | | private static string GetArgumentMessage(string? paramName) => |
| | 0 | 492 | | string.IsNullOrEmpty(paramName) |
| | 0 | 493 | | ? "The argument is invalid." |
| | 0 | 494 | | : $"{paramName} is invalid."; |
| | | 495 | | |
| | | 496 | | private static string GetInvalidOperationMessage(string? objectName) => |
| | 2 | 497 | | string.IsNullOrEmpty(objectName) |
| | 2 | 498 | | ? "Operation is not valid in the current state." |
| | 2 | 499 | | : $"Object '{objectName}' is in an invalid state."; |
| | | 500 | | |
| | | 501 | | private static string GetObjectDisposedMessage(string? objectName) => |
| | 20 | 502 | | string.IsNullOrEmpty(objectName) |
| | 20 | 503 | | ? "Object has been disposed." |
| | 20 | 504 | | : $"Object '{objectName}' has been disposed."; |
| | | 505 | | |
| | | 506 | | private static string GetKeyNotFoundMessage(object? key) => |
| | 6 | 507 | | key is null |
| | 6 | 508 | | ? "Key was not found." |
| | 6 | 509 | | : $"Key not found: {key}"; |
| | | 510 | | |
| | | 511 | | private static string? NormalizeParamName(string? paramName) |
| | | 512 | | { |
| | 86 | 513 | | if (string.IsNullOrEmpty(paramName) || paramName == "null") |
| | 1 | 514 | | return null; |
| | | 515 | | |
| | 85 | 516 | | char first = paramName[0]; |
| | 85 | 517 | | return char.IsLetter(first) || first == '_' || first == '@' |
| | 85 | 518 | | ? paramName |
| | 85 | 519 | | : null; |
| | | 520 | | } |
| | | 521 | | |
| | | 522 | | #endregion |
| | | 523 | | } |