Skip to content

Commit 42b2fe5

Browse files
feat: Get-Turtle performance ( Fixes PoshWeb#368 )
1 parent b9dc235 commit 42b2fe5

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

Commands/Get-Turtle.ps1

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,13 @@ function Get-Turtle {
10361036
# and keep track of when it became unbalanced.
10371037
$unbalancedAt = $null
10381038
foreach ($match in [Regex]::Matches(
1039-
($wordsAndArguments -join ' ' ), '[\[\]]'
1039+
(@(
1040+
foreach ($arg in $wordsAndArguments) {
1041+
if ($arg -is [string]) {
1042+
$arg
1043+
}
1044+
}
1045+
) -join ' '), '[\[\]]'
10401046
)
10411047
) {
10421048
# To do this, we increment or decrement depth for brackets `[]`
@@ -1089,7 +1095,7 @@ $(
10891095
for ($argIndex =0; $argIndex -lt $wordsAndArguments.Length; $argIndex++) {
10901096
$arg = $wordsAndArguments[$argIndex]
10911097
# If the argument is not in the member names list, we can complain about it.
1092-
if ($arg -notin $memberNames) {
1098+
if ($arg -is [string] -and $arg -notin $memberNames) {
10931099
if (
10941100
# (we might not want to, if it starts with a bracket)
10951101
-not $currentMember -and $arg -is [string] -and
@@ -1140,13 +1146,12 @@ $(
11401146
if ("$bracket" -eq '[') { $bracketDepth++ }
11411147
if ("$bracket" -eq ']') { $bracketDepth-- }
11421148
}
1143-
}
1144-
# If the next word is a method name, and our brackets are balanced
1145-
if ($wordsAndArguments[$methodArgIndex] -in $memberNames -and -not $bracketDepth) {
1146-
# break out of the loop.
1147-
break
1149+
# If the next word is a method name, and our brackets are balanced
1150+
if ($wordsAndArguments[$methodArgIndex] -in $memberNames -and -not $bracketDepth) {
1151+
# break out of the loop.
1152+
break
1153+
}
11481154
}
1149-
11501155
}
11511156
# Now we know how far we had to look to get to the next member name.
11521157

@@ -1164,7 +1169,10 @@ $(
11641169
$HelpWanted = $true
11651170
continue
11661171
}
1167-
if ($HelpWanted -and $word -in 'example', 'examples', 'parameter','parameters','online') {
1172+
if ($HelpWanted -and
1173+
$word -is [string] -and
1174+
$word -in 'example', 'examples', 'parameter','parameters','online'
1175+
) {
11681176
if ($word -in 'example','examples') {
11691177
$switches['Examples'] = $true
11701178
}
@@ -1176,7 +1184,7 @@ $(
11761184
}
11771185
continue
11781186
}
1179-
if ($word -match '^[-/]+?[\D-[\.]]') {
1187+
if ($word -is [string] -and $word -match '^[-/]+?[\D-[\.]]') {
11801188
$switchInfo = $word -replace '^[-/]+'
11811189
$switchName, $switchValue = $switchInfo -split ':', 2
11821190
if ($null -eq ($switchName -as [double])) {
@@ -1190,12 +1198,12 @@ $(
11901198
}
11911199
}
11921200
# If the word started with a bracket, and we haven't removed any
1193-
if ("$word".StartsWith('[') -and -not $debracketCount) {
1201+
if ($word -is [string] -and $word.StartsWith('[') -and -not $debracketCount) {
11941202
$word = $word -replace '^\[' # remove it
11951203
$debracketCount++ # and increment our removal counter.
11961204
}
11971205
# If the word ended with a bracket, and we have debracketed once
1198-
if ("$word".EndsWith(']') -and $debracketCount -eq 1) {
1206+
if ($word -is [string] -and $word.EndsWith(']') -and $debracketCount -eq 1) {
11991207
# remove the closing bracket
12001208
$word = $word -replace '\]$'
12011209
# and increment our removal counter

0 commit comments

Comments
 (0)