1
- using System ;
2
-
3
1
namespace RobotsTxt
4
2
{
5
- public class RobotsTxtParser
3
+ public class RobotsTxtParser ( byte [ ] robotsBody , IRobotsParseHandler handler )
6
4
{
7
- static readonly byte [ ] UtfBom = { 0xEF , 0xBB , 0xBF } ;
5
+ static readonly byte [ ] UtfBom = [ 0xEF , 0xBB , 0xBF ] ;
8
6
static readonly byte [ ] HexDigits = "0123456789ABCDEF"u8 . ToArray ( ) ;
9
7
10
8
public void Parse ( )
@@ -21,9 +19,9 @@ public void Parse()
21
19
var lineNum = 0 ;
22
20
var bomPos = 0 ;
23
21
bool lastWasCarriageReturn = false ;
24
- _handler . HandleRobotsStart ( ) ;
22
+ handler . HandleRobotsStart ( ) ;
25
23
26
- foreach ( var ch in _robotsBody )
24
+ foreach ( var ch in robotsBody )
27
25
{
28
26
// Google-specific optimization: UTF-8 byte order marks should never
29
27
// appear in a robots.txt file, but they do nevertheless. Skipping
@@ -62,16 +60,7 @@ public void Parse()
62
60
63
61
var spanLeft = lineBuffer . AsSpan ( 0 , linePos ) ;
64
62
ParseAndEmitLine ( ++ lineNum , spanLeft ) ;
65
- _handler . HandleRobotsEnd ( ) ;
66
- }
67
-
68
- private readonly byte [ ] _robotsBody ;
69
- private readonly IRobotsParseHandler _handler ;
70
-
71
- public RobotsTxtParser ( byte [ ] robotsBody , IRobotsParseHandler handler )
72
- {
73
- _robotsBody = robotsBody ;
74
- _handler = handler ;
63
+ handler . HandleRobotsEnd ( ) ;
75
64
}
76
65
77
66
void ParseAndEmitLine ( int currentLine , ReadOnlySpan < byte > line )
@@ -86,16 +75,15 @@ void ParseAndEmitLine(int currentLine, ReadOnlySpan<byte> line)
86
75
if ( NeedEscapeValueForKey ( key ) )
87
76
{
88
77
var escapedValue = MaybeEscapePattern ( value ) ;
89
- EmitKeyValueToHandler ( currentLine , key , escapedValue , _handler ) ;
78
+ EmitKeyValueToHandler ( currentLine , key , escapedValue ) ;
90
79
}
91
80
else
92
81
{
93
- EmitKeyValueToHandler ( currentLine , key , value , _handler ) ;
82
+ EmitKeyValueToHandler ( currentLine , key , value ) ;
94
83
}
95
84
}
96
85
97
- private void EmitKeyValueToHandler ( int currentLine , ParsedRobotsKey key , ReadOnlySpan < byte > value ,
98
- IRobotsParseHandler handler )
86
+ private void EmitKeyValueToHandler ( int currentLine , ParsedRobotsKey key , ReadOnlySpan < byte > value )
99
87
{
100
88
switch ( key . Type )
101
89
{
0 commit comments