@@ -22,10 +22,15 @@ public class Ingester
22
22
{
23
23
private readonly IngestOptions m_options ;
24
24
25
- private static string [ ] SymbolsToCollapse { get ; } = new [ ]
26
- {
25
+ private static string [ ] SymbolsToCollapse { get ; } =
26
+ [
27
27
"<" , "<=" , "=" , "==" , "=>" , ">" , "!=" , "(" , ")" , "{" , "}" , "[" , "]" , "-" , "+" , "*" , "&" , "%" , "/" , "<<" , ">>" , ";" , "," , "||" , "|" , ":" , "?" , "|"
28
- } ;
28
+ ] ;
29
+
30
+ private static string [ ] FilesToSkip { get ; } =
31
+ [
32
+ "resx" , ".g." , ".designer." , "\\ obj\\ " , "/obj/" , "\\ bin\\ " , "/bin/" , "assemblyinfo.cs" , "/." , "\\ ."
33
+ ] ;
29
34
30
35
public Ingester ( IngestOptions options )
31
36
{
@@ -70,7 +75,7 @@ public Ingester(IngestOptions options)
70
75
using var tempOutputFile = new TempFile ( ) ;
71
76
outputFile ??= tempOutputFile ;
72
77
73
- using ( var outputStream = ( outputFile ) . Open ( FileMode . Create ) )
78
+ using ( var outputStream = outputFile . Open ( FileMode . Create ) )
74
79
using ( var writer = new StreamWriter ( outputStream , Encoding . UTF8 ) )
75
80
{
76
81
writer . NewLine = "\n " ;
@@ -86,64 +91,34 @@ public Ingester(IngestOptions options)
86
91
break ; // Caller requested cancellation.
87
92
progress . Progress = ( int ) ( 100.0 * ( i + 1.0 ) / sourceFiles . Length ) ;
88
93
}
89
-
90
- using var reader = new StreamReader ( sourceFile . FullName , Encoding . UTF8 ) ;
91
94
92
- writer . WriteLine ( $ "// File: { ( m_options . UseFullPaths ? sourceFile . FullName : sourceFile . Name ) } ") ;
93
-
94
- var lineNumber = 1 ;
95
- string line ;
96
- while ( ( line = reader . ReadLine ( ) ) != null )
97
- {
98
- if ( ShouldIncludeSourceLine ( line , m_options ) )
99
- writer . WriteLine ( $ "{ lineNumber . ToString ( ) } |{ GetCodeLine ( line ) . Trim ( ) } ") ;
95
+ if ( m_options . Verbose )
96
+ Logger . Instance . Info ( $ "Processing: { sourceFile . FullName } ") ;
100
97
101
- lineNumber ++ ;
102
- }
98
+ using var reader = new StreamReader ( sourceFile . FullName , Encoding . UTF8 ) ;
99
+ writer . WriteLine ( $ "// File: { ( m_options . UseFullPaths ? sourceFile . FullName : sourceFile . Name ) } " ) ;
103
100
104
- if ( m_options . Verbose )
105
- Logger . Instance . Warn ( $ "{ sourceFile . FullName } processed ({ lineNumber - 1 : N0} lines)") ;
101
+ var iterator = new CodeLineIterator ( reader , m_options . StripComments , m_options . ExcludeImports ) ;
102
+ iterator
103
+ . GetLines ( )
104
+ . ForEach ( ( line , lineIndex ) =>
105
+ {
106
+ var s = GetCodeLine ( line ) ;
107
+ if ( ! string . IsNullOrWhiteSpace ( s ) )
108
+ writer . WriteLine ( $ "{ lineIndex + 1 } |{ s } ") ;
109
+ } ) ;
106
110
}
107
111
}
108
112
109
113
return ( sourceFiles . Length , outputFile . Length ) ;
110
114
}
111
115
112
116
private static bool ShouldSkipFile ( FileInfo f ) =>
113
- new [ ]
114
- {
115
- "resx" , ".g." , ".designer." , "\\ obj\\ " , "/obj/" , "\\ bin\\ " , "/bin/" , "assemblyinfo.cs" , "/." , "\\ ."
116
- } . Any ( o => f . FullName . Contains ( o , StringComparison . OrdinalIgnoreCase ) ) ;
117
-
118
- private static bool ShouldIncludeSourceLine ( string s , IngestOptions options )
119
- {
120
- if ( string . IsNullOrWhiteSpace ( s ) )
121
- return false ;
122
- var trimmed = s . Trim ( ) ;
123
-
124
- if ( options . ExcludeImports )
125
- {
126
- if ( trimmed . StartsWith ( "using" ) || trimmed . StartsWith ( "#include" ) || trimmed . StartsWith ( "#pragma" ) || trimmed . StartsWith ( "namespace" ) || trimmed . StartsWith ( "import" ) || trimmed . StartsWith ( "from " ) )
127
- return false ;
128
- }
129
-
130
- if ( options . StripComments )
131
- {
132
- if ( trimmed . StartsWith ( "//" ) || trimmed . StartsWith ( "# " ) )
133
- return false ;
134
- if ( trimmed . StartsWith ( "/*" ) && trimmed . EndsWith ( "*/" ) )
135
- return false ;
136
- }
137
-
138
- return true ;
139
- }
117
+ FilesToSkip . Any ( o => f . FullName . Contains ( o , StringComparison . OrdinalIgnoreCase ) ) ;
140
118
141
119
private static string GetCodeLine ( string line )
142
120
{
143
- var commentIndex = line . IndexOf ( "//" , StringComparison . Ordinal ) ;
144
- if ( commentIndex >= 0 )
145
- line = line [ ..commentIndex ] ;
146
-
121
+ // De-tab.
147
122
if ( line . Contains ( '\t ' ) )
148
123
line = line . Replace ( '\t ' , ' ' ) ;
149
124
if ( ! line . Contains ( ' ' ) )
0 commit comments