11static class StartEndTester
22{
3- internal static bool IsStartOrEnd ( string trimmedLine ) =>
4- IsBeginSnippet ( trimmedLine ) ||
5- IsEndSnippet ( trimmedLine ) ||
6- IsStartRegion ( trimmedLine ) ||
7- IsEndRegion ( trimmedLine ) ;
3+ internal static bool IsStartOrEnd ( CharSpan line )
4+ {
5+ var trimmedLine = line . Trim ( ) ;
6+ return IsBeginSnippet ( trimmedLine ) ||
7+ IsEndSnippet ( trimmedLine ) ||
8+ IsStartRegion ( trimmedLine ) ||
9+ IsEndRegion ( trimmedLine ) ;
10+ }
811
912 internal static bool IsStart (
10- string trimmedLine ,
11- string path ,
12- [ NotNullWhen ( true ) ] out string ? currentKey ,
13+ CharSpan trimmedLine ,
14+ CharSpan path ,
15+ out CharSpan currentKey ,
1316 [ NotNullWhen ( true ) ] out EndFunc ? endFunc ,
14- out string ? expressiveCode )
17+ out CharSpan expressiveCode )
1518 {
1619 if ( IsBeginSnippet ( trimmedLine , path , out currentKey , out expressiveCode ) )
1720 {
@@ -35,18 +38,18 @@ internal static bool IsStart(
3538
3639 static EndFunc throwFunc = _ => throw new ( "Do not use out func" ) ;
3740
38- static bool IsEndRegion ( string line ) =>
41+ static bool IsEndRegion ( CharSpan line ) =>
3942 line . StartsWith ( "#endregion" , StringComparison . Ordinal ) ;
4043
41- static bool IsEndSnippet ( string line ) =>
44+ static bool IsEndSnippet ( CharSpan line ) =>
4245 IndexOf ( line , "end-snippet" ) >= 0 ;
4346
44- static bool IsStartRegion ( string line ) =>
47+ static bool IsStartRegion ( CharSpan line ) =>
4548 line . StartsWith ( "#region " , StringComparison . Ordinal ) ;
4649
4750 internal static bool IsStartRegion (
48- string line ,
49- [ NotNullWhen ( true ) ] out string ? key )
51+ CharSpan line ,
52+ out CharSpan key )
5053 {
5154 if ( ! line . StartsWith ( "#region " , StringComparison . Ordinal ) )
5255 {
@@ -56,33 +59,28 @@ internal static bool IsStartRegion(
5659
5760 var substring = line [ 8 ..] . Trim ( ) ;
5861
59- if ( substring . Contains ( ' ' ) )
62+ if ( substring . Contains ( ' ' ) ||
63+ ! KeyValidator . IsValidKey ( substring ) )
6064 {
6165 key = null ;
6266 return false ;
6367 }
6468
65- if ( ! KeyValidator . IsValidKey ( substring . AsSpan ( ) ) )
66- {
67- key = null ;
68- return false ;
69- }
70-
71- key = substring ;
69+ key = substring . ToString ( ) ;
7270 return true ;
7371 }
7472
75- static bool IsBeginSnippet ( string line )
73+ static bool IsBeginSnippet ( CharSpan line )
7674 {
7775 var startIndex = IndexOf ( line , "begin-snippet: " ) ;
7876 return startIndex != - 1 ;
7977 }
8078
8179 internal static bool IsBeginSnippet (
82- string line ,
83- string path ,
84- [ NotNullWhen ( true ) ] out string ? key ,
85- out string ? expressiveCode )
80+ CharSpan line ,
81+ CharSpan path ,
82+ out CharSpan key ,
83+ out CharSpan expressiveCode )
8684 {
8785 expressiveCode = null ;
8886 var beginSnippetIndex = IndexOf ( line , "begin-snippet: " ) ;
@@ -117,11 +115,7 @@ internal static bool IsBeginSnippet(
117115 """ ) ;
118116 }
119117
120- expressiveCode = substring [ ( startArgs + 1 ) ..^ 1 ] . Trim ( ) ;
121- if ( expressiveCode . Length == 0 )
122- {
123- expressiveCode = null ;
124- }
118+ expressiveCode = substring [ ( startArgs + 1 ) ..^ 1 ] . Trim ( ) ;
125119 }
126120
127121 if ( key . Length == 0 )
@@ -134,7 +128,7 @@ No Key could be derived.
134128 """ ) ;
135129 }
136130
137- if ( KeyValidator . IsValidKey ( key . AsSpan ( ) ) )
131+ if ( KeyValidator . IsValidKey ( key ) )
138132 {
139133 return true ;
140134 }
@@ -148,14 +142,14 @@ Key cannot contain whitespace or start/end with symbols.
148142 """ ) ;
149143 }
150144
151- static int IndexOf ( string line , string value )
145+ static int IndexOf ( CharSpan line , CharSpan value )
152146 {
153147 if ( value . Length > line . Length )
154148 {
155149 return - 1 ;
156150 }
157151
158152 var charactersToScan = Math . Min ( line . Length , value . Length + 10 ) ;
159- return line . IndexOf ( value , startIndex : 0 , count : charactersToScan , StringComparison . Ordinal ) ;
153+ return line [ .. charactersToScan ] . IndexOf ( value , StringComparison . Ordinal ) ;
160154 }
161155}
0 commit comments