Skip to content
Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Management.Automation;

namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Implementation of the Add-String command
/// </summary>
[Cmdlet(VerbsCommon.Add, "String", DefaultParameterSetName = "wrap")]
[OutputType(typeof(string))]
public sealed class AddStringCommand : Cmdlet
{
#region Parameters
/// <summary>
/// The character to pad the input string with on the left side
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "padLeft")]
public char PadLeft;

/// <summary>
/// The character to pad the input string with on the right side
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "padRight")]
public char PadRight;

/// <summary>
/// To what total string width to pad
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "padLeft")]
[Parameter(Mandatory = true, ParameterSetName = "padRight")]
public int PadWidth;

/// <summary>
/// The string to add before the input
/// </summary>
[Parameter(Position = 0, ParameterSetName = "wrap")]
public string Before;

/// <summary>
/// The string to add behind the input
/// </summary>
[Parameter(Position = 1, ParameterSetName = "wrap")]
public string Behind;

/// <summary>
/// The string to add to
/// </summary>
[Parameter(Mandatory = true, ValueFromPipeline = true)]
public string[] InputString;
#endregion Parameters

#region Methods
/// <summary>
/// Process each string as it is passed through.
/// </summary>
protected override void ProcessRecord()
{
foreach (string line in InputString)
if (_ParameterSetName == "wrap")
WriteObject(String.Format("{0}{1}{2}", Before, line, Behind));
else if (_ParameterSetName == "padRight")
WriteObject(line.PadRight(PadWidth, PadRight));
else
WriteObject(line.PadLeft(PadWidth, PadLeft));
}
#endregion Methods
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Implementation of the Format-String command
/// </summary>
[Cmdlet(VerbsCommon.Format, "String")]
[OutputType(typeof(string))]
[Alias("format")]
public class FormatStringCommand : Cmdlet
{
#region Parameters
/// <summary>
/// The formatting string to use to format the input with
/// </summary>
[Parameter(Position = 0, Mandatory = true)]
public string Format;

/// <summary>
/// The number of items to format in bulk
/// </summary>
[Parameter(Position = 1)]
public int Count = 1;

/// <summary>
/// Object(s) to format
/// </summary>
[Parameter(Mandatory = true, ValueFromPipeline = true)]
public object[] InputObject;
#endregion Parameters

#region Internal Properties
/// <summary>
/// The property where cached items for bulk formatting are stored
/// </summary>
private List<object> ItemCache { get; set; } = new List<object>();
#endregion Internal Properties

#region Methods
/// <summary>
/// Processes each item for formatting
/// </summary>
protected override void ProcessRecord()
{
foreach (object item in InputObject)
{
if (Count == 1)
WriteObject(String.Format(Format, item));
else
{
ItemCache.Add(item);
if (ItemCache.Count == Count)
{
WriteObject(String.Format(Format, ItemCache.Select(x => x).ToArray()));
ItemCache.Clear();
}
}
}
}

/// <summary>
/// Flushes the final objects if present
/// </summary>
protected override void EndProcessing()
{
if ((Count > 1) && (ItemCache.Count > 0))
{
while (ItemCache.Count < Count)
ItemCache.Add(null);

WriteObject(String.Format(Format, ItemCache.Select(x => x).ToArray()));
}
}
#endregion Methods
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Management.Automation;

namespace Microsoft.PowerShell.Commands.Utility.commands.utility
{
/// <summary>
/// Implements Get-SubString command
/// </summary>
[Cmdlet(VerbsCommon.Get, "SubString", DefaultParameterSetName = "substring")]
[OutputType(typeof(string))]
public class GetSubStringCommand : Cmdlet
{
#region Parameters
/// <summary>
/// What string to trim with
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "trim")]
public string Trim;

/// <summary>
/// What string to trim the start with
/// </summary>
[Parameter(ParameterSetName = "trimpartial")]
public string TrimStart;

/// <summary>
/// What string to trim the end with
/// </summary>
[Parameter(ParameterSetName = "trimpartial")]
public string TrimEnd;

/// <summary>
/// Where to start taking a substring
/// </summary>
[Parameter(Position = 0, Mandatory = true, ParameterSetName = "substring")]
public int Start;

/// <summary>
/// How long the picked substring should be
/// </summary>
[Parameter(Position = 1, ParameterSetName = "substring")]
public int Length;

/// <summary>
/// The strings to process
/// </summary>
[Parameter(Mandatory = true, ValueFromPipeline = true)]
public string[] InputString;
#endregion Parameters

#region Methods
/// <summary>
/// Processes the individual items that were input
/// </summary>
protected override void ProcessRecord()
{
foreach (string item in InputString)
{
switch (_ParameterSetName)
{
case "substring":
if (Length > 0)
WriteObject(item.Substring(Start, Length));
else
WriteObject(item.Substring(Start));
break;
case "trim":
WriteObject(item.Trim(Trim.ToCharArray()));
break;
case "trimpartial":
if (!String.IsNullOrEmpty(TrimStart) && !String.IsNullOrEmpty(TrimEnd))
WriteObject(item.TrimStart(TrimStart.ToCharArray()).TrimEnd(TrimEnd.ToCharArray()));
else if (!String.IsNullOrEmpty(TrimStart))
WriteObject(item.TrimStart(TrimStart.ToCharArray()));
else
WriteObject(item.TrimEnd(TrimEnd.ToCharArray()));
break;
default:
break;
}
}
}
#endregion Methods
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Text.RegularExpressions;

namespace Microsoft.PowerShell.Commands.Utility.commands.utility
{
/// <summary>
/// Implements the Set-String command
/// </summary>
[Cmdlet(VerbsCommon.Set, "String", DefaultParameterSetName = "regex")]
[OutputType(typeof(string))]
[Alias("replace")]
public class SetStringCommand : PSCmdlet
{
#region Parameters
/// <summary>
/// What to replace
/// </summary>
[Parameter(Position = 0, Mandatory = true)]
public string OldValue;

/// <summary>
/// What to replace with
/// </summary>
[Parameter(Position = 1)]
public object NewValue;

/// <summary>
/// Whether the simple string Replace() method should be used instead
/// </summary>
[Parameter(ParameterSetName = "simple")]
[Alias("simple")]
public SwitchParameter DoNotUseRegex;

/// <summary>
/// The regex options to apply while replacing
/// </summary>
[Parameter(ParameterSetName = "regex")]
public RegexOptions Options = RegexOptions.IgnoreCase;

/// <summary>
/// The strings to update
/// </summary>
[Parameter(Mandatory = true, ValueFromPipeline = true)]
public string[] InputString;
#endregion Parameters

#region Fields
/// <summary>
/// Stores the string value of the With parameter, if applicable.
/// </summary>
private string StringValue;

/// <summary>
/// Stores the scriptblock value of the With parameter, if applicable.
/// </summary>
private ScriptBlock ScriptBlockValue;

/// <summary>
/// Evaluates the replace match.
/// </summary>
private MatchEvaluator ScriptBlockEvaluator;
#endregion Fields

#region Methods
/// <summary>
/// Handles the input conversion on the What parameter
/// </summary>
protected override void BeginProcessing()
{
if (NewValue == null)
StringValue = "";
else if (!DoNotUseRegex.ToBool() && (NewValue is ScriptBlock))
{
ScriptBlockValue = (ScriptBlock)NewValue;
ScriptBlockEvaluator = match => {
var result = ScriptBlockValue.DoInvokeReturnAsIs(
useLocalScope: false, /* Use current scope to be consistent with 'ForEach/Where-Object {}' and 'collection.ForEach{}/Where{}' */
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
dollarUnder: match,
input: AutomationNull.Value,
scriptThis: AutomationNull.Value,
args: Utils.EmptyArray<object>());

return PSObject.ToStringParser(Context, result); ;
};
}
else
StringValue = NewValue.ToString();
}

/// <summary>
/// Processes each input item
/// </summary>
protected override void ProcessRecord()
{
foreach (string item in InputString)
{
if (DoNotUseRegex.ToBool())
WriteObject(item.Replace(OldValue, StringValue));
else if (ScriptBlockValue != null)
WriteObject(Regex.Replace(item, OldValue, ScriptBlockEvaluator, Options));
else
WriteObject(Regex.Replace(item, OldValue, StringValue, Options));
}
}
#endregion Methods
}
}
Loading