5
5
using System . Collections ;
6
6
using System . Collections . Generic ;
7
7
using System . Collections . ObjectModel ;
8
+ using System . Diagnostics . Eventing . Reader ;
8
9
using System . Globalization ;
9
10
using System . IO ;
10
11
using System . Management . Automation ;
@@ -62,7 +63,7 @@ public object Clone()
62
63
} ;
63
64
}
64
65
}
65
-
66
+
66
67
/// <summary>
67
68
/// The object returned by select-string representing the result of a match.
68
69
/// </summary>
@@ -95,15 +96,15 @@ public class MatchInfo
95
96
private readonly bool _emphasize ;
96
97
97
98
/// <summary>
98
- /// Stores the starting index of each match within the line.
99
+ /// Stores the matchIndex if there are only a single match
99
100
/// </summary>
100
- private readonly IReadOnlyList < int > _matchIndexes ;
101
-
101
+ private readonly Range _matchingRange ;
102
+
102
103
/// <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.
104
105
/// </summary>
105
- private readonly IReadOnlyList < int > _matchLengths ;
106
-
106
+ private readonly IReadOnlyList < Range > _matchingRanges ;
107
+
107
108
/// <summary>
108
109
/// Initializes a new instance of the <see cref="MatchInfo"/> class with emphasis disabled.
109
110
/// </summary>
@@ -116,13 +117,20 @@ public MatchInfo()
116
117
/// Initializes a new instance of the <see cref="MatchInfo"/> class with emphasized matched text.
117
118
/// Used when virtual terminal sequences are supported.
118
119
/// </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 )
122
122
{
123
123
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
+
126
134
}
127
135
128
136
/// <summary>
@@ -314,38 +322,43 @@ public string ToEmphasizedString(string directory)
314
322
private string EmphasizeLine ( )
315
323
{
316
324
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 ) =>
319
328
{
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 ;
321
330
ReadOnlySpan < char > sourceSpan = sourceLine . AsSpan ( ) ;
322
331
var dest =
6D40
chars ;
323
332
int lineIndex = 0 ;
324
- for ( int i = 0 ; i < matchIndexes . Count ; i ++ )
333
+
334
+ for ( int i = 0 ; i < matchRanges . Count ; i ++ )
325
335
{
326
- var line = sourceSpan [ lineIndex ..matchIndexes [ i ] ] ;
336
+ Range matchRange = matchRanges [ i ] ;
337
+ var line = sourceSpan [ lineIndex ..matchRange . Start ] ;
327
338
// Adds characters before match
328
339
line . CopyTo ( dest ) ;
329
340
dest = dest [ line . Length ..] ;
330
- lineIndex = matchIndexes [ i ] ;
341
+ lineIndex = matchRange . Start . Value ;
331
342
line = sourceSpan [ lineIndex ..] ;
332
343
333
344
// Adds opening vt sequence
334
- invertColorsVT100 . AsSpan ( ) . CopyTo ( dest ) ;
345
+ invertColorsVT100 . CopyTo ( dest ) ;
335
346
dest = dest [ invertColorsVT100 . Length ..] ;
336
-
347
+ Index rangeEnd = matchRange . End ;
337
348
// Adds characters being emphasized
338
- line = line [ ..matchLengths [ i ] ] ;
349
+ line = line [ ..rangeEnd ] ;
339
350
line . CopyTo ( dest ) ;
340
- dest = dest [ matchLengths [ i ] ..] ;
341
- lineIndex += matchLengths [ i ] ;
351
+ dest = dest [ rangeEnd ..] ;
352
+ lineIndex += rangeEnd . Value ;
342
353
343
354
// Adds closing vt sequence
344
355
resetVT100 . CopyTo ( dest ) ;
345
356
dest = dest [ resetVT100 . Length ..] ;
346
357
347
358
}
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 ;
349
362
// Adds remaining characters in line
350
363
sourceSpan [ charsIndex ..] . CopyTo ( dest ) ;
351
364
} ) ;
@@ -1773,8 +1786,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
1773
1786
int patternIndex = 0 ;
1774
1787
matchResult = null ;
1775
1788
1776
- List < int > indexes = null ;
1777
- List < int > lengths = null ;
1789
+ List < Range > indexes = null ;
1778
1790
1779
1791
bool shouldEmphasize = ! NoEmphasis ; // && Host.UI.SupportsVirtualTerminal;
1780
1792
@@ -1783,8 +1795,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
1783
1795
// need to be passed in to the matchInfo object.
1784
1796
if ( shouldEmphasize )
1785
1797
{
1786
- indexes = new List < int > ( ) ;
1787
- lengths = new List < int > ( ) ;
1798
+ indexes = new List < Range > ( ) ;
1788
1799
}
1789
1800
1790
1801
if ( ! SimpleMatch )
@@ -1808,8 +1819,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
1808
1819
{
1809
1820
foreach ( Match match in matches )
1810
1821
{
1811
- indexes . Add ( match . Index ) ;
1812
- lengths . Add ( match . Length ) ;
1822
+ indexes . Add ( new ( match . Index , match . Length ) ) ;
1813
1823
}
1814
1824
}
1815
1825
@@ -1825,8 +1835,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
1825
1835
{
1826
1836
if ( shouldEmphasize )
1827
1837
{
1828
- indexes . Add ( match . Index ) ;
1829
- lengths . Add ( match . Length ) ;
1838
+ indexes . Add ( new ( match . Index , match . Length ) ) ;
1830
1839
}
1831
1840
1832
1841
matches = new Match [ ] { match } ;
@@ -1852,8 +1861,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
1852
1861
{
1853
1862
if ( shouldEmphasize )
1854
1863
{
1855
- indexes . Add ( index ) ;
1856
- lengths . Add ( pat . Length ) ;
1864
+ indexes . Add ( new ( index , pat . Length ) ) ;
1857
1865
}
1858
1866
1859
1867
gotMatch = true ;
@@ -1905,7 +1913,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
1905
1913
1906
1914
// otherwise construct and populate a new MatchInfo object
1907
1915
matchResult = shouldEmphasize
1908
- ? new MatchInfo ( indexes , lengths )
1916
+ ? new MatchInfo ( indexes )
1909
1917
: new MatchInfo ( ) ;
1910
1918
matchResult . IgnoreCase = ! CaseSensitive ;
1911
1919
matchResult . Line = operandString ;
0 commit comments