From 0b42d92d4d341ad5a71a576e8d416e4646acab81 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 25 May 2023 19:12:42 +0200 Subject: [PATCH] six files (#19695) --- .../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 79c53d04ce..5ca98664aa 100644 --- a/src/Microsoft.Management.UI.Internal/HelpWindow/ParagraphBuilder.cs +++ b/src/Microsoft.Management.UI.Internal/HelpWindow/ParagraphBuilder.cs @@ -276,15 +276,8 @@ namespace Microsoft.Management.UI.Internal /// 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 @@ namespace Microsoft.Management.UI.Internal /// 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 7a3eb0ce32..8c59b551c8 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 @@ namespace Microsoft.PowerShell.Cim 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 c1643218ce..9b9fe1aff1 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 @@ namespace Microsoft.PowerShell.Commands /// 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 @@ namespace Microsoft.PowerShell.Commands 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 cd5b33107a..559c4ade73 100644 --- a/src/System.Management.Automation/engine/ProgressRecord.cs +++ b/src/System.Management.Automation/engine/ProgressRecord.cs @@ -360,15 +360,8 @@ namespace System.Management.Automation 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 22bdd8bcd3..c1a134cdfb 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 @@ namespace System.Management.Automation.Runspaces 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 0c4658cc14..0ac1db53ff 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 @@ namespace System.Management.Automation { 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) {