From 690f7d3ad407fd8763b588fa741dfc7a0c3773f1 Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sat, 23 Feb 2019 03:04:25 -0600 Subject: [PATCH 01/23] fixing merge conflicts 1 --- .../engine/lang/parserutils.cs | 15 +++++++-------- .../Language/Operators/SplitOperator.Tests.ps1 | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index fb2c582c299..22f27a79976 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -743,23 +743,22 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e separatorPattern = Regex.Escape(separatorPattern); } - if (limit < 0) - { - // Regex only allows 0 to signify "no limit", whereas - // we allow any integer <= 0. - limit = 0; - } - RegexOptions regexOptions = parseRegexOptions(options); + // if the limit is negative then set Regex to read from right to left + if (limit < 0) { + regexOptions |= RegexOptions.RightToLeft; + limit = -limit; + } Regex regex = NewRegex(separatorPattern, regexOptions); List results = new List(); + foreach (string item in content) { string[] split = regex.Split(item, limit, 0); results.AddRange(split); } - + return results.ToArray(); } diff --git a/test/powershell/Language/Operators/SplitOperator.Tests.ps1 b/test/powershell/Language/Operators/SplitOperator.Tests.ps1 index 1e508f4a47a..581b8a07da7 100644 --- a/test/powershell/Language/Operators/SplitOperator.Tests.ps1 +++ b/test/powershell/Language/Operators/SplitOperator.Tests.ps1 @@ -33,7 +33,25 @@ Describe "Split Operator" -Tags CI { $res[2] | Should -Be "c" $res[3] | Should -Be "d" + $res = "a b c d" -split " ", -2 + $res.count | Should -Be 2 + $res[0] | Should -Be "a b c" + $res[1] | Should -Be "d" + $res = "a b c d" -split " ", -1 + $res.count | Should -Be 1 + $res[0] | Should -Be "a b c d" + } + + It "Binary split operator can work with greater than max substring" { + $res = "a b c d" -split " ",8 + $res.count | Should -Be 4 + $res[0] | Should -Be "a" + $res[1] | Should -Be "b" + $res[2] | Should -Be "c" + $res[3] | Should -Be "d" + + $res = "a b c d" -split " ",-8 $res.count | Should -Be 4 $res[0] | Should -Be "a" $res[1] | Should -Be "b" From ce4b48fe6db225a004d8eeb58a2fd74c71bbff2e Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sat, 23 Feb 2019 09:36:42 -0600 Subject: [PATCH 02/23] [feature] touch up white space and newlines --- src/System.Management.Automation/engine/lang/parserutils.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 22f27a79976..9d2c31b6e88 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -744,11 +744,13 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e } RegexOptions regexOptions = parseRegexOptions(options); + // if the limit is negative then set Regex to read from right to left if (limit < 0) { regexOptions |= RegexOptions.RightToLeft; limit = -limit; } + Regex regex = NewRegex(separatorPattern, regexOptions); List results = new List(); @@ -758,7 +760,7 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e string[] split = regex.Split(item, limit, 0); results.AddRange(split); } - + return results.ToArray(); } From ae5a1e5a96788a25b9ca22608b93416436b8e309 Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sat, 23 Feb 2019 09:51:12 -0600 Subject: [PATCH 03/23] [feature] but open curly brace on it's own line --- src/System.Management.Automation/engine/lang/parserutils.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 9d2c31b6e88..36ab05a8abf 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -746,7 +746,8 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e RegexOptions regexOptions = parseRegexOptions(options); // if the limit is negative then set Regex to read from right to left - if (limit < 0) { + if (limit < 0) + { regexOptions |= RegexOptions.RightToLeft; limit = -limit; } From bbfbfcc2c39f8bccc83d56934f736efad269039f Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sat, 23 Feb 2019 11:00:01 -0600 Subject: [PATCH 04/23] [feature] styling change --- src/System.Management.Automation/engine/lang/parserutils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 36ab05a8abf..f6446f5a8ab 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -749,7 +749,7 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e if (limit < 0) { regexOptions |= RegexOptions.RightToLeft; - limit = -limit; + limit *= -1; } Regex regex = NewRegex(separatorPattern, regexOptions); From 8ccb6c9b7f7558dcb8d5d029753865389bb747fe Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sat, 23 Feb 2019 13:18:39 -0600 Subject: [PATCH 05/23] fixing merge conflicts 2 --- .../engine/lang/parserutils.cs | 11 +++++++++-- .../Operators/SplitOperator.Tests.ps1 | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index f6446f5a8ab..510406d2458 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -745,11 +745,13 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e RegexOptions regexOptions = parseRegexOptions(options); + int calculatedLimit = limit; + // if the limit is negative then set Regex to read from right to left - if (limit < 0) + if (calculatedLimit < 0) { regexOptions |= RegexOptions.RightToLeft; - limit *= -1; + calculatedLimit *= -1; } Regex regex = NewRegex(separatorPattern, regexOptions); @@ -758,8 +760,13 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e foreach (string item in content) { +<<<<<<< HEAD string[] split = regex.Split(item, limit, 0); results.AddRange(split); +======= + string[] split = regex.Split(item, calculatedLimit); + ExtendList(results, split); +>>>>>>> dbe4d631f... [feature] added a couple more tests and made code work with the linter } return results.ToArray(); diff --git a/test/powershell/Language/Operators/SplitOperator.Tests.ps1 b/test/powershell/Language/Operators/SplitOperator.Tests.ps1 index 581b8a07da7..efe072376db 100644 --- a/test/powershell/Language/Operators/SplitOperator.Tests.ps1 +++ b/test/powershell/Language/Operators/SplitOperator.Tests.ps1 @@ -43,7 +43,7 @@ Describe "Split Operator" -Tags CI { $res[0] | Should -Be "a b c d" } - It "Binary split operator can work with greater than max substring" { + It "Binary split operator can work with different delimeter than split string" { $res = "a b c d" -split " ",8 $res.count | Should -Be 4 $res[0] | Should -Be "a" @@ -57,6 +57,23 @@ Describe "Split Operator" -Tags CI { $res[1] | Should -Be "b" $res[2] | Should -Be "c" $res[3] | Should -Be "d" + + $res = " " -split " ",-2 + $res.count | Should -Be 2 + $res[0] | Should -Be "" + $res[1] | Should -Be "" + } + + It "Binary split operator can work with regex expression" { + $res = "a2b3c4d" -split '\d+',2 + $res.count | Should -Be 2 + $res[0] | Should -Be "a" + $res[1] | Should -Be "b3c4d" + + $res = "a2b3c4d" -split '\d+',-2 + $res.count | Should -Be 2 + $res[0] | Should -Be "a2b3c" + $res[1] | Should -Be "d" } It "Binary split operator can works with freeform delimiter" { From 701b22f291eaea704c9ab86c86c7f7c43dacaf21 Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sat, 23 Feb 2019 23:06:21 -0600 Subject: [PATCH 06/23] [feature] Added negative argument compatiablity with predacate -split --- .../engine/lang/parserutils.cs | 139 +++++++++++++----- .../Operators/SplitOperator.Tests.ps1 | 25 ++++ 2 files changed, 128 insertions(+), 36 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 510406d2458..20cc972d585 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -644,14 +644,20 @@ private static object SplitOperatorImpl(ExecutionContext context, IScriptExtent } } + private static string ReverseStringUtility(string str) { + var revStr = str.ToCharArray(); + Array.Reverse(revStr); + return new string(revStr); + } + private static object SplitWithPredicate(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, ScriptBlock predicate, int limit) { - List results = new List(); + var results = new List(); foreach (string item in content) { - List split = new List(); + var split = new List(); - if (limit == 1) + if (limit == 1 || limit == -1) { // Don't bother with looking for any delimiters, // just return the original string. @@ -659,53 +665,114 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent continue; } - StringBuilder buf = new StringBuilder(); - for (int strIndex = 0; strIndex < item.Length; strIndex++) + var buf = new StringBuilder(); + + if (limit >= 0) { - object isDelimChar = predicate.DoInvokeReturnAsIs( - useLocalScope: true, - errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, - dollarUnder: CharToString(item[strIndex]), - input: AutomationNull.Value, - scriptThis: AutomationNull.Value, - args: new object[] { item, strIndex }); - if (LanguagePrimitives.IsTrue(isDelimChar)) + for (int strIndex = 0; strIndex < item.Length; strIndex++) { - split.Add(buf.ToString()); - buf = new StringBuilder(); - - if (limit > 0 && split.Count >= (limit - 1)) + object predicateResult = predicate.DoInvokeReturnAsIs( + useLocalScope: true, + errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, + dollarUnder: CharToString(item[strIndex]), + input: AutomationNull.Value, + scriptThis: AutomationNull.Value, + args: new object[] { item, strIndex }); + if (LanguagePrimitives.IsTrue(predicateResult)) { - // We're one item below the limit. If - // we have any string left, go ahead - // and add it as the last item, otherwise - // add an empty string if there was - // a delimiter at the end. - if ((strIndex + 1) < item.Length) + if (buf.Length != 0) { + split.Add(buf.ToString()); + } + + buf.Clear(); + + if (limit > 0 && split.Count >= (limit - 1)) { - split.Add(item.Substring(strIndex + 1)); + // We're one item below the limit. If + // we have any string left, go ahead + // and add it as the last item, otherwise + // add an empty string if there was + // a delimiter at the end. + if ((strIndex + 1) < item.Length) + { + split.Add(item.Substring(strIndex + 1)); + } + else + { + split.Add(string.Empty); + } + + break; } - else + + // If this delimiter is at the end of the string, + // add an empty string to denote the item "after" + // it. + if (strIndex == (item.Length - 1)) { split.Add(string.Empty); } - - break; } - - // If this delimiter is at the end of the string, - // add an empty string to denote the item "after" - // it. - if (strIndex == (item.Length - 1)) + else { - split.Add(string.Empty); + buf.Append(item[strIndex]); } } - else + } + else + { + for (int strIndex = item.Length - 1; strIndex >= 0; strIndex--) { - buf.Append(item[strIndex]); + object predicateResult = predicate.DoInvokeReturnAsIs( + useLocalScope: true, + errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, + dollarUnder: CharToString(item[strIndex]), + input: AutomationNull.Value, + scriptThis: AutomationNull.Value, + args: new object[] { item, strIndex }); + if (LanguagePrimitives.IsTrue(predicateResult)) + { + if (buf.Length != 0) + { + split.Add(ReverseStringUtility(buf.ToString())); + } + + buf.Clear(); + + if ((limit * -1) > 0 && split.Count >= ((limit * -1) - 1)) + { + // We're one item below the limit. If + // we have any string left, go ahead + // and add it as the last item, otherwise + // add an empty string if there was + // a delimiter at the end. + if ((strIndex + 1) < item.Length) + { + split.Add(item.Substring(0, strIndex)); + } + else + { + split.Add(string.Empty); + } + + break; + } + + // If this delimiter is at the end of the string, + // add an empty string to denote the item "after" + // it. + if (strIndex == (item.Length - 1)) + { + split.Add(string.Empty); + } + } + else + { + buf.Append(item[strIndex]); + } } } + // Add any remainder, if we're under the limit. if (buf.Length > 0 && @@ -756,7 +823,7 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e Regex regex = NewRegex(separatorPattern, regexOptions); - List results = new List(); + var results = new List(); foreach (string item in content) { diff --git a/test/powershell/Language/Operators/SplitOperator.Tests.ps1 b/test/powershell/Language/Operators/SplitOperator.Tests.ps1 index efe072376db..60dbb2f749c 100644 --- a/test/powershell/Language/Operators/SplitOperator.Tests.ps1 +++ b/test/powershell/Language/Operators/SplitOperator.Tests.ps1 @@ -64,6 +64,31 @@ Describe "Split Operator" -Tags CI { $res[1] | Should -Be "" } + It "Binary split operator with predicate can work with negative numbers" { + $res = "a b c d" -split {$_ -like ' '},-2 + $res.count | Should -Be 2 + $res[0] | Should -Be "d" + $res[1] | Should -Be "a b c" + + $res = "a b c d" -split {$_ -like ' '},-4 + $res.count | Should -Be 4 + $res[0] | Should -Be "d" + $res[1] | Should -Be "c" + $res[2] | Should -Be "b" + $res[3] | Should -Be "a" + + $res = "a b c d" -split {$_ -like ' '},-8 + $res.count | Should -Be 4 + $res[0] | Should -Be "d" + $res[1] | Should -Be "c" + $res[2] | Should -Be "b" + $res[3] | Should -Be "a" + + $res = " " -split {$_ -like ' '},-4 + $res.count | Should -Be 1 + $res[0] | Should -Be "" + } + It "Binary split operator can work with regex expression" { $res = "a2b3c4d" -split '\d+',2 $res.count | Should -Be 2 From def8036e8cc11f3ca1aa589fe0b07161580bd6ed Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sat, 23 Feb 2019 23:11:20 -0600 Subject: [PATCH 07/23] [feature] fixed some style issues --- .../engine/lang/parserutils.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 20cc972d585..150d3ee63f0 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -644,7 +644,8 @@ private static object SplitOperatorImpl(ExecutionContext context, IScriptExtent } } - private static string ReverseStringUtility(string str) { + private static string ReverseStringUtility(string str) + { var revStr = str.ToCharArray(); Array.Reverse(revStr); return new string(revStr); @@ -680,7 +681,8 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent args: new object[] { item, strIndex }); if (LanguagePrimitives.IsTrue(predicateResult)) { - if (buf.Length != 0) { + if (buf.Length != 0) + { split.Add(buf.ToString()); } @@ -772,7 +774,6 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent } } } - // Add any remainder, if we're under the limit. if (buf.Length > 0 && From 73b28fdbf047dfba3f996dbdab735c5498cce863 Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Mon, 25 Feb 2019 15:12:45 -0600 Subject: [PATCH 08/23] fixing merge conflicts 3 --- .../engine/lang/parserutils.cs | 9 +++++++ .../Operators/SplitOperator.Tests.ps1 | 25 +++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 150d3ee63f0..799828fc6de 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -781,9 +781,18 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent { split.Add(buf.ToString()); } +<<<<<<< HEAD results.AddRange(split); +======= + if (limit < 0) + { + split.Reverse(); + } + ExtendList(results, split); +>>>>>>> e17cee10f... [feature] changed the predicate case to return strings in correct order } + return results.ToArray(); } diff --git a/test/powershell/Language/Operators/SplitOperator.Tests.ps1 b/test/powershell/Language/Operators/SplitOperator.Tests.ps1 index 60dbb2f749c..f0bfeedac6c 100644 --- a/test/powershell/Language/Operators/SplitOperator.Tests.ps1 +++ b/test/powershell/Language/Operators/SplitOperator.Tests.ps1 @@ -67,26 +67,31 @@ Describe "Split Operator" -Tags CI { It "Binary split operator with predicate can work with negative numbers" { $res = "a b c d" -split {$_ -like ' '},-2 $res.count | Should -Be 2 - $res[0] | Should -Be "d" - $res[1] | Should -Be "a b c" + $res[0] | Should -Be "a b c" + $res[1] | Should -Be "d" $res = "a b c d" -split {$_ -like ' '},-4 $res.count | Should -Be 4 - $res[0] | Should -Be "d" - $res[1] | Should -Be "c" - $res[2] | Should -Be "b" - $res[3] | Should -Be "a" + $res[0] | Should -Be "a" + $res[1] | Should -Be "b" + $res[2] | Should -Be "c" + $res[3] | Should -Be "d" $res = "a b c d" -split {$_ -like ' '},-8 $res.count | Should -Be 4 - $res[0] | Should -Be "d" - $res[1] | Should -Be "c" - $res[2] | Should -Be "b" - $res[3] | Should -Be "a" + $res[0] | Should -Be "a" + $res[1] | Should -Be "b" + $res[2] | Should -Be "c" + $res[3] | Should -Be "d" $res = " " -split {$_ -like ' '},-4 $res.count | Should -Be 1 $res[0] | Should -Be "" + + $res = "folder/path/to/file" -split {$_ -like '/'}, -2 + $res.count | Should -Be 2 + $res[0] | Should -Be "folder/path/to" + $res[1] | Should -Be "file" } It "Binary split operator can work with regex expression" { From 02c04d97ec4a8b2a670c189b807b35c5c86e9e1a Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sun, 3 Mar 2019 17:23:04 -0600 Subject: [PATCH 09/23] fixing merge conflicts 4 --- .../engine/lang/parserutils.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 799828fc6de..401c304349c 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -781,16 +781,8 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent { split.Add(buf.ToString()); } -<<<<<<< HEAD results.AddRange(split); -======= - if (limit < 0) - { - split.Reverse(); - } - ExtendList(results, split); ->>>>>>> e17cee10f... [feature] changed the predicate case to return strings in correct order } @@ -837,13 +829,8 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e foreach (string item in content) { -<<<<<<< HEAD string[] split = regex.Split(item, limit, 0); results.AddRange(split); -======= - string[] split = regex.Split(item, calculatedLimit); - ExtendList(results, split); ->>>>>>> dbe4d631f... [feature] added a couple more tests and made code work with the linter } return results.ToArray(); From ae6119c2763f221d00b1d76621ce641d2ffe957f Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Mon, 25 Feb 2019 15:44:13 -0600 Subject: [PATCH 10/23] fixing merge conflicts 5 --- .../engine/lang/parserutils.cs | 175 +++++++----------- 1 file changed, 64 insertions(+), 111 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 401c304349c..9c4aa1a3d77 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -654,138 +654,91 @@ private static string ReverseStringUtility(string str) private static object SplitWithPredicate(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, ScriptBlock predicate, int limit) { var results = new List(); + + if (limit == 1 || limit == -1) + { + // if the user just wants 1 string + // then just return the content + return new List(content); + } + foreach (string item in content) { var split = new List(); - if (limit == 1 || limit == -1) - { - // Don't bother with looking for any delimiters, - // just return the original string. - results.Add(item); - continue; - } - var buf = new StringBuilder(); - if (limit >= 0) + for (int strIndex = 0; strIndex < item.Length; strIndex++) { - for (int strIndex = 0; strIndex < item.Length; strIndex++) - { - object predicateResult = predicate.DoInvokeReturnAsIs( - useLocalScope: true, - errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, - dollarUnder: CharToString(item[strIndex]), - input: AutomationNull.Value, - scriptThis: AutomationNull.Value, - args: new object[] { item, strIndex }); - if (LanguagePrimitives.IsTrue(predicateResult)) - { - if (buf.Length != 0) - { - split.Add(buf.ToString()); - } - - buf.Clear(); + int cursor = strIndex; - if (limit > 0 && split.Count >= (limit - 1)) - { - // We're one item below the limit. If - // we have any string left, go ahead - // and add it as the last item, otherwise - // add an empty string if there was - // a delimiter at the end. - if ((strIndex + 1) < item.Length) - { - split.Add(item.Substring(strIndex + 1)); - } - else - { - split.Add(string.Empty); - } + if (limit < 0) + { + // switch the cursor to start at the back of the string + cursor = item.Length - 1 - strIndex; + } - break; - } + // evaluate the predicate + object predicateResult = predicate.DoInvokeReturnAsIs( + useLocalScope: true, + errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, + dollarUnder: CharToString(item[strIndex]), + input: AutomationNull.Value, + scriptThis: AutomationNull.Value, + args: new object[] { item, strIndex }); - // If this delimiter is at the end of the string, - // add an empty string to denote the item "after" - // it. - if (strIndex == (item.Length - 1)) - { - split.Add(string.Empty); - } - } - else - { - buf.Append(item[strIndex]); - } + if (!LanguagePrimitives.IsTrue(predicateResult)) + { + // if the current character is not a delimiter + // then add it to the buffer + buf.Append(item[strIndex]); + continue; } - } - else - { - for (int strIndex = item.Length - 1; strIndex >= 0; strIndex--) + + if (buf.Length != 0) { - object predicateResult = predicate.DoInvokeReturnAsIs( - useLocalScope: true, - errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, - dollarUnder: CharToString(item[strIndex]), - input: AutomationNull.Value, - scriptThis: AutomationNull.Value, - args: new object[] { item, strIndex }); - if (LanguagePrimitives.IsTrue(predicateResult)) - { - if (buf.Length != 0) - { - split.Add(ReverseStringUtility(buf.ToString())); - } - - buf.Clear(); - - if ((limit * -1) > 0 && split.Count >= ((limit * -1) - 1)) - { - // We're one item below the limit. If - // we have any string left, go ahead - // and add it as the last item, otherwise - // add an empty string if there was - // a delimiter at the end. - if ((strIndex + 1) < item.Length) - { - split.Add(item.Substring(0, strIndex)); - } - else - { - split.Add(string.Empty); - } + split.Add(buf.ToString()); + } - break; - } + buf.Clear(); - // If this delimiter is at the end of the string, - // add an empty string to denote the item "after" - // it. - if (strIndex == (item.Length - 1)) - { - split.Add(string.Empty); - } + if (limit > 0 && split.Count >= (limit - 1)) + { + if ((strIndex + 1) < item.Length) + { + // We're one item below the limit. + // If we have any string left add it + // else add an empty string + split.Add(item.Substring(strIndex + 1)); } else { - buf.Append(item[strIndex]); + split.Add(string.Empty); } - } - } - // Add any remainder, if we're under the limit. - if (buf.Length > 0 && - (limit <= 0 || split.Count < limit)) - { - split.Add(buf.ToString()); + break; + } + + if (strIndex == (item.Length - 1)) + { + // If this delimiter is at the end of the string, + // add an empty string to denote the item "after" + // it. + split.Add(string.Empty); + } + // Add any remainder, if we're under the limit. + if (buf.Length > 0 && + (limit <= 0 || split.Count < limit)) + { + split.Add(buf.ToString()); + } + if (limit < 0) + { + split.Reverse(); + } + results.AddRange(split); } - - results.AddRange(split); } - - return results.ToArray(); } From a52711c86aa308975123376eacd5d30a1d5d7abd Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Mon, 25 Feb 2019 15:46:46 -0600 Subject: [PATCH 11/23] fixing merge conflicts 7 --- src/System.Management.Automation/engine/lang/parserutils.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 9c4aa1a3d77..8c1225d01df 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -726,12 +726,14 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent // it. split.Add(string.Empty); } + // Add any remainder, if we're under the limit. if (buf.Length > 0 && (limit <= 0 || split.Count < limit)) { split.Add(buf.ToString()); } + if (limit < 0) { split.Reverse(); @@ -739,6 +741,7 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent results.AddRange(split); } } + return results.ToArray(); } From 3b5ccbd94fe6b1db5efe7a927a23eee0142d4a25 Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Mon, 25 Feb 2019 17:36:01 -0600 Subject: [PATCH 12/23] fixing merge conflicts 8 --- .../engine/lang/parserutils.cs | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 8c1225d01df..a9868f72d07 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -682,44 +682,44 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent object predicateResult = predicate.DoInvokeReturnAsIs( useLocalScope: true, errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, - dollarUnder: CharToString(item[strIndex]), + dollarUnder: CharToString(item[cursor]), input: AutomationNull.Value, scriptThis: AutomationNull.Value, - args: new object[] { item, strIndex }); + args: new object[] { item, cursor }); if (!LanguagePrimitives.IsTrue(predicateResult)) { // if the current character is not a delimiter // then add it to the buffer - buf.Append(item[strIndex]); + buf.Append(item[cursor]); continue; } if (buf.Length != 0) { - split.Add(buf.ToString()); + split.Add((limit < 0)? ReverseStringUtility(buf.ToString()): buf.ToString()); } buf.Clear(); - if (limit > 0 && split.Count >= (limit - 1)) + if (System.Math.Abs(limit) > 0 && split.Count >= (System.Math.Abs(limit) - 1)) { - if ((strIndex + 1) < item.Length) + if ((cursor + 1) < item.Length && limit > 0) { // We're one item below the limit. // If we have any string left add it // else add an empty string - split.Add(item.Substring(strIndex + 1)); + split.Add(item.Substring(cursor + 1)); } - else + else if((cursor + 1) < item.Length && limit < 0) { - split.Add(string.Empty); + split.Add(item.Substring(0, cursor)); } break; } - if (strIndex == (item.Length - 1)) + if (cursor == (item.Length - 1) || cursor == 0) { // If this delimiter is at the end of the string, // add an empty string to denote the item "after" @@ -728,18 +728,23 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent } // Add any remainder, if we're under the limit. - if (buf.Length > 0 && - (limit <= 0 || split.Count < limit)) + if (buf.Length > 0 && split.Count <= System.Math.Abs(limit)) { split.Add(buf.ToString()); } + } - if (limit < 0) - { - split.Reverse(); - } - results.AddRange(split); + if (buf.Length > 0) + { + split.Add((limit < 0)? ReverseStringUtility(buf.ToString()): buf.ToString()); } + + if (limit < 0) + { + split.Reverse(); + } + + ExtendList(results, split); } return results.ToArray(); From c11938a34d2bea6bf7d86eb34f16555dbd81a171 Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Mon, 25 Feb 2019 17:38:52 -0600 Subject: [PATCH 13/23] [feature] style fixes --- src/System.Management.Automation/engine/lang/parserutils.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index a9868f72d07..3733f884cff 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -697,7 +697,7 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent if (buf.Length != 0) { - split.Add((limit < 0)? ReverseStringUtility(buf.ToString()): buf.ToString()); + split.Add((limit < 0) ? ReverseStringUtility(buf.ToString()) : buf.ToString()); } buf.Clear(); @@ -711,7 +711,7 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent // else add an empty string split.Add(item.Substring(cursor + 1)); } - else if((cursor + 1) < item.Length && limit < 0) + else if ((cursor + 1) < item.Length && limit < 0) { split.Add(item.Substring(0, cursor)); } @@ -736,7 +736,7 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent if (buf.Length > 0) { - split.Add((limit < 0)? ReverseStringUtility(buf.ToString()): buf.ToString()); + split.Add((limit < 0) ? ReverseStringUtility(buf.ToString()) : buf.ToString()); } if (limit < 0) From 8b7c843053ca8e0dbff7ac7cad42c36bcd1c2cd6 Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sun, 3 Mar 2019 18:10:57 -0600 Subject: [PATCH 14/23] Hopefully fixed all merge conflicts --- src/System.Management.Automation/engine/lang/parserutils.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 3733f884cff..4fbfb44d7cd 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -744,7 +744,7 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent split.Reverse(); } - ExtendList(results, split); + results.AddRange(split); } return results.ToArray(); @@ -790,7 +790,7 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e foreach (string item in content) { - string[] split = regex.Split(item, limit, 0); + string[] split = regex.Split(item, calculatedLimit); results.AddRange(split); } From c96c58ecb0f38161a21c6ba378068b7bfd38cdf3 Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Fri, 10 May 2019 18:48:15 -0500 Subject: [PATCH 15/23] [feature] Added comments and better test cases --- .../engine/lang/parserutils.cs | 111 ++++++++++-------- .../Operators/SplitOperator.Tests.ps1 | 3 +- 2 files changed, 66 insertions(+), 48 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 4fbfb44d7cd..4a0b8e3ee0a 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -666,19 +666,18 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent { var split = new List(); - var buf = new StringBuilder(); + // Find out where to start the splitting process. + // If it is a reverse split then start at the end + // of the item. Else, start at the start of the item. + var cursor = (limit < 0)? item.Length - 1: 0; - for (int strIndex = 0; strIndex < item.Length; strIndex++) - { - int cursor = strIndex; - - if (limit < 0) - { - // switch the cursor to start at the back of the string - cursor = item.Length - 1 - strIndex; - } + // This is used to calculate how much to split from item. + var subStringLength = 0; - // evaluate the predicate + // Loop through the entire item + for (int charCount = 0; charCount < item.Length; charCount++) + { + // Evaluate the predicate using the character at cursor. object predicateResult = predicate.DoInvokeReturnAsIs( useLocalScope: true, errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, @@ -686,57 +685,75 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent input: AutomationNull.Value, scriptThis: AutomationNull.Value, args: new object[] { item, cursor }); - - if (!LanguagePrimitives.IsTrue(predicateResult)) - { - // if the current character is not a delimiter - // then add it to the buffer - buf.Append(item[cursor]); - continue; - } - if (buf.Length != 0) - { - split.Add((limit < 0) ? ReverseStringUtility(buf.ToString()) : buf.ToString()); - } - - buf.Clear(); - - if (System.Math.Abs(limit) > 0 && split.Count >= (System.Math.Abs(limit) - 1)) + // If the current character is not a delimiter + // then it must be included into a substring. + if (!LanguagePrimitives.IsTrue(predicateResult)) { - if ((cursor + 1) < item.Length && limit > 0) + subStringLength++; + if (limit < 0) { - // We're one item below the limit. - // If we have any string left add it - // else add an empty string - split.Add(item.Substring(cursor + 1)); + cursor -= 1; } - else if ((cursor + 1) < item.Length && limit < 0) + else { - split.Add(item.Substring(0, cursor)); + cursor += 1; } + continue; + } - break; + // Else, if the character is a delimiter + // then add a substring to the split list. + split.Add(item.Substring((limit < 0)? cursor + 1: cursor - subStringLength, subStringLength)); + + subStringLength = 0; + + if (limit < 0) + { + cursor -= 1; } - - if (cursor == (item.Length - 1) || cursor == 0) + else { - // If this delimiter is at the end of the string, - // add an empty string to denote the item "after" - // it. - split.Add(string.Empty); + cursor += 1; } - - // Add any remainder, if we're under the limit. - if (buf.Length > 0 && split.Count <= System.Math.Abs(limit)) + + if (System.Math.Abs(limit) == (split.Count + 1)) { - split.Add(buf.ToString()); + break; } } - if (buf.Length > 0) + if (cursor == -1) + { + // Used when the limit is negative + // and the cursor was allowed to go + // all the way to the start of the + // string. + split.Add(item.Substring(0, subStringLength)); + } + else if (limit < 0) + { + // Used to get the rest of the string + // when using a negative limit and + // the cursor doesn't reach the end + // of the string. + split.Add(item.Substring(0, cursor + 1)); + } + else if (cursor == item.Length) + { + // Used to get the rest of the string + // when the limit is not negative and + // the cursor is allowed to make it to + // the end of the string. + split.Add(item.Substring(cursor - subStringLength, subStringLength)); + } + else { - split.Add((limit < 0) ? ReverseStringUtility(buf.ToString()) : buf.ToString()); + // Used to get the rest of the string + // when the limit is not negative and + // the cursor is not at the end of the + // string. + split.Add(item.Substring(cursor, item.Length - cursor)); } if (limit < 0) diff --git a/test/powershell/Language/Operators/SplitOperator.Tests.ps1 b/test/powershell/Language/Operators/SplitOperator.Tests.ps1 index f0bfeedac6c..68550ea3c48 100644 --- a/test/powershell/Language/Operators/SplitOperator.Tests.ps1 +++ b/test/powershell/Language/Operators/SplitOperator.Tests.ps1 @@ -85,8 +85,9 @@ Describe "Split Operator" -Tags CI { $res[3] | Should -Be "d" $res = " " -split {$_ -like ' '},-4 - $res.count | Should -Be 1 + $res.count | Should -Be 2 $res[0] | Should -Be "" + $res[1] | Should -Be "" $res = "folder/path/to/file" -split {$_ -like '/'}, -2 $res.count | Should -Be 2 From 21a7c66e3a2b8022bc632f0915f02ac7ce8ddbee Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sat, 11 May 2019 11:36:38 -0500 Subject: [PATCH 16/23] [feature] Got rid of ReverseStringUtility function --- .../engine/lang/parserutils.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 4a0b8e3ee0a..bd4bfd9c742 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -644,13 +644,6 @@ private static object SplitOperatorImpl(ExecutionContext context, IScriptExtent } } - private static string ReverseStringUtility(string str) - { - var revStr = str.ToCharArray(); - Array.Reverse(revStr); - return new string(revStr); - } - private static object SplitWithPredicate(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, ScriptBlock predicate, int limit) { var results = new List(); From e55b277ed476af30112f7d7d9172a21dab437c65 Mon Sep 17 00:00:00 2001 From: Jacob Scott Date: Fri, 31 May 2019 16:34:41 -0500 Subject: [PATCH 17/23] [reature] negative predicate is now a seperate function --- .../engine/lang/parserutils.cs | 136 ++++++++++++------ 1 file changed, 89 insertions(+), 47 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index bd4bfd9c742..252a700cf13 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -634,23 +634,27 @@ private static object SplitOperatorImpl(ExecutionContext context, IScriptExtent options |= SplitOptions.IgnoreCase; } - if (predicate != null) + if (predicate == null) + { + return SplitWithPattern(context, errorPosition, content, separatorPattern, limit, options); + } + else if (limit >= 0) { return SplitWithPredicate(context, errorPosition, content, predicate, limit); } else { - return SplitWithPattern(context, errorPosition, content, separatorPattern, limit, options); + return NegativeSplitWithPredicate(context, errorPosition, content, predicate, limit); } } - private static object SplitWithPredicate(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, ScriptBlock predicate, int limit) + private static object NegativeSplitWithPredicate(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, ScriptBlock predicate, int limit) { var results = new List(); - if (limit == 1 || limit == -1) + if (limit == -1) { - // if the user just wants 1 string + // if the user just wants 1 string // then just return the content return new List(content); } @@ -659,17 +663,12 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent { var split = new List(); - // Find out where to start the splitting process. - // If it is a reverse split then start at the end - // of the item. Else, start at the start of the item. - var cursor = (limit < 0)? item.Length - 1: 0; + // Used to traverse through the item + var cursor = item.Length - 1; - // This is used to calculate how much to split from item. var subStringLength = 0; - // Loop through the entire item - for (int charCount = 0; charCount < item.Length; charCount++) - { + for (var charCount = 0; charCount < item.Length; charCount++) { // Evaluate the predicate using the character at cursor. object predicateResult = predicate.DoInvokeReturnAsIs( useLocalScope: true, @@ -678,37 +677,19 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent input: AutomationNull.Value, scriptThis: AutomationNull.Value, args: new object[] { item, cursor }); - - // If the current character is not a delimiter - // then it must be included into a substring. + if (!LanguagePrimitives.IsTrue(predicateResult)) { subStringLength++; - if (limit < 0) - { - cursor -= 1; - } - else - { - cursor += 1; - } + cursor -= 1; continue; } - // Else, if the character is a delimiter - // then add a substring to the split list. - split.Add(item.Substring((limit < 0)? cursor + 1: cursor - subStringLength, subStringLength)); + split.Add(item.Substring(cursor + 1, subStringLength)); subStringLength = 0; - if (limit < 0) - { - cursor -= 1; - } - else - { - cursor += 1; - } + cursor -= 1; if (System.Math.Abs(limit) == (split.Count + 1)) { @@ -724,36 +705,97 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent // string. split.Add(item.Substring(0, subStringLength)); } - else if (limit < 0) + else { // Used to get the rest of the string - // when using a negative limit and - // the cursor doesn't reach the end + // when using a negative limit and + // the cursor doesn't reach the end // of the string. split.Add(item.Substring(0, cursor + 1)); } - else if (cursor == item.Length) + + split.Reverse(); + + results.AddRange(split); + } + + return results.ToArray(); + } + + private static object SplitWithPredicate(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, ScriptBlock predicate, int limit) + { + var results = new List(); + + if (limit == 1) + { + // if the user just wants 1 string + // then just return the content + return new List(content); + } + + foreach (string item in content) + { + var split = new List(); + + // Used to traverse through the item + var cursor = 0; + + // This is used to calculate how much to split from item. + var subStringLength = 0; + + for (var charCount = 0; charCount < item.Length; charCount++) + { + // Evaluate the predicate using the character at cursor. + object predicateResult = predicate.DoInvokeReturnAsIs( + useLocalScope: true, + errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, + dollarUnder: CharToString(item[cursor]), + input: AutomationNull.Value, + scriptThis: AutomationNull.Value, + args: new object[] { item, cursor }); + + // If the current character is not a delimiter + // then it must be included into a substring. + if (!LanguagePrimitives.IsTrue(predicateResult)) + { + subStringLength++; + + cursor += 1; + + continue; + } + + // Else, if the character is a delimiter + // then add a substring to the split list. + split.Add(item.Substring(cursor - subStringLength, subStringLength)); + + subStringLength = 0; + + cursor += 1; + + if (limit == (split.Count + 1)) + { + break; + } + } + + if (cursor == item.Length) { // Used to get the rest of the string // when the limit is not negative and - // the cursor is allowed to make it to + // the cursor is allowed to make it to // the end of the string. split.Add(item.Substring(cursor - subStringLength, subStringLength)); } else { - // Used to get the rest of the string + // Used to get the rest of the string // when the limit is not negative and // the cursor is not at the end of the // string. split.Add(item.Substring(cursor, item.Length - cursor)); } - if (limit < 0) - { - split.Reverse(); - } - results.AddRange(split); } @@ -788,7 +830,7 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e int calculatedLimit = limit; // if the limit is negative then set Regex to read from right to left - if (calculatedLimit < 0) + if (calculatedLimit < 0) { regexOptions |= RegexOptions.RightToLeft; calculatedLimit *= -1; From 27c32033fd6bf3374eb728899d96c952edd57c1b Mon Sep 17 00:00:00 2001 From: Jacob Scott <36082020+ece-jacob-scott@users.noreply.github.com> Date: Sat, 1 Jun 2019 11:10:02 -0500 Subject: [PATCH 18/23] switch var to int Co-Authored-By: Robert Holt --- src/System.Management.Automation/engine/lang/parserutils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 252a700cf13..e464fd4a715 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -668,7 +668,7 @@ private static object NegativeSplitWithPredicate(ExecutionContext context, IScri var subStringLength = 0; - for (var charCount = 0; charCount < item.Length; charCount++) { + for (int charCount = 0; charCount < item.Length; charCount++) { // Evaluate the predicate using the character at cursor. object predicateResult = predicate.DoInvokeReturnAsIs( useLocalScope: true, From 261bd5639ad762641bf4d987493ac614d984a0c8 Mon Sep 17 00:00:00 2001 From: Jacob Scott <36082020+ece-jacob-scott@users.noreply.github.com> Date: Sat, 1 Jun 2019 11:10:21 -0500 Subject: [PATCH 19/23] switch var to int Co-Authored-By: Robert Holt --- src/System.Management.Automation/engine/lang/parserutils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index e464fd4a715..26d30bc1710 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -666,7 +666,7 @@ private static object NegativeSplitWithPredicate(ExecutionContext context, IScri // Used to traverse through the item var cursor = item.Length - 1; - var subStringLength = 0; + int subStringLength = 0; for (int charCount = 0; charCount < item.Length; charCount++) { // Evaluate the predicate using the character at cursor. From c06eed95c20a69e78a1e9ea267ff044686e4374b Mon Sep 17 00:00:00 2001 From: Jacob Scott <36082020+ece-jacob-scott@users.noreply.github.com> Date: Sat, 1 Jun 2019 11:10:40 -0500 Subject: [PATCH 20/23] switch var to int Co-Authored-By: Robert Holt --- src/System.Management.Automation/engine/lang/parserutils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 26d30bc1710..1d3c7e83cc5 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -664,7 +664,7 @@ private static object NegativeSplitWithPredicate(ExecutionContext context, IScri var split = new List(); // Used to traverse through the item - var cursor = item.Length - 1; + int cursor = item.Length - 1; int subStringLength = 0; From 70893951eead8a319fb3a194a1cb7ced521a8507 Mon Sep 17 00:00:00 2001 From: Jacob Scott <36082020+ece-jacob-scott@users.noreply.github.com> Date: Sat, 1 Jun 2019 11:11:11 -0500 Subject: [PATCH 21/23] capitalize comment Co-Authored-By: Robert Holt --- src/System.Management.Automation/engine/lang/parserutils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 1d3c7e83cc5..205debfeb60 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -829,7 +829,7 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e int calculatedLimit = limit; - // if the limit is negative then set Regex to read from right to left + // If the limit is negative then set Regex to read from right to left if (calculatedLimit < 0) { regexOptions |= RegexOptions.RightToLeft; From d71453ff3304070add32c230435b39b8dfa13940 Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sat, 1 Jun 2019 11:19:33 -0500 Subject: [PATCH 22/23] [feature] Updated the return types of split functions to IReadOnlyList --- .../engine/lang/parserutils.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 205debfeb60..5ad3c805f9a 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -584,7 +584,7 @@ internal static object SplitOperator(ExecutionContext context, IScriptExtent err return SplitOperatorImpl(context, errorPosition, lval, rval, SplitImplOptions.None, ignoreCase); } - private static object SplitOperatorImpl(ExecutionContext context, IScriptExtent errorPosition, object lval, object rval, SplitImplOptions implOptions, bool ignoreCase) + private static IReadOnlyList SplitOperatorImpl(ExecutionContext context, IScriptExtent errorPosition, object lval, object rval, SplitImplOptions implOptions, bool ignoreCase) { IEnumerable content = enumerateContent(context, errorPosition, implOptions, lval); @@ -648,13 +648,13 @@ private static object SplitOperatorImpl(ExecutionContext context, IScriptExtent } } - private static object NegativeSplitWithPredicate(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, ScriptBlock predicate, int limit) + private static IReadOnlyList NegativeSplitWithPredicate(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, ScriptBlock predicate, int limit) { var results = new List(); if (limit == -1) { - // if the user just wants 1 string + // If the user just wants 1 string // then just return the content return new List(content); } @@ -722,13 +722,13 @@ private static object NegativeSplitWithPredicate(ExecutionContext context, IScri return results.ToArray(); } - private static object SplitWithPredicate(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, ScriptBlock predicate, int limit) + private static IReadOnlyList SplitWithPredicate(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, ScriptBlock predicate, int limit) { var results = new List(); if (limit == 1) { - // if the user just wants 1 string + // If the user just wants 1 string // then just return the content return new List(content); } @@ -802,7 +802,7 @@ private static object SplitWithPredicate(ExecutionContext context, IScriptExtent return results.ToArray(); } - private static object SplitWithPattern(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, string separatorPattern, int limit, SplitOptions options) + private static IReadOnlyList SplitWithPattern(ExecutionContext context, IScriptExtent errorPosition, IEnumerable content, string separatorPattern, int limit, SplitOptions options) { // Default to Regex matching if no match specified. if ((options & SplitOptions.SimpleMatch) == 0 && From c951ab68f04685e38879a658328f36073f8eddc6 Mon Sep 17 00:00:00 2001 From: ece-jacob-scott Date: Sun, 2 Jun 2019 13:02:20 -0500 Subject: [PATCH 23/23] [feature] changed var to int where needed --- src/System.Management.Automation/engine/lang/parserutils.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 5ad3c805f9a..2cf6f2b3831 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -738,12 +738,12 @@ private static IReadOnlyList SplitWithPredicate(ExecutionContext context var split = new List(); // Used to traverse through the item - var cursor = 0; + int cursor = 0; // This is used to calculate how much to split from item. - var subStringLength = 0; + int subStringLength = 0; - for (var charCount = 0; charCount < item.Length; charCount++) + for (int charCount = 0; charCount < item.Length; charCount++) { // Evaluate the predicate using the character at cursor. object predicateResult = predicate.DoInvokeReturnAsIs(