functions/New-SBStringBuilder.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function New-SBStringBuilder
{
<#
    .SYNOPSIS
        Create a new stringbuilder object.
     
    .DESCRIPTION
        Create a new stringbuilder object.
     
    .PARAMETER Register
        Register the string builder under the name (if specified) or the current module (if not so).
     
    .PARAMETER Name
        Name under which to register a string builder.
        Defaults to the caller's module name (if present) or '<none>' (if not so)
     
    .EXAMPLE
        PS C:\> New-SBStringBuilder
     
        Create a new string builder under the current module's name.
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [OutputType([System.Text.StringBuilder])]
    [CmdletBinding()]
    Param (
        [switch]
        $Register,
        
        [PsfArgumentCompleter('StringBuilder.Name')]
        [string]
        $Name
    )
    
    begin{
        $sbName = [PSFramework.Utility.UtilityHost]::FriendlyCallstack.Entries[1].InvocationInfo.MyCommand.Module.Name
        if (-not $sbName) { $sbName = '<none>' }
        if ($Name) { $sbName = $Name }
    }
    process
    {
        $stringBuilder = [System.Text.StringBuilder]::new()
        if ($Register -or $Name) { $script:builders[$sbName] = $stringBuilder }
        $stringBuilder
    }
}