8000 Implementing MatchInfo with Range in single list. · powercode/PowerShell@1d339d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d339d1

Browse files
committed
Implementing MatchInfo with Range in single list.
1 parent b2ccb8f commit 1d339d1

File tree

1 file changed

+43
-35
lines changed
  • src/Microsoft.PowerShell.Commands.Utility/commands/utility

1 file changed

+43
-35
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections;
66
using System.Collections.Generic;
77
using System.Collections.ObjectModel;
8+
using System.Diagnostics.Eventing.Reader;
89
using System.Globalization;
910
using System.IO;
1011
using System.Management.Automation;
@@ -62,7 +63,7 @@ public object Clone()
6263
};
6364
}
6465
}
65-
66+
6667
/// <summary>
6768
/// The object returned by select-string representing the result of a match.
6869
/// </summary>
@@ -95,15 +96,15 @@ public class MatchInfo
9596
private readonly bool _emphasize;
9697

9798
/// <summary>
98-
/// Stores the starting index of each match within the line.
99+
/// Stores the matchIndex if there are only a single match
99100
/// </summary>
100-
private readonly IReadOnlyList<int> _matchIndexes;
101-
101+
private readonly Range _matchingRange;
102+
102103
/// <summary>
103-
/// Stores the length of each match within the line.
104+
/// Stores a tuple of the starting index and length of each match within the line.
104105
/// </summary>
105-
private readonly IReadOnlyList<int> _matchLengths;
106-
106+
private readonly IReadOnlyList<Range> _matchingRanges;
107+
107108
/// <summary>
108109
/// Initializes a new instance of the <see cref="MatchInfo"/> class with emphasis disabled.
109110
/// </summary>
@@ -116,13 +117,20 @@ public MatchInfo()
116117
/// Initializes a new instance of the <see cref="MatchInfo"/> class with emphasized matched text.
117118
/// Used when virtual terminal sequences are supported.
118119
/// </summary>
119-
/// <param name="matchIndexes">Sets the matchIndexes.</param>
120-
/// <param name="matchLengths">Sets the matchLengths.</param>
121-
public MatchInfo(IReadOnlyList<int> matchIndexes, IReadOnlyList<int> matchLengths)
120+
/// <param name="matchRanges">Sets the matchIndexes.</param>
121+
internal MatchInfo(IReadOnlyList<Range> matchRanges)
122122
{
123123
this._emphasize = true;
124-
this._matchIndexes = matchIndexes;
125-
this._matchLengths = matchLengths;
124+
if (matchRanges.Count == 1)
125+
{
126+
_matchingRange = matchRanges[0];
127+
this._matchingRanges = null;
128+
}
129+
else
130+
{
131+
this._matchingRanges = matchRanges;
132+
}
133+
126134
}
127135

128136
/// <summary>
@@ -314,38 +322,43 @@ public string ToEmphasizedString(string directory)
314322
private string EmphasizeLine()
315323
{
316324
var psStyle = PSStyle.Instance;
317-
int emphasizedLineLength = _matchIndexes.Count * (psStyle.Reverse.Length + psStyle.Reset.Length) + Line.Length;
318-
var result = string.Create(emphasizedLineLength, (Line, _matchIndexes, _matchLengths, psStyle.Reverse, psStyle.Reset), static (chars, state) =>
325+
int emphasizedLineLength = ((_matchingRanges ?.Count ?? 1) * (psStyle.Reverse.Length + psStyle.Reset.Length)) + Line.Length;
326+
IReadOnlyList<Range> matchRanges = _matchingRanges ?? [_matchingRange];
327+
var result = string.Create(emphasizedLineLength, (Line, matchIndexes: matchRanges, psStyle.Reverse, psStyle.Reset), static (chars, state) =>
319328
{
320-
(string sourceLine, IReadOnlyList<int> matchIndexes, IReadOnlyList<int> matchLengths, string invertColorsVT100, string resetVT100) = state;
329+
(string sourceLine, IReadOnlyList<Range> matchRanges, string invertColorsVT100, string resetVT100) = state;
321330
ReadOnlySpan<char> sourceSpan = sourceLine.AsSpan();
322331
var dest = 6D40 chars;
323332
int lineIndex = 0;
324-
for (int i = 0; i < matchIndexes.Count; i++)
333+
334+
for (int i = 0; i < matchRanges.Count; i++)
325335
{
326-
var line = sourceSpan[lineIndex..matchIndexes[i]];
336+
Range matchRange = matchRanges[i];
337+
var line = sourceSpan[lineIndex..matchRange.Start];
327338
// Adds characters before match
328339
line.CopyTo(dest);
329340
dest = dest[line.Length..];
330-
lineIndex = matchIndexes[i];
341+
lineIndex = matchRange.Start.Value;
331342
line = sourceSpan[lineIndex..];
332343

333344
// Adds opening vt sequence
334-
invertColorsVT100.AsSpan().CopyTo(dest);
345+
invertColorsVT100.CopyTo(dest);
335346
dest = dest[invertColorsVT100.Length..];
336-
347+
Index rangeEnd = matchRange.End;
337348
// Adds characters being emphasized
338-
line = line[..matchLengths[i]];
349+
line = line[..rangeEnd];
339350
line.CopyTo(dest);
340-
dest = dest[matchLengths[i]..];
341-
lineIndex += matchLengths[i];
351+
dest = dest[rangeEnd..];
352+
lineIndex += rangeEnd.Value;
342353

343354
// Adds closing vt sequence
344355
resetVT100.CopyTo(dest);
345356
dest = dest[resetVT100.Length..];
346357

347358
}
348-
var charsIndex = matchIndexes.Count == 0 ? 0 : matchIndexes[^1] + matchLengths[^1];
359+
360+
Range lastMatch = matchRanges[^1];
361+
var charsIndex = matchRanges.Count == 0 ? 0 : lastMatch.Start.Value + lastMatch.End.Value;
349362
// Adds remaining characters in line
350363
sourceSpan[charsIndex..].CopyTo(dest);
351364
});
@@ -1773,8 +1786,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
17731786
int patternIndex = 0;
17741787
matchResult = null;
17751788

1776-
List<int> indexes = null;
1777-
List<int> lengths = null;
1789+
List<Range> indexes = null;
17781790

17791791
bool shouldEmphasize = !NoEmphasis; // && Host.UI.SupportsVirtualTerminal;
17801792

@@ -1783,8 +1795,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
17831795
// need to be passed in to the matchInfo object.
17841796
if (shouldEmphasize)
17851797
{
1786-
indexes = new List<int>();
1787-
lengths = new List<int>();
1798+
indexes = new List<Range>();
17881799
}
17891800

17901801
if (!SimpleMatch)
@@ -1808,8 +1819,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
18081819
{
18091820
foreach (Match match in matches)
18101821
{
1811-
indexes.Add(match.Index);
1812-
lengths.Add(match.Length);
1822+
indexes.Add(new(match.Index, match.Length));
18131823
}
18141824
}
18151825

@@ -1825,8 +1835,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
18251835
{
18261836
if (shouldEmphasize)
18271837
{
1828-
indexes.Add(match.Index);
1829-
lengths.Add(match.Length);
1838+
indexes.Add(new(match.Index, match.Length));
18301839
}
18311840

18321841
matches = new Match[] { match };
@@ -1852,8 +1861,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
18521861
{
18531862
if (shouldEmphasize)
18541863
{
1855-
indexes.Add(index);
1856-
lengths.Add(pat.Length);
1864+
indexes.Add(new(index, pat.Length));
18571865
}
18581866

18591867
gotMatch = true;
@@ -1905,7 +1913,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
19051913

19061914
// otherwise construct and populate a new MatchInfo object
19071915
matchResult = shouldEmphasize
1908-
? new MatchInfo(indexes, lengths)
1916+
? new MatchInfo(indexes)
19091917
: new MatchInfo();
19101918
matchResult.IgnoreCase = !CaseSensitive;
19111919
matchResult.Line = operandString;

0 commit comments

Comments
 (0)
0