From 33ef997491219d9166f3e8d32ba2993c0aeac1c6 Mon Sep 17 00:00:00 2001
From: CarloToso <105941898+CarloToso@users.noreply.github.com>
Date: Mon, 22 May 2023 20:24:14 +0200
Subject: [PATCH] six files
---
.../HelpWindow/ParagraphBuilder.cs | 22 ++++---------------
.../cmdletization/cim/cimConverter.cs | 12 +++-------
.../commands/utility/MatchString.cs | 13 +++--------
.../engine/ProgressRecord.cs | 11 ++--------
.../engine/TypeTable_Types_Ps1Xml.cs | 5 +----
.../engine/runtime/Operations/StringOps.cs | 7 ++----
6 files changed, 15 insertions(+), 55 deletions(-)
diff --git a/src/Microsoft.Management.UI.Internal/HelpWindow/ParagraphBuilder.cs b/src/Microsoft.Management.UI.Internal/HelpWindow/ParagraphBuilder.cs
index 79c53d04ced..5ca98664aa8 100644
--- a/src/Microsoft.Management.UI.Internal/HelpWindow/ParagraphBuilder.cs
+++ b/src/Microsoft.Management.UI.Internal/HelpWindow/ParagraphBuilder.cs
@@ -276,15 +276,8 @@ private static void MoveSpanToPosition(ref int currentSpanIndex, ref TextSpan? c
/// Highlight length.
private void AddHighlight(int start, int length)
{
- if (start < 0)
- {
- throw new ArgumentOutOfRangeException("start");
- }
-
- if (start + length > this.textBuilder.Length)
- {
- throw new ArgumentOutOfRangeException("length");
- }
+ ArgumentOutOfRangeException.ThrowIfNegative(start);
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(this.textBuilder.Length, start + length, nameof(length));
this.highlightedSpans.Add(new TextSpan(start, length));
}
@@ -324,15 +317,8 @@ internal struct TextSpan
/// Index of the last character in the span.
internal TextSpan(int start, int length)
{
- if (start < 0)
- {
- throw new ArgumentOutOfRangeException("start");
- }
-
- if (length < 1)
- {
- throw new ArgumentOutOfRangeException("length");
- }
+ ArgumentOutOfRangeException.ThrowIfNegative(start);
+ ArgumentOutOfRangeException.ThrowIfLessThan(1, length);
this.start = start;
this.end = start + length - 1;
diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimConverter.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimConverter.cs
index 7a3eb0ce326..8c59b551c82 100644
--- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimConverter.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimConverter.cs
@@ -50,15 +50,9 @@ internal SensitiveString(int numberOfCharacters)
private unsafe void Copy(char* source, int offset, int charsToCopy)
{
- if ((offset < 0) || (offset >= _string.Length))
- {
- throw new ArgumentOutOfRangeException(nameof(offset));
- }
-
- if (offset + charsToCopy > _string.Length)
- {
- throw new ArgumentOutOfRangeException(nameof(charsToCopy));
- }
+ ArgumentOutOfRangeException.ThrowIfNegative(offset);
+ ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(_string.Length, offset);
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(_string.Length, offset + charsToCopy, nameof(charsToCopy));
fixed (char* target = _string)
{
diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs
index c1643218ce1..9b9fe1aff13 100644
--- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs
+++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs
@@ -428,10 +428,7 @@ private sealed class CircularBuffer : ICollection
/// If is negative.
public CircularBuffer(int capacity)
{
- if (capacity < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(capacity));
- }
+ ArgumentOutOfRangeException.ThrowIfNegative(capacity);
_items = new T[capacity];
Clear();
@@ -532,12 +529,8 @@ public bool Contains(T item)
public void CopyTo(T[] array, int arrayIndex)
{
- ArgumentNullException.ThrowIfNull(array);
-
- if (arrayIndex < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(arrayIndex));
- }
+ ArgumentNullException.ThrowIfNull(array);
+ ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
if (Count > (array.Length - arrayIndex))
{
diff --git a/src/System.Management.Automation/engine/ProgressRecord.cs b/src/System.Management.Automation/engine/ProgressRecord.cs
index cd5b33107aa..559c4ade73f 100644
--- a/src/System.Management.Automation/engine/ProgressRecord.cs
+++ b/src/System.Management.Automation/engine/ProgressRecord.cs
@@ -360,15 +360,8 @@ internal static int GetPercentageComplete(DateTime startTime, TimeSpan expectedD
startTime.Kind == DateTimeKind.Utc,
"DateTime arithmetic should always be done in utc mode [to avoid problems when some operands are calculated right before and right after switching to /from a daylight saving time");
- if (startTime > now)
- {
- throw new ArgumentOutOfRangeException(nameof(startTime));
- }
-
- if (expectedDuration <= TimeSpan.Zero)
- {
- throw new ArgumentOutOfRangeException(nameof(expectedDuration));
- }
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(now, startTime);
+ ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(TimeSpan.Zero, expectedDuration);
/*
* According to the spec of Checkpoint-Computer
diff --git a/src/System.Management.Automation/engine/TypeTable_Types_Ps1Xml.cs b/src/System.Management.Automation/engine/TypeTable_Types_Ps1Xml.cs
index 22bdd8bcd35..c1a134cdfbf 100644
--- a/src/System.Management.Automation/engine/TypeTable_Types_Ps1Xml.cs
+++ b/src/System.Management.Automation/engine/TypeTable_Types_Ps1Xml.cs
@@ -17,10 +17,7 @@ public sealed partial class TypeTable
private static Func> GetValueFactoryBasedOnInitCapacity(int capacity)
{
- if (capacity <= 0)
- {
- throw new ArgumentOutOfRangeException(nameof(capacity));
- }
+ ArgumentOutOfRangeException.ThrowIfNegativeOrZero(capacity);
if (capacity > ValueFactoryCacheCount)
{
diff --git a/src/System.Management.Automation/engine/runtime/Operations/StringOps.cs b/src/System.Management.Automation/engine/runtime/Operations/StringOps.cs
index 0c4658cc14d..0ac1db53ff9 100644
--- a/src/System.Management.Automation/engine/runtime/Operations/StringOps.cs
+++ b/src/System.Management.Automation/engine/runtime/Operations/StringOps.cs
@@ -28,11 +28,8 @@ internal static string Multiply(string s, int times)
{
Diagnostics.Assert(s != null, "caller to verify argument is not null");
- if (times < 0)
- {
- // TODO: this should be a runtime error.
- throw new ArgumentOutOfRangeException(nameof(times));
- }
+ // TODO: this should be a runtime error.
+ ArgumentOutOfRangeException.ThrowIfNegative(times);
if (times == 0 || s.Length == 0)
{