Functions/Get-CommonSuffix.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
46
47
48
49
50
51
52
53
54
55
56
57
58
function Get-CommonSuffix {
    <#
        .SYNOPSIS
            Find the common suffix of two strings.
        .DESCRIPTION
            This function will get the common suffix of two strings; that is, all
            the letters that they share, starting from the end of the strings.
        .EXAMPLE
            Get-CommonSuffix 'Morning' 'Lasting'
            Will get the common suffix of both string. Should output 'ing'.
        .LINK
            https://communary.wordpress.com/
            https://github.com/gravejester/Communary.PASM
        .INPUTS
            System.String
        .OUTPUTS
            System.String
        .NOTES
            Author: Øyvind Kallstad
            Date: 03.11.2014
            Version 1.1
            Dependencies: Get-CommonPrefix
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$String1,

        [Parameter(Mandatory = $true, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [string]$String2,

        # Maximum length of the returned suffix.
        [Parameter()]
        [int]$MaxSuffixLength,

        # Makes matches case-sensitive. By default, matches are not case-sensitive.
        [Parameter()]
        [switch] $CaseSensitive
    )

    if (-not($CaseSensitive)) {
        $String1 = $String1.ToLowerInvariant()
        $String2 = $String2.ToLowerInvariant()
    }

    $shortestStringLength = [Math]::Min($String1.Length,$String2.Length)

    # Let the maximum prefix length be the same as the length of the shortest of
    # the two input strings, unless defined by the MaxPrefixLength parameter.
    if (($shortestStringLength -lt $MaxSuffixLength) -or ($MaxSuffixLength -eq 0)) {
        $MaxSuffixLength = $shortestStringLength
    }

    $tmp = Get-CommonPrefix ($String1[-1..-($String1.Length)] -join '') ($String2[-1..-($String2.Length)] -join '') -MaxPrefixLength $MaxSuffixLength -CaseSensitive:$CaseSensitive
    Write-Output ($tmp[-1..-($tmp.Length)] -join '')
}